sql语句or与union all的执行效率哪个更高
如题
第一种写法:
with pagelist as (
select * from table where table_column=1
union all
select * from table where table_column=2
)
select * from pagelist
第二种写法:
with pagelist as (
select * from table where table_column=1 or table_column=2
)
select * from pagelist
请教下,以上两种写法,哪个效率更高些,为什么?
[解决办法]
为什么是union all,我觉得是or,毕竟这里面不用连接
[解决办法]
union all
[解决办法]
查看执行计划就明白了
[解决办法]
所以说选择union all是没错D,不过还是建议用1楼的写法.
[解决办法]
ctrl + L
[解决办法]
长知识了!
[解决办法]
or不走索引了,UNION ALL每次单独查询可以走索引,UNION ALL好一点。但是从时间上来看需要看数据量,数据量大的话检索出来的数据小,肯定是UNION ALL效率高。