我有兩個表:SQL查詢去爲全表掃描,而不是基於索引掃描
create table big(id number, name varchar2(100));
insert into big(id, name) select rownum, object_name from all_objects;
create table small as select id from big where rownum < 10;
create index big_index on big(id);
,如果我執行以下查詢這些表:
select *
from big_table
where id like '45%'
or id in (select id from small_table);
它總是去了一個完整的表掃描。
Execution Plan
----------------------------------------------------------
Plan hash value: 2290496975
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 3737 | 97162 | 85 (3)| 00:00:02 |
|* 1 | FILTER | | | | | |
| 2 | TABLE ACCESS FULL| BIG | 74718 | 1897K| 85 (3)| 00:00:02 |
|* 3 | TABLE ACCESS FULL| SMALL | 1 | 4 | 3 (0)| 00:00:01 |
----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("ID"=45 OR EXISTS (SELECT /*+ */ 0 FROM "SMALL" "SMALL"
WHERE "ID"=:B1))
3 - filter("ID"=:B1)
是否有任何方法可以重寫查詢以便始終進行索引掃描。
那是一個錯字 - 覺得這一定是要在其數據類型的列使用這樣的錯誤是不 – 2011-03-10 04:59:53
相關的字符串(VARCHAR2等)所以你想要以45開頭的ID?像45,45029和451? – 2011-03-10 05:48:14
是的,你是對的 – Ajitesh 2011-03-10 10:21:35