if not object_id('tb') is null
drop table tb
Go
Create table tb([gid] int,[tel1] nvarchar(11),[tel2] nvarchar(11),[tel3] nvarchar(11))
Insert tb
select 20081234,15910600407,N'0104781050',15847323223 union all
select 20081235,15910600403,N'0104781051',15847323227
Go
Select * from tb
declare @i int,@tel1 char(11),@tel2 char(11),@tel3 char(11),@id bigint
select @i=1,@tel1='15910600407',@tel2='0104781051',@tel3='15847323227'
,@id=20081235
while @i<100000
begin
set @tel1=left(@tel1,4)+cast(cast(right(@tel1,7) as bigint)+@i as char(6))
set @tel2=left(@tel2,4)+cast(cast(right(@tel2,7) as bigint)+@i as char(6))
set @tel3=left(@tel3,4)+cast(cast(right(@tel3,7) as bigint)+@i as char(6))
set @id=@id+@i
insert into tb
values(@id,@tel1,@tel2,@tel3)
set @i=@i+1
end
第一次测试:没有建立任何索引
select gid from tb where tel1=@tel or tel2=@tel or tel3=@tel 需要时间480ms
select gid from tb where tel1=@tel union select gid from tb where tel2=@tel
union select gid from tb where tel3=@tel 需要时间700ms
把两个分别做成存储过程
第一个sql做成存储过程 需要时间480ms
第二个sql做成存储过程 需要时间690ms
第二次测试:在tel1、tel2、tel3上建立联合索引
1、查询时不指定索引
1)select gid from tb where tel1=@tel or tel2=@tel or tel3=@tel 需要时间420ms
2)select gid from tb where tel1=@tel union select gid from tb where tel2=@tel
union select gid from tb where tel3=@tel 需要时间360ms
把两个分别做成存储过程
第一个sql做成存储过程 需要时间420ms
第二个sql做成存储过程 需要时间350ms
2、查询时指定索引
1)select gid from tb(index(i_tb)) where tel1=@tel or tel2=@tel or tel3=@tel 需要时间420ms
2)select gid from tb (index(i_tb)) where tel1=@tel
union
select gid from tb (index(i_tb)) where tel2=@tel
union
select gid from tb (index(i_tb)) where tel3=@tel 需要时间360ms
把两个分别做成存储过程
第一个sql做成存储过程 需要时间420ms
第二个sql做成存储过程 需要时间350ms
第三次测试: 在tel1,tel2,tel3上分别建立索引
create index i_tel1 on tb(tel1)
create index i_tel2 on tb(tel2)
create index i_tel3 on tb(tel3)
1、查询时不指定索引
1) select gid from tb where tel1=@tel or tel2=@tel or tel3=@tel 当页面在内存中,需要时间0ms,不在是需要10ms
2) select gid from tb where tel1=@tel
union
select gid from tb where tel2=@tel
union
select gid from tb where tel3=@tel 当页面在内存中,需要时间0ms,不在是需要10ms
2、查询时指定索引
1)select gid from tb(index(i_tel1,i_tel2,i_tel3)) where tel1=@tel or tel2=@tel or tel3=@tel 需要时间:16000-22000
2)select gid from tb (index(i_tel1)) where tel1=@tel
union
select gid from tb (index(i_tel2)) where tel2=@tel
union
select gid from tb (index(i_tel3)) where tel3=@tel 当页面在内存中,需要时间0ms,不在是需要10ms
2、
把两个分别做成存储过程