SQL命令运行缓慢。需要帮助识别缓慢

时间:2014-11-06 16:45:59

标签: sql sql-server database query-performance

我有一个SQL查询需要花费数小时才能运行。基本上,我希望在我们的“页面”表中找到具有特殊字符的文档(PDF)。我在PDFFile表中找到了这些PDF。仅执行第一个AND子句将在16秒内返回。添加第二个子句使SQL需要3个小时。试图弄清楚我做错了什么。非常感谢任何帮助。

查询:

select b.bookletname, b.trackingID, b.[version], s.name
from page p
inner join section s on s.id = p.sectionid
inner join booklet b on b.id = s.bookletid
INNER JOIN [user] u ON b.CreatedBy = u.id
INNER JOIN client c ON c.id = u.clientID
WHERE u.clientID = 2
AND p.[filename] IN (
    SELECT DISTINCT pdf.[FileName]
    FROM PDFFile pdf
    WHERE pdf.fileName LIKE '%\<%' ESCAPE '\'
    OR pdf.fileName LIKE '%\>%' ESCAPE '\'
    OR pdf.fileName LIKE '%\"%' ESCAPE '\'
    OR pdf.fileName LIKE '%\%%' ESCAPE '\'
    OR pdf.fileName LIKE '%''%' ESCAPE '\'
    OR pdf.fileName LIKE '%\*%' ESCAPE '\'
    OR pdf.fileName LIKE '%\+%' ESCAPE '\'
    OR pdf.fileName LIKE '%\\%' ESCAPE '\'
    OR pdf.fileName LIKE '%\/%' ESCAPE '\'
    OR pdf.fileName LIKE '%\:%' ESCAPE '\'
    OR pdf.fileName LIKE '%\?%' ESCAPE '\'
    OR pdf.fileName LIKE '%\[%' ESCAPE '\'
    OR pdf.fileName LIKE '%\]%' ESCAPE '\'
    OR pdf.fileName LIKE '%\|%' ESCAPE '\'
)
OR p.[PDF_File_Name] IN (
    SELECT DISTINCT pdf.[FileName]
    FROM PDFFile pdf
    WHERE pdf.fileName LIKE '%\<%' ESCAPE '\'
    OR pdf.fileName LIKE '%\>%' ESCAPE '\'
    OR pdf.fileName LIKE '%\"%' ESCAPE '\'
    OR pdf.fileName LIKE '%\%%' ESCAPE '\'
    OR pdf.fileName LIKE '%''%' ESCAPE '\'
    OR pdf.fileName LIKE '%\*%' ESCAPE '\'
    OR pdf.fileName LIKE '%\+%' ESCAPE '\'
    OR pdf.fileName LIKE '%\\%' ESCAPE '\'
    OR pdf.fileName LIKE '%\/%' ESCAPE '\'
    OR pdf.fileName LIKE '%\:%' ESCAPE '\'
    OR pdf.fileName LIKE '%\?%' ESCAPE '\'
    OR pdf.fileName LIKE '%\[%' ESCAPE '\'
    OR pdf.fileName LIKE '%\]%' ESCAPE '\'
    OR pdf.fileName LIKE '%\|%' ESCAPE '\'
)
OR p.[PDFName] IN (
    SELECT DISTINCT pdf.[FileName]
    FROM PDFFile pdf
    WHERE pdf.fileName LIKE '%\<%' ESCAPE '\'
    OR pdf.fileName LIKE '%\>%' ESCAPE '\'
    OR pdf.fileName LIKE '%\"%' ESCAPE '\'
    OR pdf.fileName LIKE '%\%%' ESCAPE '\'
    OR pdf.fileName LIKE '%''%' ESCAPE '\'
    OR pdf.fileName LIKE '%\*%' ESCAPE '\'
    OR pdf.fileName LIKE '%\+%' ESCAPE '\'
    OR pdf.fileName LIKE '%\\%' ESCAPE '\'
    OR pdf.fileName LIKE '%\/%' ESCAPE '\'
    OR pdf.fileName LIKE '%\:%' ESCAPE '\'
    OR pdf.fileName LIKE '%\?%' ESCAPE '\'
    OR pdf.fileName LIKE '%\[%' ESCAPE '\'
    OR pdf.fileName LIKE '%\]%' ESCAPE '\'
    OR pdf.fileName LIKE '%\|%' ESCAPE '\'
)

2 个答案:

答案 0 :(得分:2)

尝试删除三个表命中并将该选择放入临时表,因为您使用相同的选项来检查三列。所以你可以在条件的地方使用临时表。

SELECT DISTINCT pdf.[FileName]
INTO   #temp
FROM   PDFFile pdf
WHERE  pdf.fileName LIKE '%\<%' ESCAPE '\'
        OR pdf.fileName LIKE '%\>%' ESCAPE '\'
        OR pdf.fileName LIKE '%\"%' ESCAPE '\'
        OR pdf.fileName LIKE '%\%%' ESCAPE '\'
        OR pdf.fileName LIKE '%''%' ESCAPE '\'
        OR pdf.fileName LIKE '%\*%' ESCAPE '\'
        OR pdf.fileName LIKE '%\+%' ESCAPE '\'
        OR pdf.fileName LIKE '%\\%' ESCAPE '\'
        OR pdf.fileName LIKE '%\/%' ESCAPE '\'
        OR pdf.fileName LIKE '%\:%' ESCAPE '\'
        OR pdf.fileName LIKE '%\?%' ESCAPE '\'
        OR pdf.fileName LIKE '%\[%' ESCAPE '\'
        OR pdf.fileName LIKE '%\]%' ESCAPE '\'
        OR pdf.fileName LIKE '%\|%' ESCAPE '\' 

SELECT b.bookletname,
       b.trackingID,
       b.[version],
       s.NAME
FROM   page p
       INNER JOIN section s
               ON s.id = p.sectionid
       INNER JOIN booklet b
               ON b.id = s.bookletid
       INNER JOIN [user] u
               ON b.CreatedBy = u.id
       INNER JOIN client c
               ON c.id = u.clientID
WHERE  u.clientID = 2
       AND p.[filename] IN (SELECT *
                            FROM   #temp)
        OR p.[PDF_File_Name] IN (SELECT *
                                 FROM   #temp)
        OR p.[PDFName] IN (SELECT *
                           FROM   #temp) 

答案 1 :(得分:2)

使用exists重写此内容并简化like。 SQL Server允许您在模式中包含字符列表,并且您不需要转义的大部分字符(我希望我得到的结果模式完全正确)。

我建议:

select b.bookletname, b.trackingID, b.[version], s.name
from page p inner join
     section s
     on s.id = p.sectionid inner join
     booklet b
     on b.id = s.bookletid inner join
     [user] u
     on b.CreatedBy = u.id inner join
     client c 
     on c.id = u.clientID
where u.clientID = 2 and
      (exists (select 1
               from PDFFile pdf
               where pdf.fileName LIKE '%[<>"''*+\\/:?\[\]|]%' ESCAPE '\' and
                     pdf.fileName = p.filename
              ) or
       exists (select 1
               from PDFFile pdf
               where pdf.fileName LIKE '%[<>"''*+\\/:?\[\]|]%' ESCAPE '\' and
                     pdf.fileName = p.PDF_File_Name
              ) or
       exists (select 1
               from PDFFile pdf
               where pdf.fileName LIKE '%[<>"''*+\\/:?\[\]|]%' ESCAPE '\' and
                     pdf.fileName = p.PDFName
              )
      );

您希望确保在PDFFile(fileName)上有关于效果的索引。

我也改变了逻辑。您的原始逻辑类似where A and B or C or D,其解析为where (A and B) or C or C。我将其更改为where A and (B or C or D)。这也可能是性能瓶颈所致。