为什么不平等比平等更快地返回结果呢?

时间:2012-11-26 16:45:35

标签: performance tsql sql-server-2005 querying

关于此特定查询:

不要指望理解被查询的数据:它是无关紧要的。

只需看一下 WHERE子句

SELECT 
    de.DocumentEntryId,
    de.Number,
    currentdev.ToIgnore,
    previousdev.ToIgnore,
    currentdev.DocumentEntryValueId AS CurrentDocumentEntryValueId, 
    SUM(currentper.Value) AS CurrentPaymentRate,
    SUM(currentdevr.ConversionRate) AS CurrentConversionRate,
    MAX(previousdev.DocumentEntryValueId) AS PreviousDocumentEntryValueId,
    SUM(previousper.Value) AS PreviousPaymentRate,
    SUM(previousdevr.ConversionRate) AS PreviousConversionRate
FROM 
    DocumentEntry de
    INNER JOIN PaymentEntry currentpe ON currentpe.PaymentEntryId = de.CurrentPaymentEntryId
    INNER JOIN DocumentEntryValue currentdev ON currentpe.DocumentEntryValueId = currentdev.DocumentEntryValueId
    INNER JOIN DocumentEntryValue previousdev ON previousdev.DocumentEntryId = de.DocumentEntryId
    INNER JOIN PaymentEntryRate currentper ON currentpe.PaymentEntryId = currentper.PaymentEntryId
    INNER JOIN DocumentEntryValueRate currentdevr ON currentdev.DocumentEntryValueId = currentdevr.DocumentEntryValueId
    INNER JOIN PaymentEntry previouspe ON previousdev.DocumentEntryValueId = previouspe.DocumentEntryValueId
    INNER JOIN PaymentEntryRate previousper ON previouspe.PaymentEntryId = previousper.PaymentEntryId
    INNER JOIN DocumentEntryValueRate previousdevr ON previousdevr.DocumentEntryValueId = previousdev.DocumentEntryValueId
WHERE 
    previousdev.DocumentEntryValueId <> currentdev.DocumentEntryValueId
    AND currentdev.ToIgnore <> 1
    AND previousdev.ToIgnore <> 1
    AND currentpe.PaymentId = previouspe.PaymentId
GROUP BY 
    de.DocumentEntryId, 
    de.Number,
    currentdev.ToIgnore,
    previousdev.ToIgnore,
    currentdev.DocumentEntryValueId
ORDER BY DocumentEntryId

特别是这两个不等式

AND currentdev.ToIgnore <> 1
AND previousdev.ToIgnore <> 1

注意:ToIgnore是一个专栏。

此查询大约需要10秒钟才能返回约1300行。

但是,如果我更改这两行以使用相等运算符:

AND currentdev.ToIgnore = 0
AND previousdev.ToIgnore = 0

返回任何东西都需要永远。

它有帮助,这是每个表的索引:

DocumentEntry 索引:

ALTER TABLE [dbo].[DocumentEntry] ADD  CONSTRAINT [DocumentEntry$0] PRIMARY KEY CLUSTERED 
(
    [DocumentEntryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]

CREATE NONCLUSTERED INDEX [IDX_DOCUMENTENTRY_REFNUMBER] ON [dbo].[DocumentEntry]
(
    [EntryReferenceNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]

CREATE NONCLUSTERED INDEX [missing_index_14227] ON [dbo].[DocumentEntry]
(
    [MasterDocumentEntryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]

CREATE NONCLUSTERED INDEX [missing_index_186297_186296_DocumentEntry] ON [dbo].[DocumentEntry]
(
    [Number] ASC
)
INCLUDE (   [DocumentEntryId]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]

DocumentEntryValue 索引:

ALTER TABLE [dbo].[DocumentEntryValue] ADD  CONSTRAINT [DocumentEntryValue$0] PRIMARY KEY CLUSTERED 
(
    [DocumentEntryValueId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]

CREATE NONCLUSTERED INDEX [IDX_DOCUMENTENTRYVALUE_DOCENTRYID] ON [dbo].[DocumentEntryValue]
(
    [DocumentEntryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]


CREATE NONCLUSTERED INDEX [IDX_DOCUMENTENTRYVALUE_REFNUMBER] ON [dbo].[DocumentEntryValue]
(
    [EntryValueReferenceNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]


CREATE NONCLUSTERED INDEX [missing_index_4022_4021_DocumentEntryValue] ON [dbo].[DocumentEntryValue]
(
    [ReferenceDocumentId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]

DocumentEntryValueRate 索引:

ALTER TABLE [dbo].[DocumentEntryValueRate] ADD  CONSTRAINT [DocumentEntryValueRate$0] PRIMARY KEY CLUSTERED 
(
    [DocumentEntryValueRateId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]


CREATE NONCLUSTERED INDEX [missing_index_56865_56864_DocumentEntryValueRate] ON [dbo].[DocumentEntryValueRate]
(
    [DocumentEntryValueId] ASC
)
INCLUDE (   [timestamp],
    [DocumentEntryValueRateId],
    [RateId],
    [RateVersionId],
    [RateTypeId],
    [RateGroupId],
    [RatePeriodValueId],
    [ConversionRate]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]

PaymentEntry 索引:

ALTER TABLE [dbo].[PaymentEntry] ADD  CONSTRAINT [PaymentEntry$0] PRIMARY KEY CLUSTERED 
(
    [PaymentEntryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]

CREATE NONCLUSTERED INDEX [IDX_PAYMENTENTRY_DOCENTRYVALUEID] ON [dbo].[PaymentEntry]
(
    [DocumentEntryValueId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]

CREATE NONCLUSTERED INDEX [missing_index_2517_2516_PaymentEntry] ON [dbo].[PaymentEntry]
(
    [PaymentId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]

PaymentEntryRate 索引:

ALTER TABLE [dbo].[PaymentEntryRate] ADD  CONSTRAINT [PaymentEntryRate$0] PRIMARY KEY CLUSTERED 
(
    [PaymentEntryRateId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]

CREATE NONCLUSTERED INDEX [IDX_PAYMENTENTRYRATE_PAYMENTENTRYID] ON [dbo].[PaymentEntryRate]
(
    [PaymentEntryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]

CREATE NONCLUSTERED INDEX [missing_index_2618_2617_PaymentEntryRate] ON [dbo].[PaymentEntryRate]
(
    [RateId] ASC
)
INCLUDE (   [timestamp],
    [PaymentEntryRateId],
    [PaymentEntryId],
    [RateTypeId],
    [RateGroupId],
    [RateVersionId],
    [RatePeriodValueId],
    [Value]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]

CREATE NONCLUSTERED INDEX [missing_index_2686_2685_PaymentEntryRate] ON [dbo].[PaymentEntryRate]
(
    [RateTypeId] ASC,
    [RateId] ASC,
    [RatePeriodValueId] ASC
)
INCLUDE (   [PaymentEntryId]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [Data Filegroup 1]

当我使用相等运算符(= 0)时,这是估计执行计划的一部分: Estimated execution plan

有人可以解释为什么,在这种情况下(或者可能在很多情况下),不等式运算符似乎比等式运算符更快?

1 个答案:

答案 0 :(得分:1)

我不知道为什么,但我无法发表评论,只能回答所以如果这对您不起作用请忽略。

使用“WHERE ToIgnore = 0”创建非聚集索引应该可以解决问题。

ToIgnore = 1 vs ToIgnore = 0的记录的记录数是多少?我的猜测是,ToIngore = 1的记录数远大于设置为0的记录。

我记得有一段时间回到Oracle / SQL DBA告诉我并且位列不需要索引,因为Microsoft为您解决了这个问题,这可以解释您的问题。但是,我无法找到任何支持它的东西,它可能与Oracle有关,他们认为MS也必须这样做。