收集统计信息,之后可以看到在dba_indexes中依然显示存在的索引叶块,优化器从而使用该索引。
SQL> exec dbms_stats.gather_table_stats('SYS','TEST',cascade=>true);
PL/SQL procedure successfully completed.
SQL> select index_name,leaf_blocks,num_rows,degree from dba_indexes where index_name='TEST_IDX1';
INDEX_NAME LEAF_BLOCKS NUM_ROWS DEGREE
------------------------------ ----------- ---------- ----------------------------------------
TEST_IDX1 1 1 1
SQL> set autotrace trace exp
sSQL>
SQL> select * from test where id<20;
Execution Plan
----------------------------------------------------------
Plan hash value: 2624864549
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 7 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| TEST | 1 | 7 | 3 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | TEST_IDX1 | 1 | | 2 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("ID"<20)
SQL>
eg:
SQL> select * from test;
ID TEX
---------- ---
10000 Amy
SQL> insert into test values(10001,'Rhys');
insert into test values(10001,'Rhys')
*
ERROR at line 1:
ORA-12899: value too large for column "SYS"."TEST"."TEXT" (actual: 4, maximum: 3)
SQL> desc test
Name Null Type
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
ID NUMBER
TEXT CHAR(3)
SQL> alter table test modify text char(15);
Table altered.
SQL> insert into test values(10001,'Rhys');
1 row created.
SQL> commit;
Commit complete.
SQL> select height,
2 lf_rows,
3 lf_blks,
4 del_lf_rows,
5 btree_space,
6 used_space,
pct_used,
7 8 ((used_space - del_lf_rows_len) / btree_space) pct_unused,
9 ((lf_rows - del_lf_rows) / lf_rows) pct_undel_rows
10 from index_stats
11 where name = 'TEST_IDX1';
HEIGHT LF_ROWS LF_BLKS DEL_LF_ROWS BTREE_SPACE USED_SPACE PCT_USED PCT_UNUSED PCT_UNDEL_ROWS
---------- ---------- ---------- ----------- ----------- ---------- ---------- ---------- --------------
2 10000 21 9999 175944 150021 86 .001329969 .0001
SQL> analyze index test_idx1 validate structure;
Index analyzed.
SQL> select height,
2 lf_rows,
3 lf_blks,
del_lf_rows,
4 5 btree_spac