全局索引 truncate有数据的分区,索引失效,没数据的分区,索引不失效

查看索引状态:
SQL> select index_name, index_type, partitioned, status, global_stats
  from dba_indexes
 where
 table_name = 'F_AGT_BUSINESS_CONTRACT_H';  2    3    4  

INDEX_NAME		       INDEX_TYPE		   PAR STATUS	GLO
------------------------------ --------------------------- --- -------- ---
SYS_C0091072		       NORMAL			   NO  VALID	NO


GLOBAL_STATS 不表示是本地索引还是全局索引 

查看user_part_indexes

SQL> select * from user_part_indexes where index_name='SYS_C0091072';

no rows selected

此时没有记录


怎么才能让全局索引失效呢?
truncate 一个分区试试?

SQL> alter table dwf.f_agt_business_contract_h truncate  partition p20101230;

Table truncated.

SQL> select index_name, index_type, partitioned, status, global_stats
  from dba_indexes
 where
 table_name = 'F_AGT_BUSINESS_CONTRACT_H';  2    3    4  

INDEX_NAME		       INDEX_TYPE		   PAR STATUS	GLO
------------------------------ --------------------------- --- -------- ---
SYS_C0091072		       NORMAL			   NO  VALID	NO

删除一个有数据的分区呢?

SQL> alter table dwf.f_agt_business_contract_h truncate  partition p20111230;

SQL> select index_name, index_type, partitioned, status, global_stats
  from dba_indexes
 where
 table_name = 'F_AGT_BUSINESS_CONTRACT_H';   2    3    4  

INDEX_NAME		       INDEX_TYPE		   PAR STATUS	GLO
------------------------------ --------------------------- --- -------- ---
SYS_C0091072		       NORMAL			   NO  UNUSABLE NO

此时索引已经失效

SQL> explain plan for select *
  from dwf.f_agt_business_contract_h
 where AGMT_ID = '20100611000052'
   and CORP_ORG = 15601
   and end_dt = date '2011-1-8';  2    3    4    5  
SQL> select * from table(dbms_xplan.display());

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1549525094

--------------------------------------------------------------------------------------------------------------------
| Id  | Operation	       | Name			   | Rows  | Bytes | Cost (%CPU)| Time	   | Pstart| Pstop |
--------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT       |			   |	 1 |  5039 |   411  (20)| 00:00:05 |	   |	   |
|   1 |  PARTITION RANGE SINGLE|			   |	 1 |  5039 |   411  (20)| 00:00:05 |	11 |	11 |
|*  2 |   TABLE ACCESS FULL    | F_AGT_BUSINESS_CONTRACT_H |	 1 |  5039 |   411  (20)| 00:00:05 |	11 |	11 |
--------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("AGMT_ID"='20100611000052' AND TO_NUMBER("CORP_ORG")=15601 AND "END_DT"=TO_DATE(' 2011-01-08
	      00:00:00', 'syyyy-mm-dd hh24:mi:ss'))

Note
-----
   - dynamic sampling used for this statement

19 rows selected.


重建索引:
SQL> alter index SYS_C0091072 rebuild online nologging parallel 16;

Index altered.

SQL> explain plan for select *
  from dwf.f_agt_business_contract_h
 where AGMT_ID = '20100611000052'
   and CORP_ORG = 15601
   and end_dt = date '2011-1-8';  2    3    4    5  

Explained.

SQL> select * from table(dbms_xplan.display());

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 942726628

--------------------------------------------------------------------------------------------------------------------------------
| Id  | Operation			   | Name		       | Rows  | Bytes | Cost (%CPU)| Time     | Pstart| Pstop |
--------------------------------------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT		   |			       |  1762 |  8670K|  1661	 (1)| 00:00:20 |       |       |
|*  1 |  TABLE ACCESS BY GLOBAL INDEX ROWID| F_AGT_BUSINESS_CONTRACT_H |  1762 |  8670K|  1661	 (1)| 00:00:20 |    11 |    11 |
|*  2 |   INDEX RANGE SCAN		   | SYS_C0091072	       |   705 |       |   670	 (1)| 00:00:09 |       |       |
--------------------------------------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("END_DT"=TO_DATE(' 2011-01-08 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
   2 - access("AGMT_ID"='20100611000052')
       filter(TO_NUMBER("CORP_ORG")=15601)

Note
-----
   - dynamic sampling used for this statement

20 rows selected.

此时已经走索引,查看索引状态

SQL> select index_name, index_type, partitioned, status, global_stats
  from dba_indexes
 where
 table_name = 'F_AGT_BUSINESS_CONTRACT_H';  2    3    4  

INDEX_NAME		       INDEX_TYPE		   PAR STATUS	GLO
------------------------------ --------------------------- --- -------- ---
SYS_C0091072		       NORMAL			   NO  VALID	NO

 

你可能感兴趣的:(sql,查询优化)