在SQL Server的SQL优化过程中,如果遇到WHERE条件中包含LIKE '%search_string%'是一件非常头痛的事情。这种情况下,一般要修改业务逻辑或改写SQL才能解决SQL执行计划走索引扫描或全表扫描的问题。最近在优化SQL语句的时候,遇到了一个很有意思的问题。某些使用LIKE '%' + @search_string + '%'(或者 LIKE @search_string)这样写法的SQL语句的执行计划居然走索引查找(Index Seek)。下面这篇文章来分析一下这个奇怪的现象。

首先,我们来看看WHERE查询条件中使用LIKE的几种情况,这些是我们对LIKE的一些常规认识:

1: LIKE 'condition%'

执行计划会走索引查找(Index Seek or Clustered Index Seek)。

2:  LIKE '%condition'

执行计划会走索引扫描(Index Scan or Clustered Index Scan)或全表扫描(Table Scan)

3:  LIKE '%condition%'

执行计划会走索引扫描(Index Scan or Clustered Index Scan)或全表扫描(Table Scan)

4: LIKE 'condition1%condition%';

执行计划会走索引查找(Index Seek)

下面我们以AdventureWorks2014示例数据库为测试环境(测试环境为SQL Server 2014 SP2),测试上面四种情况,如下所示:

其实复杂的情况下,LIKE 'search_string%'也有走索引扫描(Index Scan)的情况,上面情况并不是唯一、绝对的。如下所示

在表Person.Person的 rowguid字段上创建有唯一索引AK_Person_rowguid

那么我们来看看上面所说的这个特殊案例(这里使用一个现成的案例,懒得构造案例了),如何让LIKE %search_string%走索引查找(Index Seek),这个技巧就是使用变量,如下SQL对比所示:

如下所示,表[dbo].[GEN_CUSTOMER]在字段CUSTOMER_CD有聚集索引。

可以看到CUSTOMER_CD LIKE '%' + @CUSTOMER_CD + '%'这样的SQL写法(或者CUSTOMER_CD LIKE @CUSTOMER_CD也可以), 执行计划就走聚集索引查找(Clustered Index Seek)了, 而条件中直接使用CUSTOMER_CD LIKE '%00630%' 反而走聚集索引扫描(Clustered Index Scan),另外可以看到实际执行的Cost开销比为4% VS 96% ,初一看,还真的以为第一个执行计划比第二个执行的代价要小很多。但是从IO开销,以及CPU time、elapsed time对比来看,两者几乎没有什么差异。在这个案例中,并不是走索引查找(Index Seek)就真的开销代价小很多。

考虑到这里数据量较小,我使用网上的一个脚本,在AdventureWorks2014数据库构造了一个10000000的大表,然后顺便做了一些测试对比

CREATE TABLE dbo.TestLIKESearches

(

     ID1         INT

    ,ID2         INT

    ,AString     VARCHAR(100)

    ,Value       INT

    ,PRIMARY KEY (ID1, ID2)

);

 

WITH Tally (n) AS

(

SELECT TOP 10000000 ROW_NUMBER() OVER (ORDER BY (SELECT NULL))

FROM sys.all_columns a CROSS JOIN sys.all_columns b

)

INSERT INTO dbo.TestLIKESearches

    (ID1, ID2, AString, Value)

SELECT 1+n/500, n%500

    ,CASE WHEN n%500 > 299 THEN

            SUBSTRING('abcdefghijklmnopqrstuvwxyz', 1+ABS(CHECKSUM(NEWID()))%26, 1) +

            SUBSTRING('abcdefghijklmnopqrstuvwxyz', 1+ABS(CHECKSUM(NEWID()))%26, 1) +

            SUBSTRING('abcdefghijklmnopqrstuvwxyz', 1+ABS(CHECKSUM(NEWID()))%26, 1) +

            RIGHT(1000+n%1000, 3) +

            SUBSTRING('abcdefghijklmnopqrstuvwxyz', 1+ABS(CHECKSUM(NEWID()))%26, 1) +

            SUBSTRING('abcdefghijklmnopqrstuvwxyz', 1+ABS(CHECKSUM(NEWID()))%26, 1) +

            SUBSTRING('abcdefghijklmnopqrstuvwxyz', 1+ABS(CHECKSUM(NEWID()))%26, 1)

          END

    ,1+ABS(CHECKSUM(NEWID()))%100

FROM Tally;

 

 

CREATE INDEX IX_TestLIKESearches_N1 ON dbo.TestLIKESearches(AString);

如下测试所示,在一个大表上面,LIKE @search_string这种SQL写法,IO开销确实要小一些,CPU Time也要小一些。个人多次测试都是这种结果。也就是说对于数据量较大的表,这种SQL写法性能确实要好一些。

现在回到最开始那个SQL语句,个人对执行计划有些疑惑,查看执行计划,你会看到优化器对CUSTOMER_CD LIKE '%' + @CUSTOMER_CD + '%' 进行了转换。如下截图或通过执行计划的XML,你会发现上面转换为使用三个内部函数LikeRangeStart, LikeRangeEnd,  LikeRangeInfo.

<OutputList>

                    <ColumnReference Column="Expr1007" />

                    <ColumnReference Column="Expr1008" />

                    <ColumnReference Column="Expr1009" />

                  </OutputList>

                  <ComputeScalar>

                    <DefinedValues>

                      <DefinedValue>

                        <ColumnReference Column="Expr1007" />

                        <ScalarOperator ScalarString="LikeRangeStart((N'%'+[@CUSTOMER_CD])+N'%')">

                          <Identifier>

                            <ColumnReference Column="ConstExpr1004">

                              <ScalarOperator>

                                <Intrinsic FunctionName="LikeRangeStart">

                                  <ScalarOperator>

                                    <Arithmetic Operation="ADD">

                                      <ScalarOperator>

                                        <Arithmetic Operation="ADD">

                                          <ScalarOperator>

                                            <Const ConstValue="N'%'" />

                                          </ScalarOperator>

                                          <ScalarOperator>

                                            <Identifier>

                                              <ColumnReference Column="@CUSTOMER_CD" />

                                            </Identifier>

                                          </ScalarOperator>

                                        </Arithmetic>

                                      </ScalarOperator>

                                      <ScalarOperator>

                                        <Const ConstValue="N'%'" />

                                      </ScalarOperator>

                                    </Arithmetic>

                                  </ScalarOperator>

                                  <ScalarOperator>

                                    <Const ConstValue="" />

                                  </ScalarOperator>

                                </Intrinsic>

                              </ScalarOperator>

                            </ColumnReference>

                          </Identifier>

                        </ScalarOperator>

                      </DefinedValue>

                      <DefinedValue>

                        <ColumnReference Column="Expr1008" />

                        <ScalarOperator ScalarString="LikeRangeEnd((N'%'+[@CUSTOMER_CD])+N'%')">

                          <Identifier>

                            <ColumnReference Column="ConstExpr1005">

                              <ScalarOperator>

                                <Intrinsic FunctionName="LikeRangeEnd">

                                  <ScalarOperator>

                                    <Arithmetic Operation="ADD">

                                      <ScalarOperator>

                                        <Arithmetic Operation="ADD">

                                          <ScalarOperator>

                                            <Const ConstValue="N'%'" />

                                          </ScalarOperator>

                                          <ScalarOperator>

                                            <Identifier>

                                              <ColumnReference Column="@CUSTOMER_CD" />

                                            </Identifier>

                                          </ScalarOperator>

                                        </Arithmetic>

                                      </ScalarOperator>

                                      <ScalarOperator>

                                        <Const ConstValue="N'%'" />

                                      </ScalarOperator>

                                    </Arithmetic>

                                  </ScalarOperator>

                                  <ScalarOperator>

                                    <Const ConstValue="" />

                                  </ScalarOperator>

                                </Intrinsic>

                              </ScalarOperator>

                            </ColumnReference>

                          </Identifier>

                        </ScalarOperator>

                      </DefinedValue>

                      <DefinedValue>

                        <ColumnReference Column="Expr1009" />

                        <ScalarOperator ScalarString="LikeRangeInfo((N'%'+[@CUSTOMER_CD])+N'%')">

                          <Identifier>

                            <ColumnReference Column="ConstExpr1006">

                              <ScalarOperator>

                                <Intrinsic FunctionName="LikeRangeInfo">

                                  <ScalarOperator>

                                    <Arithmetic Operation="ADD">

                                      <ScalarOperator>

                                        <Arithmetic Operation="ADD">

                                          <ScalarOperator>

                                            <Const ConstValue="N'%'" />

                                          </ScalarOperator>

                                          <ScalarOperator>

                                            <Identifier>

                                              <ColumnReference Column="@CUSTOMER_CD" />

                                            </Identifier>

                                          </ScalarOperator>

                                        </Arithmetic>

                                      </ScalarOperator>

                                      <ScalarOperator>

                                        <Const ConstValue="N'%'" />

                                      </ScalarOperator>

                                    </Arithmetic>

                                  </ScalarOperator>

                                  <ScalarOperator>

                                    <Const ConstValue="" />

                                  </ScalarOperator>

                                </Intrinsic>

                              </ScalarOperator>

                            </ColumnReference>

                          </Identifier>

                        </ScalarOperator>

                      </DefinedValue>

                    </DefinedValues>

另外,你会发现Nested Loops & Compute Scalar 等步骤的Cost都为0.后面在“Dynamic Seeks and Hidden Implicit Conversions”这篇博客里面看到了一个新名词Dynamic Seeks。文字提到因为成本估算为0,所以,你看到的执行计划的Cost又是“不准确”的,具体描述如下:

The plan now contains an extra Constant Scan,  a Compute Scalar and a Nested Loops Join.  These operators are interesting because they have zero cost estimates: no CPU, no I/O, nothing.  That’s because they are purely architectural: a workaround for the fact that SQL Server cannot currently perform a dynamic seek within the Index Seek operator itself.  To avoid affecting plan choices, this extra machinery is costed at zero.

The Constant Scan produces a single in-memory row with no columns.  The Compute Scalar defines expressions to describe the covering seek range (using the runtime value of the @Like variable).  Finally, the Nested Loops Join drives the seek using the computed range information as correlated values.

The upper tooltip shows that the Compute Scalar uses three internal functions, LikeRangeStart, LikeRangeEnd, and LikeRangeInfo.  The first two functions describe the range as an open interval.  The third function returns a set of flags encoded in an integer, that are used internally to define certain seek properties for the Storage Engine.  The lower tooltip shows the seek on the open interval described by the result of LikeRangeStart and LikeRangeEnd, and the application of the residual predicate ‘LIKE @Like’.

不管你返回的记录有多少,执行计划Nested Loops & Compute Scalar 等步骤的Cost都为0,如下测试所示,返回1000条记录,它的成本估算依然为0 ,显然这样是不够精确的。深层次的原因就不太清楚了。执行计划Cost不可靠的案例很多。

SET STATISTICS IO ON;

 

SET STATISTICS TIME ON;

 

DECLARE @CUSTOMER_CD NVARCHAR(10);

 

SET @CUSTOMER_CD=N'%44%'

 

 

 

SELECT * FROM  [dbo].[GEN_CUSTOMER] WHERE CUSTOMER_CD LIKE @CUSTOMER_CD

另外,其实还一点没有搞清楚的时候在什么条件下出现Index Seek的情况。有些情况下,使用变量的方式,依然是索引扫描

不过我在测试过程,发现有一个原因是书签查找(Bookmark Lookup:键查找(Key Lookup)或RID查找 (RID Lookup))开销过大会导致索引扫描。如下测试对比所示:

CREATE NONCLUSTERED INDEX [IX_xriteWhite_N1] ON.[dbo].[xriteWhite] ([Item_NO]) INCLUDE ([Iden],[WI_CE],[CIE],[Operate_Time])

参考资料:

http://sqlblog.com/blogs/paul_white/archive/2012/01/18/dynamic-seeks-and-hidden-implicit-conversions.aspx

https://blogs.msdn.microsoft.com/varund/2009/11/30/index-usage-by-like-operator-query-tuning/

https://sqlperformance.com/2017/02/sql-indexes/seek-leading-wildcard-sql-server

https://stackoverflow.com/questions/1388059/sql-server-index-columns-used-in-like

SQL Server中LIKE %search_string% 走索引查找(Index Seek)浅析的更多相关文章

  1. SQL Server中的聚集索引(clustered index) 和 非聚集索引 (non-clustered index)

    本文转载自  http://blog.csdn.net/ak913/article/details/8026743 面试时经常问到的问题: 1. 什么是聚合索引(clustered index) / ...

  2. SQL SERVER中什么情况会导致索引查找变成索引扫描

    SQL Server 中什么情况会导致其执行计划从索引查找(Index Seek)变成索引扫描(Index Scan)呢? 下面从几个方面结合上下文具体场景做了下测试.总结.归纳. 1:隐式转换会导致 ...

  3. Sql Server中的表访问方式Table Scan, Index Scan, Index Seek

    1.oracle中的表访问方式 在oracle中有表访问方式的说法,访问表中的数据主要通过三种方式进行访问: 全表扫描(full table scan),直接访问数据页,查找满足条件的数据 通过row ...

  4. 转:Sql Server中的表访问方式Table Scan, Index Scan, Index Seek

    0.参考文献 Table Scan, Index Scan, Index Seek SQL SERVER – Index Seek vs. Index Scan – Diffefence and Us ...

  5. sql server中index的REBUILD和REORGANIZE的区别及工作方式

    sql server中index的REBUILD和REORGANIZE 转自:https://www.cnblogs.com/flysun0311/archive/2013/12/05/3459451 ...

  6. c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程

    c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...

  7. 第十七周翻译-SQL Server中事务日志管理的阶梯,级别5:以完全恢复模式管理日志

    SQL Server中事务日志管理的阶梯,级别5:以完全恢复模式管理日志 作者:Tony Davis,2012/01/27 翻译:赖慧芳 译文: 该系列   本文是Stairway系列的一部分:SQL ...

  8. SQL Server中SCAN 和SEEK的区别

    SQL Server中SCAN 和SEEK的区别 SQL SERVER使用扫描(scan)和查找(seek)这两种算法从数据表和索引中读取数据.这两种算法构成了查询的基础,几乎无处不在.Scan会扫描 ...

  9. SQL Server中的执行引擎入门

      简介 当查询优化器(Query Optimizer)将T-SQL语句解析后并从执行计划中选择最低消耗的执行计划后,具体的执行就会交由执行引擎(Execution Engine)来进行执行.本文旨在 ...

随机推荐

  1. php设计模式2

    代理模式 <?php /** * 代理模式:为其他对象提供一个代理以控制这个对象的访问 它是给某一个对象提供一个替代者,使之在client对象和subject对象之间编码更有效率. 代理可以提供 ...

  2. 《关于长沙.NET技术社区未来发展规划》问卷调查结果公布

    那些开发者们对于社区的美好期待 2月,长沙.net 技术社区自从把群拉起来开始,做了一次比较正式.题目为<关于长沙.NET技术社区未来发展规划>的问卷调查,在问卷调查中,溪源写道: 随着互 ...

  3. 机器学习入门16 - 多类别神经网络 (Multi-Class Neural Networks)

    原文链接:https://developers.google.com/machine-learning/crash-course/multi-class-neural-networks/ 多类别分类, ...

  4. spring boot - 整合jpa多对对关系保存和查询示例

    pojo: package com.example.zs.springDataJpa; import org.hibernate.annotations.Proxy; import javax.per ...

  5. 一个老程序员是如何手写Spring MVC的

    人见人爱的Spring已然不仅仅只是一个框架了.如今,Spring已然成为了一个生态.但深入了解Spring的却寥寥无几.这里,我带大家一起来看看,我是如何手写Spring的.我将结合对Spring十 ...

  6. Python内置函数(43)——min

    英文文档: min(iterable, *[, key, default]) min(arg1, arg2, *args[, key]) Return the smallest item in an ...

  7. 【Docker】(4)搭建私有镜像仓库

    [Docker](4)搭建私有镜像仓库 说明 1. 这里是通过阿里云,搭建Docker私有镜像仓库. 2. 这里打包的镜像是从官网拉下来的,并不是自己项目创建的新镜像,主要测试功能 一.搭建过程 首先 ...

  8. mac用户丢失管理员身份急救

    用了这么久mac,发现居然还存在这么大一个bug.就是如果你曾经编辑mac电脑默认用户资料,比如试图改用户名.试图改HOME路径之类的,有很高比率可能导致该用户丧失管理员权限. 随后悲剧就开始了,所有 ...

  9. [一] java8 函数式编程入门 什么是函数式编程 函数接口概念 流和收集器基本概念

      本文是针对于java8引入函数式编程概念以及stream流相关的一些简单介绍 什么是函数式编程?   java程序员第一反应可能会理解成类的成员方法一类的东西 此处并不是这个含义,更接近是数学上的 ...

  10. Django学习笔记(6)——Form表单

    知识储备:HTML表单form学习 表单,在前端页面中属于最常见的一个东西了.基本上网站信息的提交都用到了表单,所以下面来学习Django中优雅的表单系统:Form 表单的主要作用是在网页上提供一个图 ...