oracle之date 查询性能优化

来源:互联网 发布:淘宝介入后还能评价吗 编辑:程序博客网 时间:2024/05/29 23:24

创建测试表

created table test(

id varchar2(20) not null,

dtime date

)

插入6000000数据

declare
i number;
k number;
begin
k:=0;
for i in 1 .. 6000000 loop
insert into test(id,dtime) values('1',sysdate-k);
if i/10000 =0 then
k:=k+1;
commit;
end if;
end loop;
commit;
end;

第一种查询语句

select * from test where to_char(dtime,'yyyy-MM-dd')='2013-02-12';

用时:1654ms

第二种查询语句

select * from test t where dtime between to_date('2012-02-12 00:00:00','yyyy-MM-dd HH24:mi:ss')
and to_date('2012-02-12 23:59:59','yyyy-MM-dd HH24:mi:ss');

用时:203ms

第三种查询语句

select * from test where dtime = to_date('2012-02-12 00:00:00','yyyy-MM-dd HH24:mi:ss');

用时:188ms

说明:字符进行比较是字节码的比较

           date是整数比较

请纠错!谢谢