SQL查询的性能⼤PK之:orvs.union
sql查询的where条件语句中,可以使⽤and和or实现逻辑的判断。如果where⽐较复杂的话,就会产⽣and 和 or的嵌套使⽤,写起来会很费⼒⽓,看起来就更是⼀头雾⽔了。
  于是有⼈就想起了union,其实它是可以替代or的,反正就是把结果串联起来,貌似应该可以。⽽且,写起来更加容易,看起来也很清晰。但是不知道两个的性能如何呢?下⾯我就做⼀个⽐较,建⽴三张表,分别插⼊10万,100万和1000万的数据,每张表格都有8个字段,然后在三种数据量下,做三个字段的or和union,五个字段的or和union,然后通过查询时间⽐较⼀下他们的效率吧。
  硬件环境:Q8200  4GB 内存
  操作系统:Windows2003 R2
  数据库:SQL SERVER 2005
代码
create database test
go
use test
go
--建⽴测试表1
create table table1
(
col1 varchar(20),
col2 varchar(20),
col3 varchar(20),
col4 varchar(20),
col5 varchar(20),
col6 varchar(20),
col7 varchar(20),
col8 varchar(20)
)
go
--插⼊10万数据
declare@i int
set@i=1
while(@i<100000)
begin
insert into table1 values('123','123','123','123','123','123','123','123')
set@i=@i+1
end
go
--建⽴测试表2
create table table2
(
col1 varchar(20),
col2 varchar(20),
col3 varchar(20),
col4 varchar(20),
col5 varchar(20),
col6 varchar(20),
col7 varchar(20),
col8 varchar(20)
)
go
--插⼊100万数据
declare@i int
set@i=1
while(@i<1000000)
begin
insert into table2 values('123','123','123','123','123','123','123','123')    set@i=@i+1
end
go
--建⽴测试表3
create table table3
(
col1 varchar(20),
col2 varchar(20),
col3 varchar(20),
col4 varchar(20),
col5 varchar(20),
col6 varchar(20),
col7 varchar(20),
col8 varchar(20)
)
go
--插⼊1000万数据
declare@i int
set@i=1
while(@i<1000000)
begin
insert into table3 values('123','123','123','123','123','123','123','123')    set@i=@i+1
end
go
-
-耗时4秒
select*from table1
where col1='123'or col2='123'or col3='123'
go
--耗时11秒
select*from table1
where col1='123'
union all
select*from table1
where col2='123'
union all
select*from table1
where col3='123'
go
--耗时4秒
select*from table1
where col1='123'or col2='123'or col3='123'or col4='123'or col5='123' go
--耗时19秒
select*from table1
where col1='123'
union all
select*from table1
where col2='123'
union all
select*from table1
where col3='123'
union all
select*from table1
where col4='123'
union all
select*from table1
where col5='123'
go
-
-耗时37秒
select*from table2
where col1='123'or col2='123'or col3='123'
go
--耗时1分53秒
select*from table2
where col1='123'
union all
select*from table2
where col2='123'
union all
select*from table2
where col3='123'
go
--耗时38秒
select*from table2
where col1='123'or col2='123'or col3='123'or col4='123'or col5='123'
go
--耗时2分24秒
select*from table2
where col1='123'
union all
select*from table2
where col2='123'
union all
select*from table2
where col3='123'
union all
select*from table2
where col4='123'
union all
select*from table2
where col5='123'
go
drop table table1
drop table table2
drop table table3
drop database test
  从上⾯的可以看出来使⽤or和union连接where条件的话,数据10w和100w没有差距,只是在1000w的时候急速增⼤,但是同等数据量的话,使⽤or和union就表现了很⼤的差距,尽管union写起来和看起来都⽐较好理解。
tabletable
  结论:我想是因为每次使⽤union都会扫描⼀次表结构,or虽然难些难看,但是只扫描⼀次表结构,所以数据量上去的话,就会体现出来更⼤的优势。
  结论仅供参考,欢迎⼤家⼀起讨论。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。