简体   繁体   English

SQL Server 2008 LIKE性能

[英]sql server 2008 LIKE performance

I've noticed that the first sql statement with LIKE on a large table runs very slowly (around 20 minutes) but every subsequent one very very fast (a few seconds), even if the strings searched for are completely different from the initial one (the first string 'ability%', the second 'su_mit%') 我注意到,在大表上使用LIKE的第一个sql语句运行非常缓慢(大约20分钟),但随后的每个sql语句却运行非常快(几秒钟),即使搜索的字符串与初始字符串完全不同(第一个字符串'ability%',第二个字符串'su_mit%')

Does sql server store the results of table scan for the "like statement" in a cache? sql server是否在高速缓存中存储表扫描的结果以查找“ like语句”? Is the possible cache accessible to all clients? 所有客户端都可以访问可能的缓存吗?
Does the "cache" somehow expire? “缓存”会以某种方式过期吗?
if a full text index is available for the same column where the LIKE was applied and populated, does the "cache" influences full text speed? 如果全文索引可用于应用和填充LIKE的同一列,那么“缓存”会影响全文速度吗?
Does anybody know a document explaining the issue? 有人知道解释这个问题的文件吗?
Cheers 干杯
Greg 格雷格

What you're experiencing is most likely the result of part (or all) of the table being in the buffer cache (data pages cached in memory) after the first scan. 您遇到的情况很可能是表的部分(或全部)第一次扫描后进入缓冲区缓存(数据页缓存在内存中)的结果。 Depending on how frequently you hit the same table with scans, available memory, and other tables being cached SQL Server may keep your table's pages in the buffer cache, or may bump it for the benefit of someone else. 根据您扫描扫描同一表的频率,可用内存以及要缓存的其他表的频率,SQL Server可能会将表的页面保留在缓冲区缓存中,或者为了其他人的利益而破坏它。

For queries with no leading wild card (as in your examples above) you could reduce it to an index scan if the column you're querying is indexed with a normal index... 对于没有前导通配符的查询(如上述示例),如果您要查询的列已使用普通索引编制索引,则可以将其简化为索引扫描...

Full text indexes are not used by the LIKE operator, if you have a fulltext index on the column it is probably a good idea to change from a LIKE query to a fulltext query... See http://msdn.microsoft.com/en-us/library/ms142559.aspx LIKE运算符不使用全文索引,如果列上有全文索引,则从LIKE查询更改为全文查询可能是一个好主意...请参阅http://msdn.microsoft.com/ zh-CN / library / ms142559.aspx

SQL Server basically works like a virtual memory system, so the performance increase you see is simply more pages being in memory when it does the second table scan. SQL Server基本上像一个虚拟内存系统一样工作,因此您看到的性能提高只是在进行第二次表扫描时更多的页面在内存中。

I am not a SQL server expert so perhaps someone else can add further details to this, but when you use LIKE you are basically removing the databases ability to use any indexes. 我不是SQL Server专家,所以也许其他人可以为此添加更多详细信息,但是当您使用LIKE时,基本上是在删除数据库使用任何索引的能力。 If you want to use the full text index you need to use one of the specific full text index query commands FREETEXT, FREETEXTTABLE, CONTAINS and CONTAINSTABLE. 如果要使用全文索引,则需要使用特定的全文索引查询命令之一FREETEXT,FREETEXTTABLE,CONTAINS和CONTAINSTABLE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM