1. 程式人生 > >oracle建立使用索引 查詢索引狀態

oracle建立使用索引 查詢索引狀態

                SQL> conn tudou/111;

//插入測試資料

SQL> insert into temp1 values('z壯','23');

SQL> insert into temp1 values('z','25');

SQL> insert into temp1 values('a','28');

SQL> insert into temp1 values('奇才','29');

SQL> insert into temp1 values('才子、','12');

SQL> insert into temp1 values('二飄落','14');

SQL> insert into temp1 values('脾用','18');

SQL> select * from temp1;

NAME                AGE
------------------- ---
土土                22
z壯                 23
z                   25
a                   28
奇才                29
才子、              12
二飄落              14
脾用                18

SQL> insert into temp1 values('fdffs','18');

SQL> insert into temp1 values('ppppp','18');

SQL> insert into temp1 values('大規模 ','13');

SQL> select * from temp1;

NAME                AGE
------------------- ---
土土                22
z壯                 23
z                   25
a                   28
奇才                29
才子、              12
二飄落              14
脾用                18
fdffs               18
ppppp               18
大規模              13

//建立索引,索引建立在name之上。

SQL> create index ix_name on temp1(name);

Index created

//監視索引

SQL> alter index ix_name monitoring usage;

Index altered

//檢視索引狀態,發現USED這列顯示為no,說明索引尚未使用。

SQL> select * from v$object_usage;

INDEX_NAME                     TABLE_NAME                     MONITORING USED START_MONITORING    END_MONITORING
------------------------------ ------------------------------ ---------- ---- ------------------- -------------------
IX_NAME                        TEMP1                          YES        NO   02/21/2011 11:44:26

//查詢欄位,模糊查詢的欄位正是建立索引的欄位。

SQL> select * from temp1 where name like '%z%';

NAME                AGE
------------------- ---
z壯                 23
z                   25

//再次查詢索引狀態,發現used列還是no。說明索引在使用like模糊查詢時未起作用。不過好像like '%z%';寫成like 'z%'索引就會起作用。

SQL> select * from v$object_usage;

INDEX_NAME                     TABLE_NAME                     MONITORING USED START_MONITORING    END_MONITORING
------------------------------ ------------------------------ ---------- ---- ------------------- -------------------
IX_NAME                        TEMP1                          YES        NO   02/21/2011 11:44:26

//不要緊,我們這次用精確查詢

SQL> select name from temp1;

NAME
-------------------
土土
z壯
z
a
奇才
才子、
二飄落
脾用
fdffs
ppppp
大規模

11 rows selected

//再次查詢看發現索引還是不起作用,這說明直接查詢欄位名稱索引不起作用。

SQL> select * from v$object_usage;

INDEX_NAME                     TABLE_NAME                     MONITORING USED START_MONITORING    END_MONITORING
------------------------------ ------------------------------ ---------- ---- ------------------- -------------------
IX_NAME                        TEMP1                          YES        NO   02/21/2011 11:44:26

//最後我們用條件查詢

SQL> select * from temp1 where name ='a';

NAME                AGE
------------------- ---
a                   28

//OK,索引在條件查詢時起作用了。這時索引才真正發揮它的作用。

SQL> select * from v$object_usage;

INDEX_NAME                     TABLE_NAME                     MONITORING USED START_MONITORING    END_MONITORING
------------------------------ ------------------------------ ---------- ---- ------------------- -------------------
IX_NAME                        TEMP1                          YES        YES  02/21/2011 11:44:26