小编典典

为什么此SQL语句非常慢?

sql

我有一张约有100万条记录的表(运行SQL Server 2008
Web)。我有一个搜索例程,该例程试图匹配产品代码以及产品描述。但是在某些情况下,它非常慢。下面是(缩减的)sql语句:

WITH AllProducts AS (
  SELECT       p.*, Row_Number() OVER (ORDER BY ProductId) AS RowNumber
  FROM        Product AS p 
    WHERE p.IsEnabled=1 AND
    (
      p.BaseSku = 'KPK-3020QWC-C' -- this on its own is fast
      OR
      CONTAINS(p.FreeTextStrings, '"KPK-3020QWC*"') -- and this on its own is fast, but not both
    )
) SELECT * FROM AllProducts        
  WHERE RowNumber BETWEEN 1 AND 20;

请注意,如果我只是立即(但不是同时)比较[p.BaseSku
=’KPK-3020QWC-C’]或[CONTAINS(p.FreeTextStrings,’“ KPK-3020QWC
*”’))。如果我将它们进行比较,则需要花费一些时间(几分钟),并且仅返回一行。

对IsEnabled和BaseSku进行索引,对FreeTextStrings进行FTS索引。

我记得以前工作很好。

有人可以对此提出任何建议并提出一些解决方案吗?

执行计划文件位于此处:http
://wiki.webgear.co.nz/GetFile.aspx?File=Temp%5cSearch%
20Test.sqlplan.zip


阅读 222

收藏
2021-04-22

共1个答案

小编典典

or在SQL Server上非常慢。至少可以这样说,这使情况更加恶化。

尝试将其分为两个查询union

WITH AllProducts AS (
  select *, Row_Number() OVER (ORDER BY ProductId) AS RowNumber
  from (
  SELECT       p.*
  FROM        Product AS p 
    WHERE p.IsEnabled=1 AND
      p.BaseSku = 'KPK-3020QWC-C' 
  UNION
  SELECT       p.*
  FROM        Product AS p 
    WHERE p.IsEnabled=1 AND
      CONTAINS(p.FreeTextStrings, '"KPK-3020QWC*"')
  )
) SELECT * FROM AllProducts        
  WHERE RowNumber BETWEEN 1 AND 20;
2021-04-22