当前位置:数据库 > SQLServer >>

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

环境:sql2005

数据量:300万

查询结果数据量:127221

机器环境

P4 3.0双核 1G内存

 

1.         执行语句

--无索引

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

select count(*) from testing where 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 where charindex('00',[name])>0

--查询所有字段

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

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

 

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

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

select id,bname from testing where charindex('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)          ...也许你能从数据中再发现些什么欢迎补充

like 如果使用 'x%'还是可以使用索引的,charindex和 like '%x'都不能,除非,索引覆盖扫描!

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,