简体   繁体   中英

Index seek instead of index scan for is not null in condition

I have the query below...

SELECT Compra_ID
FROM [Fart_Compras_dss_tracking]
WHERE [local_create_peer_timestamp] IS NOT NULL
CREATE TABLE [DataSync].[Fart_Compras_dss_tracking]
(
    [COMPRA_ID] [int] NOT NULL,
    [Sucursal_ID] [int] NOT NULL,
    [update_scope_local_id] [int] NULL,
    [scope_update_peer_key] [int] NULL,
    [scope_update_peer_timestamp] [bigint] NULL,
    [local_update_peer_key] [int] NOT NULL,
    [local_update_peer_timestamp] [timestamp] NOT NULL,
    [create_scope_local_id] [int] NULL,
    [scope_create_peer_key] [int] NULL,
    [scope_create_peer_timestamp] [bigint] NULL,
    [local_create_peer_key] [int] NOT NULL,
    [local_create_peer_timestamp] [bigint] NOT NULL,
    [sync_row_is_tombstone] [int] NOT NULL,
    [last_change_datetime] [datetime] NULL,

    CONSTRAINT [PK_DataSync.Fart_Compras_dss_tracking] 
        PRIMARY KEY CLUSTERED ([COMPRA_ID] ASC, [Sucursal_ID] ASC)
                WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [Idx_Text_4] 
ON [DataSync].[Fart_Compras_dss_tracking] ([COMPRA_ID] ASC,
                                           [Sucursal_ID] ASC,
                                           [local_create_peer_timestamp] ASC,
                                           [local_update_peer_timestamp] ASC)
INCLUDE ([update_scope_local_id], [scope_update_peer_key],
         [scope_update_peer_timestamp], [local_update_peer_key],
         [create_scope_local_id], [scope_create_peer_key],
         [scope_create_peer_timestamp], [local_create_peer_key],
         [sync_row_is_tombstone], [last_change_datetime]) 
WHERE ([local_create_peer_timestamp] IS NOT NULL)
WITH (STATISTICS_NORECOMPUTE = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
GO

在此处输入图像描述

Execution plan is available in the link below...

https://www.brentozar.com/pastetheplan/?id=SkZbOe8zi

When I take a look at the execution plan it shows INDEX SCAN, I tried creating different indexes/filtered indexes, but I am not able to see INDEX SEEK even forcing the usage of my indexes.

What index is required to improve performance for IS NOT NULL conditions like this?

The index you showed us isn't suitable for optimizing your filter condition. (It does seem to be quicker to scan than your PK clustered index, but a scan is still required.)

This index will help performance.

CREATE INDEX tstamp ON Fart_Compras_dss_tracking 
      (local_create_peer_timestamp);

It can be random-accessed by your WHERE filter. You'll still see an index range scan.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM