我们有一个表格,其中包含所有准备发送且已发送的电子邮件。该表包含超过 100 万行。
下面是查找仍然需要发送的消息的查询。5 次错误后,该消息不再尝试,需要手动修复。SentDate
一直null
到消息发送为止。
SELECT TOP (15)
ID,
FromEmailAddress,
FromEmailDisplayName,
ReplyToEmailAddress,
ToEmailAddresses,
CCEmailAddresses,
BCCEmailAddresses,
[Subject],
Body,
AttachmentUrl
FROM sysEmailMessage
WHERE ErrorCount < 5
AND SentDate IS NULL
ORDER BY CreatedDate
查询很慢,我认为是由于缺少索引。我已将查询提供给数据库引擎优化顾问。它建议了以下索引(以及一些我通常忽略的统计数据):
SET ANSI_PADDING ON
CREATE NONCLUSTERED INDEX [_dta_index_sysEmailMessage_7_1703677117__K14_K1_K12_5_6_7_8_9_10_11_15_17_18] ON [dbo].[sysEmailMessage]
(
[SentDate] ASC,
[ID] ASC,
[ErrorCount] ASC
)
INCLUDE ( [FromEmailAddress],
[ToEmailAddresses],
[CCEmailAddresses],
[BCCEmailAddresses],
[Subject],
[Body],
[AttachmentUrl],
[CreatedDate],
[FromEmailDisplayName],
[ReplyToEmailAddress]) WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
(附带说明:该索引的建议大小为 5,850,573 KB (?),接近 6 GB,对我来说根本没有任何意义。)
我的问题是这个建议的索引是否有意义?例如,为什么ID
包含列,而查询中不需要它(据我所知)?就我对索引的了解而言,它们旨在快速查找以找到相关行。如果我必须自己设计索引,我会想出类似的东西:
SET ANSI_PADDING ON
CREATE NONCLUSTERED INDEX [index_alternative_a] ON [dbo].[sysEmailMessage]
(
[SentDate] ASC,
[ErrorCount] ASC
)
WITH (SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
优化器真的很聪明还是我的索引更有效并且可能更好?