使用XML时的右索引扫描

时间:2015-06-09 09:40:38

标签: sql-server tsql

以下是应用程序生成的T-SQL

DECLARE @xml_0 XML
SET @xml_0 = N'<Val>8e4cd3e3-de98-4f55-9c55-57881157a0f0</Val>
               <Val>2f483275-7333-4786-aca8-454e5bf4823f</Val>
               <Val>ce1ce763-1f68-48ec-bedf-f4641e40d8f8</Val>
               <Val>6b471d5e-fd5c-4db8-aa31-abb910651e18</Val>
               <Val>89064e42-0592-4845-b21e-38f788ab0d2e</Val>
               <Val>d54793f0-cbfb-428e-ba08-db70cab1af07</Val>
               <Val>8027e6bd-09e5-4a5b-aae7-54aff4a0e6c0</Val>
               <Val>53f1a5e3-b2a8-49c3-935b-a5ac7fe0c1d8</Val>
               <Val>faceabad-1d0c-4f3f-8d94-674bbf1c3428</Val>
               <Val>f8e0a43d-cff7-45aa-b73f-6858b1d17cd1</Val>
               <Val>94e9bc76-5bb3-4cf9-9b59-fc3163c904d7</Val>
               <Val>e4be8c69-5166-40cc-b49a-18adec78e356</Val>
               <Val>5c564b82-64e1-46c5-a41d-bc30104f14a5</Val>
               <Val>dc246c2c-7edd-407a-b378-747789bd5a75</Val>
               <Val>411ac1e9-3d4f-447c-808a-b82d388816dd</Val>'

SELECT COUNT(*) FROM 
(
    SELECT [t0].[ID]
    FROM [dbo].[HM_Rows] AS [t0], [dbo].[HM_Cells] AS [t1]
    WHERE 
    (
        [t1].[Value] IN 
        (
            SELECT node.value('.', 'NVARCHAR(200)') FROM @xml_0.nodes('/Val') xml_0(node)
        )
    )
) AS [r2487772634]

这里是T-SQL的执行计划 Exec Plan

所以它扫描索引 它扫描正确的索引

missing_index_FOR_Value_INC_RowID - 在HM_Cells表上 和 PK_HM_Rows - 在HM_Rows表

任何想法?

P.S表很大 行数 HM_Rows - 17&#39;&#39; 181 HM_Cells - 1&#39; 048&#39; 693&#39; 775 和是我已经重建了索引和更新的统计数据 HM_Cells.Value是NVarChar(200)

也没有XML和HM_Rows表它运行正常 例如     SELECT ID FROM HM_Cells WHERE值IN(.........) 效果很好

非常感谢:)

1 个答案:

答案 0 :(得分:0)

尝试使用JOIN而不是IN,因为这样可以强制执行循环策略,这可能会使用搜索而不是扫描:

SELECT COUNT(*) FROM 
(
    SELECT [t0].[ID]
    FROM (
        SELECT DISTINCT node.value('.', 'NVARCHAR(200)') AS Val 
        FROM @xml_0.nodes('/Val') xml_0(node)
    ) q1 INNER LOOP JOIN [dbo].[HM_Cells] AS [t1] ON q1.Val=t1.Value
    CROSS JOIN [dbo].[HM_Rows] AS [t0]
) AS [r2487772634]