【oracle】truncate 操作对索引的影响

Posted on 2012-03-17 15:06  雅飞士  阅读(3841)  评论(0编辑  收藏  举报

 

由于测试库的需要,每个月要对部分表的数据清空,然后插入新的数据。
然后就在想truncate后对原表上的索引是否有影响。
测试实验如下;

数据库版本:
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
PL/SQL Release 10.2.0.3.0 - Production
CORE    10.2.0.3.0    Production
TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production

新建分区表测试用:3个分区
create table testtruncate (num number,name char(2))
partition by range(num)
(partition a values less than(10),
partition b values less than(20),
partition c values less than(30));

向3个分区插入数据,没个分区一条:
insert into testtruncate values(5,'a');
insert into testtruncate values(15,'b');
insert into testtruncate values(25,'c');

commit;

创建两个索引,一个全局索引,一个本地索引:
create index idx_g_tst on testtruncate (num) ;
create index idx_l_tst on testtruncate (name) local;

truncate之前查看索引状态:
SQL> SELECT INDEX_NAME,TABLE_NAME,STATUS FROM USER_INDEXES WHERE INDEX_NAME LIKE '%TST%';
INDEX_NAME                     TABLE_NAME                     STATUS
------------------------------ ------------------------------ --------
IDX_G_TST                      TESTTRUNCATE                   VALID
IDX_L_TST                      TESTTRUNCATE                   N/A

SQL> select index_name,status from user_ind_partitions  where index_name like '%TST%';
INDEX_NAME                     STATUS
------------------------------ --------
IDX_L_TST                      USABLE
IDX_L_TST                      USABLE
IDX_L_TST                      USABLE

结果显示都是可用状态,下面先测试 truncate 分区对索引的影响:
SQL> alter table testtruncate truncate partition a;
Table truncated

查看索引状态:
SQL> SELECT INDEX_NAME,TABLE_NAME,STATUS FROM USER_INDEXES WHERE INDEX_NAME LIKE '%TST%';
INDEX_NAME                     TABLE_NAME                     STATUS
------------------------------ ------------------------------ --------
IDX_G_TST                      TESTTRUNCATE                   UNUSABLE
IDX_L_TST                      TESTTRUNCATE                   N/A

SQL> select index_name,status from user_ind_partitions  where index_name like '%TST%';
INDEX_NAME                     STATUS
------------------------------ --------
IDX_L_TST                      USABLE
IDX_L_TST                      USABLE
IDX_L_TST                      USABLE

结果显示,truncate分区使全局索引失效,对本地索引没有影响。

重建全局索引,使其生效。
SQL> alter index idx_g_tst rebuild;
Index altered
SQL> SELECT INDEX_NAME,TABLE_NAME,STATUS FROM USER_INDEXES WHERE INDEX_NAME LIKE '%TST%';
INDEX_NAME                     TABLE_NAME                     STATUS
------------------------------ ------------------------------ --------
IDX_G_TST                      TESTTRUNCATE                   VALID
IDX_L_TST                      TESTTRUNCATE                   N/A

测试 truncate 表对索引的影响:
SQL> truncate table testtruncate;
Table truncated
SQL> SELECT INDEX_NAME,TABLE_NAME,STATUS FROM USER_INDEXES WHERE INDEX_NAME LIKE '%TST%';
INDEX_NAME                     TABLE_NAME                     STATUS
------------------------------ ------------------------------ --------
IDX_G_TST                      TESTTRUNCATE                   VALID
IDX_L_TST                      TESTTRUNCATE                   N/A
SQL> select index_name,status from user_ind_partitions  where index_name like '%TST%';
INDEX_NAME                     STATUS
------------------------------ --------
IDX_L_TST                      USABLE
IDX_L_TST                      USABLE
IDX_L_TST                      USABLE

结果显示,truncate 表 对两种索引都没有影响。

Copyright © 2024 雅飞士
Powered by .NET 9.0 on Kubernetes