我尝试优化以下查询,该查询返回与搜索词组匹配的产品:
WITH [RankedQuery] AS
(
SELECT FTS.RANK, ROW_NUMBER() OVER (ORDER BY [FTS].RANK DESC) AS [RowNumber], [Product].*
FROM [Product]
INNER JOIN FREETEXTTABLE([Product],
(
[Name],
[Brand],
[Category],
[Description]
), 'man shoes') AS [FTS]
ON FTS.[Key] = [Product].[Id]
)
SELECT * FROM [RankedQuery]
WHERE [RowNumber] BETWEEN 100 AND 150
我在执行计划中发现它花费大部分时间按RANK
对结果进行排序 - 这是有道理的,因为它必须通过许多非必要的行。
我尝试通过在排序前按排名过滤产品来优化它,但结果很差。
查询:
WITH [RankedQuery] AS
(
SELECT MaxRelevance, FTS.RANK, ROW_NUMBER() OVER (ORDER BY [FTS].RANK DESC) AS [RowNumber], [Product].*
FROM [Product]
INNER JOIN
(
select * from
(
select FTS_stage_1.*, max(FTS_stage_1.rank) over() as MaxRelevance
from FREETEXTTABLE
([Product],
(
[Name],
[Brand],
[Category],
[Description]
), 'man shoes'
)
as FTS_stage_1
) AS [FTS_with_MaxRelevance]
where [FTS_with_MaxRelevance].Rank > [FTS_with_MaxRelevance].MaxRelevance * 0.3
) AS [FTS]
ON FTS.[Key] = [Product].[Id]
)
SELECT * FROM [RankedQuery]
WHERE [RowNumber] BETWEEN 100 AND 150
造成了这种怪异,并且执行时间延长了几倍。
有没有办法让它更有效率?