sql语句中条件查询like的效率,及使用规范.

我在做数据库查询的时候,发现了一个奇怪的问题,在使用like关键字的时候,速度其慢,效率极低。不明白是什么原因,请大家讲解一下,为什么like慢,而其他的两个快。3个查询不同的地方用红色做了标识。
我数据表中的数据量如下FDAEnterprise 数据513条,TYHJL里面的数据为22516条。
我有如下一个查询语句:查询所花费的时间是13秒。
  select top 15  
FDAEnterprise.cName,FDAEnterprise.cEnterpriseCode,FDAEnterprise.cAddress,count(*) as NoteNum  
from TYHJL join FDAEnterprise on  
TYHJL.cEnterpriseCode=FDAEnterprise.cEnterpriseCode   
where  
FDAEnterprise.cName like '%大药房%' 
and  
yh_xq<='2050-01-01'  
and  
Yh_datetime > '1900-01-01'  
and  
Yh_datetime <'2050-01-01'
  group by  
FDAEnterprise.cName ,FDAEnterprise.cEnterpriseCode,FDAEnterprise.cAddress   
  order by  
FDAEnterprise.cEnterpriseCode


后来经过修改,改为如下语句,查询所花时间大概是0.25秒
select top 15  
FDAEnterprise.cName,FDAEnterprise.cEnterpriseCode,FDAEnterprise.cAddress,count(*) as NoteNum  
from TYHJL join FDAEnterprise on  
TYHJL.cEnterpriseCode=FDAEnterprise.cEnterpriseCode   
where
(select cName from FDAEnterprise where TYHJL.cEnterpriseCode=FDAEnterprise.cEnterpriseCode)like '%大药房%' 
and  
yh_xq<='2050-01-01'  
and  
Yh_datetime > '1900-01-01'  
and  
Yh_datetime <'2050-01-01'
group by  
FDAEnterprise.cName ,FDAEnterprise.cEnterpriseCode,FDAEnterprise.cAddress   
order by  
FDAEnterprise.cEnterpriseCode


最后我又进行了修改,改为如下的语句,查询耗时大概0.14秒
select top 15  
FDAEnterprise.cName,FDAEnterprise.cEnterpriseCode,FDAEnterprise.cAddress,count(*) as NoteNum  
from TYHJL join FDAEnterprise on  
TYHJL.cEnterpriseCode=FDAEnterprise.cEnterpriseCode   
where  
charindex('大药房',FDAEnterprise.cName)<>0 
and  
yh_xq<='2050-01-01'  
and  
Yh_datetime > '1900-01-01'  
and  
Yh_datetime <'2050-01-01'
group by
FDAEnterprise.cName ,FDAEnterprise.cEnterpriseCode,FDAEnterprise.cAddress   
order by  
FDAEnterprise.cEnterpriseCode

 

 

第一种like的内容如果前后都使用了%的话,是无法使用索引的,这应该是慢的主要原因
第二种先select了一次,相当于用hash索引的方式过滤了一遍,结果集小了很多
第三种使用了文本索引,所以速度比较快

在sql语句里,"like","not in","not exists"这几个是比较慢的,原因在于它们的实现机制。例如,如果我们在一个“字符串”里找查“另一个字符串”,而且这个字符串有通配符(%),不管是用顺序查找还是二分查找,效率都不见得太高。like比较符,如果用的是like "大药房%",这种方式会用索引扫描,但如果是 like "%大药房%",这种方式会用表扫描,该字段上的索引不起作用,速度会非常慢,如果你的记录很多,该字段很大的话。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值