日期:2014-05-16  浏览次数:20953 次

一个表里面的两个日期字段比较大小
select * from A where A.date1>A.date2 这样执行sql数据一大效率很慢 有没有办法提高效率
------解决方案--------------------
A.date1>A.date2 这个我觉得快不了,因为这个是会用全表扫描的。

建议新增一个字段,比如flags,然后update一下这个字段,如果A.date1>A.date2  那么就是1,否则就是0.:

update a
set = case when date1>date2 then 1 else 0 end


这样,查询的时候,给这个flag字段建一个索引,速度就快了:

select * from A where flag= 1
------解决方案--------------------
不要用left join,改为inner join