我正在使用Lucene索引CMS的内容,所以我扩展了SQL Server数据库模式以添加一个“IsIndexed”位列,所以Lucene索引器可以找到尚未编入索引的内容。SQL Server - 查询执行索引扫描而不是查询
我在Content
表中添加了一个索引,因此IsIndexed
列的查找速度应该更快。这是数据库的样子:
CREATE TABLE Content (
DocumentId bigint,
CategoryId bigint,
Title nvarchar(255),
AuthorUserId bigint,
Body nvarchar(MAX),
IsIndexed bit
)
CREATE TABLE Users (
UserId bigint,
UserName nvarchar(20)
)
以下指标存在:
Content (
PK_Content (Clustered) : DocumentId ASC
IX_CategoryId (Non-Unique, Non-Clustered) : CategoryId ASC
IX_AuthorUserId (Non-Unique, Non-Clustered) : AuthorUserId ASC
IX_Indexed_ASC (Non-Unique, Non-Clustered) : IsIndexed ASC, DocumentId ASC
IX_Indexed_DESC (Non-Unique, Non-Clustered) : IsIndexed DESC, DocumentId ASC
)
Users (
PK_Users (Clustered) : UserId
)
这是用来查找非索引内容查询:当我运行它的
SELECT
TOP 1
Content.DocumentId,
Content.CategoryId,
Content.Title,
Content.AuthorUserId,
Content.Body
Users.UserName
FROM
Content
INNER JOIN Users ON Content.AuthorUserId = Users.UserId
WHERE
IsIndexed = 0
然而实际执行计划报告PK_Content的聚集索引扫描与PK_Users的聚集索引搜索相结合。查询需要大约300ms才能执行。
当我修改查询以删除Users.UserName字段和Users内部连接时,查询大约需要60ms才能运行,并且没有用于PK_Content的集群索引扫描,只有集群索引寻找PK_Content。
我在为Content.IsIndexed
列添加降序索引之前和之后尝试过,我还将Content.DocumentId
添加到IX_Indexed索引,但它没有区别。
我在做什么错?我做了所有必要的索引(然后是一些)。内容表有成千上万的行,类似于Users表,所以我不明白为什么优化器会选择扫描。
是否有任何索引?如果所有行(或大多数行)的IsIndexed等于0,则索引不太可能被使用 –
大约30%的所有内容行都有IsIndexed = 1 – Dai