我有一张极其庞大的表, 里面有个时间的字段 atime,我如果要取某天的,如:
select * from table_name
where to_char(atime,'yyyymmdd')='20110402' 太慢了,运行了十多分钟都没有出来,请问有没有其他的方法更快的?
select * from table_name
where to_char(atime,'yyyymmdd')='20110402' 太慢了,运行了十多分钟都没有出来,请问有没有其他的方法更快的?
调试欢乐多
where atime >= date('20110402') and atime <date('20110403');
where to_char(atime,'yyyymmdd')='20110402'
这种情况下,由于to_char(atime,'yyyymmdd')对列进行了转换,用不到索引了。
一般情况下,都是在后面进行转换
select * from table_name
where atime >= date('20110402','yyyy-mm-dd') and atime <date('20110403','yyyy-mm-dd');
提示 missing expression 啊,少了参数吧。
where atime>= timestamp('20110402000000') and atime<timestamp('20110403000000');
where atime>= to_timestamp('20110402','yyyyMMdd') and atime< to_timestamp('20110403','yyyyMMdd');
select * from table_name
where atime>= to_char('20110402','yyyyMMdd') and atime< to_char('20110403','yyyyMMdd');
where atime = to_date('20110402','yyyymmdd')
trunc(atime,'DD')这样是用不到索引的select * from table_name
where atime>= to_char('20110402','yyyyMMdd') and atime< to_char('20110403','yyyyMMdd');
-- *(01) 尽量避免隐式类型转换
-- (例如:如果字段是日期类型的,我会在where语句等式左边用字段原型,而尽量在等式右边用函数,
绝不会在左边用函数转换成字符再去与右边比较);
-- *(02) 尽量少用函数,同样一件事情,能够用两个函数就能解决的,我绝不会嵌套三个函数;
-- (例如:求上个月的最后一天,我会用trunc(sysdate,'mm')-1,而不用last_day(add_months(sysdate,-1)) )
-- *(03) 在所有的存储过程中,我会尽量用绑定变量,以避免硬解析带来的资源消耗!
-- *(04) 在所有的存储过程中,能够用SQL语句的,我绝不会用循环去实现!
-- (例如:列出上个月的每一天,我会用connect by去递归查询一下,绝不会去用循环从上个月第一天到最后一天)
-- *(05) 当有一批处理的插入或更新时,我会用批量插入或批量更新,绝不会一条条记录的去更新!
alter session set nls_date_format='YYYY-MM-DD';
然后直接用:
select * from table_name where atime='20110402'