查询同一张表,该表结构为
create table log (a varchar(10),b date,c varchar(20));其中,a 和 a,b分别为索引
查询语句1:
select * from log where a='A1000' and b>=to_date(...) and b<=to_date(...) order by b;用时2秒。
查询语句2:
select * from log where a='A2314' and b>=to_date(...) and b<=to_date(...) order by b;用时2分多。返回的记录数差不多,都是1000多条。为什么会有这么大的差别?怎么分析这种情况?
create table log (a varchar(10),b date,c varchar(20));其中,a 和 a,b分别为索引
查询语句1:
select * from log where a='A1000' and b>=to_date(...) and b<=to_date(...) order by b;用时2秒。
查询语句2:
select * from log where a='A2314' and b>=to_date(...) and b<=to_date(...) order by b;用时2分多。返回的记录数差不多,都是1000多条。为什么会有这么大的差别?怎么分析这种情况?
解决方案 »
- 【万分火急】oracle不使用escape,如何使用like实现含有"_"或"%"的模糊匹配?
- 100分 求oracle备份方案
- 请问一个数据导入到oracle中的乱码问题!
- 如何在linux下手工添加oracle数据库呢?希望大虾们不要不屑于回答,wait online....
- 求一个sql语句
- 小妹有个问题想请问各位大侠高手
- 一个关于ORDER BY 的问题?
- 寫一個函數(用Oracle完成)
- oracle 怎么将表中某一列的值统一下移
- 请教如何同比数据?
- 我装的是9i,开机时总是报日志文件错误,说什么至少一个或一个以上服务错误什么的,我查看了事件管理器和服务,是以下的两个没起来
- 求Oracle下类似于SQLServer中sp_bindefault的系统存储过程?
而a='A1000'的就比较少,使用了索引还是看一下执行计划有什么区别吧。
语句1:
Execution Plan
----------------------------------------
0 select statement optimizer=choose
1 0 sort<order by>
2 1 table access <by index rowid> of 'log'
3 2 index <range scan> of 'idx_a' <non-unique>
Statistics
-------------------------------------
0 recursive calls
0 db block gets
3125 consistent gets
2638 physical reads
60 redo size
43981 bytes sent via SQL*Net to client
1218 bytes received via SQL*Net from client
67 SQL*Net roundtrips to/from client
1 sorts<memory>
0 sorts<disk>
979 rows processed
语句2:
Execution Plan
----------------------------------------
0 select statement optimizer=choose
1 0 sort<order by>
2 1 table access <by index rowid> of 'log'
3 2 index <range scan> of 'idx_a' <non-unique>Statistics
-------------------------------------
0 recursive calls
0 db block gets
43672 consistent gets
43152 physical reads
52 redo size
73252 bytes sent via SQL*Net to client
1504 bytes received via SQL*Net from client
93 SQL*Net roundtrips to/from client
1 sorts<memory>
0 sorts<disk>
1368 rows processed
从你的执行计划看来,第二步是“1 0 sort<order by>”,也就是说从这里开始你原本的SQL就没有采用INDEX (a,b),因此后面的执行也就会变得非常缓慢。
dbms_stats.gather_table_stats('用户名','表名');如果还没有使用索引可以使用提示符强制使用索引
select /*+ INDEX(idx_ab)*/ * from log where a='A2314' and b>=to_date(...) and b<=to_date(...) order by b