我有两个表: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