LIKE'%...'如何寻求索引?

时间:2013-08-05 19:20:00

标签: sql sql-server-2012 wildcard sql-execution-plan sql-like

我希望这两个SELECT具有相同的执行计划和性能。由于LIKE上有一个前导通配符,我希望进行索引扫描。当我运行此计划并查看计划时,第一个SELECT的行为与预期一致(使用扫描)。但第二个SELECT计划显示索引搜索,并且运行速度提高了20倍。

代码:

-- Uses index scan, as expected:
SELECT 1
    FROM AccountAction
    WHERE AccountNumber LIKE '%441025586401'

-- Uses index seek somehow, and runs much faster:
declare @empty VARCHAR(30) = ''
SELECT 1
    FROM AccountAction
    WHERE AccountNumber LIKE '%441025586401' + @empty

问题:

当模式以通配符开头时,SQL Server如何使用索引查找?

奖金问题:

为什么连接空字符串会改变/改进执行计划?

详细说明:

  • Accounts.AccountNumber
  • 上有非聚集索引
  • 还有其他索引,但搜索和扫描都在索引上。
  • Accounts.AccountNumber列是可以为空的varchar(30)
  • 服务器是SQL Server 2012

表和索引定义:

CREATE TABLE [updatable].[AccountAction](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [AccountNumber] [varchar](30) NULL,
    [Utility] [varchar](9) NOT NULL,
    [SomeData1] [varchar](10) NOT NULL,
    [SomeData2] [varchar](200) NULL,
    [SomeData3] [money] NULL,
    --...
    [Created] [datetime] NULL,
 CONSTRAINT [PK_Account] PRIMARY KEY NONCLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]


CREATE NONCLUSTERED INDEX [IX_updatable_AccountAction_AccountNumber_UtilityCode_ActionTypeCd] ON [updatable].[AccountAction]
(
    [AccountNumber] ASC,
    [Utility] ASC
)
INCLUDE ([SomeData1], [SomeData2], [SomeData3]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]


CREATE CLUSTERED INDEX [CIX_Account] ON [updatable].[AccountAction]
(
    [Created] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

注意: 以下是两个查询的实际执行计划。对象的名称与上面的代码略有不同,因为我试图让问题变得简单。

The two execution plans.

1 个答案:

答案 0 :(得分:7)

这些测试(数据库AdventureWorks2008R2)显示了会发生什么:

SET NOCOUNT ON;
SET STATISTICS IO ON;

PRINT 'Test #1';
SELECT  p.BusinessEntityID, p.LastName
FROM    Person.Person p
WHERE   p.LastName LIKE '%be%';

PRINT 'Test #2';
DECLARE @Pattern NVARCHAR(50);
SET @Pattern=N'%be%';
SELECT  p.BusinessEntityID, p.LastName
FROM    Person.Person p
WHERE   p.LastName LIKE @Pattern;

SET STATISTICS IO OFF;
SET NOCOUNT OFF;

结果:

Test #1
Table 'Person'. Scan count 1, logical reads 106
Test #2
Table 'Person'. Scan count 1, logical reads 106

SET STATISTICS IO的结果显示LIO是相同。 但执行计划完全不同: enter image description here

在第一个测试中,SQL Server使用Index Scan显式,但在第二个测试中,SQL Server使用的Index SeekIndex Seek - range scan。在最后一种情况下,SQL Server使用Compute Scalar运算符生成这些值

[Expr1005] = Scalar Operator(LikeRangeStart([@Pattern])), 
[Expr1006] = Scalar Operator(LikeRangeEnd([@Pattern])), 
[Expr1007] = Scalar Operator(LikeRangeInfo([@Pattern]))

并且Index Seek运算符使用Seek Predicate(已优化)range scanLastName > LikeRangeStart AND LastName < LikeRangeEnd)加上另一个未经优化的PredicateLastName LIKE @pattern )。

  

LIKE'%...'如何寻找索引?

我的回答:这不是“真实的”Index Seek。它是Index Seek - range scan,在这种情况下,与Index Scan具有相同的效果。

另请参阅Index SeekIndex Scan之间的区别(类似辩论): So…is it a Seek or a Scan?

编辑1: OPTION(RECOMPILE)的执行计划(请参阅Aaron的建议)也显示Index Scan(而不是Index Seek): enter image description here