Sql-Server

支持查詢的過濾索引 - 是否可以考慮空值?

  • May 24, 2018

我一直在處理這個查詢:

SELECT @amount = sum(accountAmount)  
FROM dbo.accountTransaction  
WHERE invoiceID = @invoiceID  
AND (notCountedInTotal = 0 OR notCountedInTotal IS NULL)   

因為我在過濾索引方面有很好的經驗

我可以創建這個:

CREATE NONCLUSTERED INDEX F_INVOICEID  ON dbo.accountTransaction (  INVOICEID  )  INCLUDE ( accountAmount)  
where (notCountedInTotal is null)

但這並不能解決我上面的查詢問題。所以我想有以下過濾索引:

 CREATE NONCLUSTERED INDEX F_INVOICEID  ON dbo.accountTransaction (  INVOICEID  )  INCLUDE ( accountAmount)  
WHERE (notCountedInTotal IN (null,0))

消息 10620,級別 16,狀態 1,第 38 行無法在表“dbo.accountTransaction”上創建過濾索引“F_INVOICEID”,因為過濾表達式包含與文字 NULL 值的比較。重寫比較以使用 IS

$$ NOT $$用於測試 NULL 值的 NULL 比較運算符。

有可能嗎?

是否值得創建一個計算列,持久化,將零和空值放在一起並在其上創建索引?

當您想在過濾索引中放置 OR 時,是否有解決方法?

我不會遇到與下面相同的問題嗎?

無法在計算列上創建過濾索引

我在這種情況下找到的解決方案是查看數據、約束、應用程序等,我可以將列更改notCountedInTotalnot null預設為零,如下面的表定義所示。

CREATE TABLE [dbo].[accountTransaction] ( 
[accountTransactionID]     INT              IDENTITY(1,1)   NOT NULL,
[accountCode]              VARCHAR(30)                          NULL,
[transactionType]          CHAR(3)                              NULL,
[transactionReason]        VARCHAR(100)                         NULL,
[applicationID]            INT                                  NULL,
[invoiceID]                INT                                  NULL,
[entityID]                 INT                                  NULL,
[entityType]               CHAR(1)                              NULL,
[transactionCurrencyCode]  CHAR(3)                              NULL,
[transactionAmount]        DECIMAL(10,2)                        NULL,
[accountCurrencyCode]      CHAR(3)                              NULL,
[accountAmount]            DECIMAL(10,2)                        NULL,
[chequeNo]                 VARCHAR(20)                          NULL,
[issuingBank]              VARCHAR(50)                          NULL,
[issuingCountryCode]       CHAR(2)                              NULL,
[STReference]              VARCHAR(30)                          NULL,
[paymentMethod]            VARCHAR(10)                          NULL,
[GPStatus]                 INT                                  NULL,
[transactionDate]          DATETIME                             NULL  CONSTRAINT [DF_accountTransaction_transactionDate] DEFAULT (getdate()),
[transactionStatus]        VARCHAR(10)                          NULL,
[userID]                   INT                                  NULL,
[parentID]                 INT                                  NULL,
[transactionComment]       VARCHAR(max)                         NULL,
[notCountedInTotal]        BIT                              NOT NULL  CONSTRAINT [DF_accountTransaction_notCountedInTotal] DEFAULT ((0)),
[enteredDate]              DATETIME                             NULL  CONSTRAINT [DF_accountTransaction_enteredDate] DEFAULT (getdate()),
[chequeDetails]            VARCHAR(1000)                    NOT NULL  CONSTRAINT [DF_accountTransaction_chequeDetails] DEFAULT (''),
CONSTRAINT   [PK_accountTransaction]  PRIMARY KEY CLUSTERED    ([accountTransactionID] asc) WITH FILLFACTOR = 100)

之後很容易添加過濾索引,因為沒有更多的either the value is null or the value is zero東西。

使用新創建的索引執行計劃:

在此處輸入圖像描述

引用自:https://dba.stackexchange.com/questions/207553