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