日期:2014-05-17  浏览次数:21142 次

怎么写查询速度快?
type列有 1,2,3 三个值,无索引。
现需要卡1,2取数,三种写法:
第一种:type<>3
第二种:type in (1,2)
第三种:type=1 or type=2
哪种写法查询快?为什么?

菜鸟求问简单问题,望不吝赐教,若有更优的写法请说明。

------解决方案--------------------
一般说来  查询花费时间由大到小排序 2 > 3 > 1 

------解决方案--------------------
在CBO的模式下,2和3会转化成同样的路径(11g环境)
------解决方案--------------------
中间用union all连接
select count(*) from 
( select * from 表名 where type=1 
union all
select * from 表名 where type=2 ) 

------解决方案--------------------
如果1经常被读到
第三种:type=1 or type=2
这是比较快的
type=2优化器是不会再去进行逻辑判断