这实际上是一个更大的复杂查询的一部分。
根据查询计划,此语句上的排序支配较大查询的成本。
通过具体化这部分查询,我验证它决定了成本。
select [sID], ROW_NUMBER() over (partition by [sID] order by [wordPos]) [rn], [wordPos], [wordID]
from [FTSindex]
where [wordID] in (428,2112)
order by [sID], [rn]
从右到左:
- 索引搜索 5% (IX_FTSindex_wordID_sID)
- 排序 76%
- 并行度 19%
CREATE TABLE [dbo].[FTSindex](
[sID] [int] NOT NULL,
[wordPos] [int] NOT NULL,
[wordID] [int] NOT NULL,
[charPos] [int] NOT NULL,
CONSTRAINT [PK_FTSindex] PRIMARY KEY CLUSTERED
(
[sID] ASC,
[wordPos] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 100) ON [PRIMARY]
) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IX_FTSindex_wordID_sID] ON [dbo].[FTSindex]
(
[wordID] ASC,
[sID] ASC,
[wordPos] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 100) ON [PRIMARY]
GO
鉴于 IX_FTSindex_wordID_sID 包括 [sID] 和 [wordPos] 我认为排序会非常快。
单独尝试了 [wordID] 和 [wordID]、[sID],但排序仍然是成本的 76%。
甚至这个查询
select [sID], [wordPos] -- , wordID
from [FTSindex]
where [wordID] in (428,2112)
order by [sID], [wordPos]
排序是排序是 76% 或成本。
我怎样才能降低排序成本?
PK必须保持原样。
我可以添加或修改其他索引。