300万条记录 like 和 charindex 函数性能比较 .

来源:互联网 发布:淘宝小米手机专卖店 编辑:程序博客网 时间:2024/05/01 21:24

300万条记录 like 和 charindex 函数性能比较

环境:sql2005

数据量:300万

查询结果数据量:127221

机器环境

P4 3.0双核 1G内存

 

1.         执行语句

--无索引

select count(*) from testingwhere [name] like'%00%'

select count(*) from testingwhere charindex('00',[name])>0

--有索引

select id from testing where [name] like '%00%'

select id from testing where charindex('00',[name])>0

--查询字段全部为索引

select id,[name]from testing where [name]like '%00%'

select id,[name]from testing wherecharindex('00',[name])>0

--查询所有字段

select *from testing where [name]like '%00%'

select *from testing wherecharindex('00',[name])>0

 

--查询字段不全部为索引

select id,bnamefrom testing where [name]like '%00%'

select id,bnamefrom testing wherecharindex('00',[name])>0

2.         没有 索引的情况下测试

         结论:

a)         在完全没有添加索引时 charindex函数的性能比like好

 

3.         为name字段添加索引测试:

 

 

结论:

a)         charindex函数的性能比like好

b)         检索的全部为索引项耗时要少的多,性能提升很高

 

4.         对单字段查询测试

 

 

结论:

a)         charindex函数的性能比like好

 

5.         对查询字段全部为索引项的测试

 

结论:

a)         charindex函数的性能比like好

 

6.         对表全字段查询测试

 

结论:

a)         charindex函数的性能比like略好

 

7.         查询同等列数一个字段为索引项一个不是索引项字段的测试

 

结论:

a)         charindex函数的性能比like略好

b)         bname不为索引项查询的速度近似于第一的测试。

 

 

总结:

a)         在模糊查询时用charindex函数比用like性能好

b)         查询的字段数量越多速度越慢,在查询时只查需要的字段

c)         查询字段中如果有字段不为索引项则查询的速度和无索引时差不多即使where条件查询字段是索引项

d)          ...也许你能从数据中再发现些什么欢迎补充

原创粉丝点击