sql语句or与union all的执行效率哪个更高解决思路
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
请教下,以上两种写法,哪个效率更高些,为什么?
------解决方案--------------------
这么写比较好,
如题
第一种写法:
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
请教下,以上两种写法,哪个效率更高些,为什么?
------解决方案--------------------
这么写比较好,
- SQL code
select * from table where table_column in(1,2)
------解决方案--------------------
如果一定要选,我选union all
------解决方案--------------------
为什么是union all,我觉得是or,毕竟这里面不用连接
------解决方案--------------------
union all
------解决方案--------------------
------解决方案--------------------
所以说选择union all是没错D,不过还是建议用1楼的写法.
------解决方案--------------------
ctrl + L
------解决方案--------------------
长知识了!
------解决方案--------------------
or不走索引了,UNION ALL每次单独查询可以走索引,UNION ALL好一点。但是从时间上来看需要看数据量,数据量大的话检索出来的数据小,肯定是UNION ALL效率高。