当前位置:网站首页>online 方式创建索引触发trigger怎么办?
online 方式创建索引触发trigger怎么办?
2022-08-03 18:19:00 【墨天轮】
背景:
- 为防止误删除数据,我们会在数据库中创建trigger,禁止对表进行drop或者truncate。
- 通常给表创建索引时候,为了避免创建索引过程中锁表引起性能问题,我们会要求创建索引时候加上online参数。
普通创建索引 和 online 方式创建索引的区别:
① 普通创建索引的命令create index会先锁表,然后再创建索引,如果表中数据量很大,会造成阻塞DML语句;
② create index online 允许其它会话修改索引字段,但如果修改索引字段的会话没有commit或是rollbak,则索引创建会被阻塞。
但是加online字段有一些限制:
实验:
1. 创建trigger
-- 创建trigger,禁止对表进行drop和truncate操作SQL> conn / as sysdbaConnected.SQL> create or replace trigger trigger_undroptable BEFORE DROP or TRUNCATE ON DATABASE begin end if; if ora_login_user not in ('SYS','SYSTEM') THEN if upper(dictionary_obj_type) ='TABLE' THEN Raise_application_error (-20001,'Please not do DROP or TRUNCATE Table,You will be Caught!!!'); end if; end if; end; /Trigger created.
2. 创建测试表
SQL> conn sxc/sxcConnected.SQL> create table t(a int primary key,b int,c int,d int);Table created.SQL> set line222SQL> select index_name,table_name from user_indexes;INDEX_NAME TABLE_NAME----------------------------------- ----------------------------------------SYS_C0014935 T-- 插入数据SQL> BEGIN FOR X IN 1 .. 1000 LOOP INSERT INTO t VALUES (x,x+1,x+2,x+3); COMMIT; DBMS_LOCK.SLEEP(0.2); END LOOP;END;/
3. 使用online方式创建索引
-- 使用online 方式创建索引SQL> create index idx1 on t (b) online;create index idx1 on t (b) online *ERROR at line 1:ORA-00604: error occurred at recursive SQL level 2ORA-20001: Please not do DROP or TRUNCATE Table,You will be Caught!!!ORA-06512: at line 4ORA-00604: error occurred at recursive SQL level 2ORA-20001: Please not do DROP or TRUNCATE Table,You will be Caught!!!ORA-06512: at line 4SQL> select index_name,table_name,STATUS from user_indexes;INDEX_NAME TABLE_NAME STATUS------------------------------------------- ------------------------------------------------------------SYS_C0014935 T VALIDIDX1 T VALIDSYS_IOT_TOP_94130 SYS_JOURNAL_94129 VALID
结论:可以看到创建索引时,触发了trigger_undroptable触发器,创建报错。但是我们发现IDX1索引也创建好了,并且多出了SYS_JOURNAL_94129表和索引,状态都是VALID。
4. 校验索引
SQL> set line222SQL> set autotrace off SQL> alter session set statistics_level=all ;Session altered.SQL> VARIABLE id NUMBERSQL> EXECUTE :id := 10;PL/SQL procedure successfully completed.SQL> SELECT count(*) FROM t WHERE b < :id; COUNT(*)---------- 8SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'));PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------SQL_ID d3f4gyz6wsjmn, child number 0-------------------------------------SELECT count(*) FROM t WHERE b < :idPlan hash value: 3080277828------------------------------------------------------------------------------------| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | | 1 |00:00:00.01 | 2 || 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:00:00.01 | 2 |PLAN_TABLE_OUTPUT------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|* 2 | INDEX RANGE SCAN| IDX1 | 1 | 8 | 8 |00:00:00.01 | 2 |------------------------------------------------------------------------------------Predicate Information (identified by operation id):--------------------------------------------------- 2 - access("B"<:ID)Note----- - dynamic sampling used for this statement (level=2)23 rows selected.
结论:可以看到SQL语句执行计划中,用到了索引IDX1 ,说明可以正常使用。
5. 删除索引
SQL> drop index idx1;drop index idx1 *ERROR at line 1:ORA-08104: this index object 94129 is being online built or rebuilt-- 删除index 报错,无法正常删除。-- 解决办法:-- 查询object_id 94129 就是我们创建的indexSQL> set line222SQL> col owner for a10SQL> col OBJECT_NAME for a10SQL> select owner,object_name,object_id,object_type from dba_objects where object_id='94129';OWNER OBJECT_NAM OBJECT_ID OBJECT_TYPE---------- ---------- ---------- --------------------------------------SXC IDX1 94129 INDEX[[email protected] trace]$ sqlplus / as sysdbaSQL*Plus: Release 11.2.0.4.0 Production on Wed Aug 3 15:32:19 2022Copyright (c) 1982, 2013, Oracle. All rights reserved.Connected to:Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit ProductionWith the Partitioning, OLAP, Data Mining and Real Application Testing optionsSQL> declare 2 done boolean; 3 begin 4 done:=dbms_repair.online_index_clean(94129); ---OBJECT_ID 5 end; 6 /PL/SQL procedure successfully completed.-- 再次查询 idx1 没有了SQL> select index_name,table_name from user_indexes;INDEX_NAME TABLE_NAME------------------------------------------------------------ -----------------------------------------------SYS_C0014935 T
6. 通过10046 trace 查看创建索引过程
SQL> set autotrace offSQL> alter session set statistics_level=typical; Session altered.SQL> alter session set events '10046 trace name context forever,level 12';Session altered.SQL> create index idx1 on t (b) online;create index idx1 on t (b) online *ERROR at line 1:ORA-00604: error occurred at recursive SQL level 2ORA-20001: Please not do DROP or TRUNCATE Table,You will be Caught!!!ORA-06512: at line 4ORA-00604: error occurred at recursive SQL level 2ORA-20001: Please not do DROP or TRUNCATE Table,You will be Caught!!!ORA-06512: at line 4SQL> alter session set events '10046 trace name context off'; Session altered.SQL> select SPID from v$process where addr=(select PADDR from v$session where sid=(select distinct sid from v$mystat));SPID------------------------------------------------1820-- 查看trace文件[[email protected] trace]$ tkprof LHR11G_ora_1820.trc LHR11G_ora_1820.txt
结论:可以发现使用online参数建立索引要建立一个临时SYS_JOURNAL_94130 IOT表,索引创建完成后再drop table时,触发到trigger。
7. rebuild index online
-- 普通方式rebuild 正常SQL> alter index SYS_C0014935 rebuild;Index altered.-- oline方式rebuild 报错SQL> alter index SYS_C0014935 rebuild online;alter index SYS_C0014935 rebuild online*ERROR at line 1:ORA-00604: error occurred at recursive SQL level 2ORA-20001: Please not do DROP or TRUNCATE Table,You will be Caught!!!ORA-06512: at line 4ORA-00604: error occurred at recursive SQL level 2ORA-20001: Please not do DROP or TRUNCATE Table,You will be Caught!!!ORA-06512: at line 4
结论:使用online 方式rebuild index 同样也会触发trigger报错。
综上: 当我们数据库中存在禁止drop table trigger 时候,我们使用online 方式创建索引,需要先disable trigger 或者使用trigger 约束范围以外的用户进行创建。
边栏推荐
- China Hashpower Conference Ascension Kunpeng Ecological Forum was held; Kuaishou established an independent to B business department…
- Shell:循环语句
- flink-sql 客户端,咋回事 我show tables 报错
- 微信小程序分享功能
- 技术开发人员常用的安全浏览器
- 谷歌浏览器安装插件教程步骤,开发用这2个插件工作效率倍增
- CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes), problem: (D) Magical Array
- Digital IC Handwriting - MCMM, WNS and TNS
- USD 能统一元宇宙吗?
- ASA归因:如何评估关键词的投放价值
猜你喜欢
随机推荐
理想L9旗舰级的安全性有多强?守护一家人安全出行“底线”
cell delay和net delay
Atomic Wallet已支持TRC20-USDT
宝塔搭建企业招聘网站源码实测
Install porterLB
这是Facebook母公司 关于元宇宙的80万亿美元豪赌
实时渲染器不止lumion,Chaos Vantage你值得一试
常见荧光染料修饰多种基团及其激发和 发射波长数据一览数据
异常与智能指针
PHP基础笔记-NO.2
Flask框架——项目可安装化
广告电商、泰山众筹、链动2+1,这3个模式到底怎么样?
揭秘deepin 23,从这里开始!
CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-E
5000元价位高性能轻薄本标杆 华硕无双高颜能打
87.(cesium之家)cesium热力图(贴地形)
VsCode预览Geojson数据
Oracle备份的几种方式
动态接口比例性能测试实践
大佬们,flinkcdc 2.2 版本采集sqlserver只能采集到全量的数据,不能采集到增量的数