简体   繁体   English

索引搜索而不是索引扫描的条件不是 null

[英]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 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.当我查看显示 INDEX SCAN 的执行计划时,我尝试创建不同的索引/过滤索引,但我无法看到 INDEX SEEK 甚至强制使用我的索引。

What index is required to improve performance for IS NOT NULL conditions like this?像这样的IS NOT NULL条件需要什么索引来提高性能?

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.) (它看起来确实比你的 PK 聚簇索引扫描得更快,但仍然需要扫描。)

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.您的 WHERE 过滤器可以随机访问它。 You'll still see an index range scan.您仍然会看到索引范围扫描。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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