Sql-Server-2008

如果我更新表中的列記錄,其中沒有該列的索引會受到影響嗎?

  • August 13, 2015

如果我有這樣的表,就性能而言:

CREATE TABLE [TESTDATA].[TableA](
   [Col1] [nchar](5) NOT NULL,
   [Col2] [nchar](2) NULL,
   [Col3] [float] NULL
CONSTRAINT [TableA_PK] PRIMARY KEY CLUSTERED 
(
   [Col1] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

然後像這樣創建一個非聚集索引:

CREATE NONCLUSTERED INDEX [_idx_TableA]
ON [TESTDATA].[TableA] ([Col2])
WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
GO

如果我對錶執行更新,只更改Col3數據庫是否需要觸摸索引_idx_TableA

只是好奇性能對所有索引的影響有多遠?

不,_idx_TableA不會影響此操作。我修改了您的範例並添加了另一個實際上包含 key column 的索引(NCI)Col3。這是我的範常式式碼:

use testdb;
go

CREATE TABLE [DBO].[TableA](
   [Col1] [nchar](5) NOT NULL,
   [Col2] [nchar](2) NULL,
   [Col3] [int] NULL
CONSTRAINT [TableA_PK] PRIMARY KEY CLUSTERED 
(
   [Col1] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE NONCLUSTERED INDEX [_idx_TableA]
ON [DBO].[TableA] ([Col2])
WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY]
GO

create nonclustered index IX_Col3
on dbo.TableA(Col3);
go

insert into dbo.TableA
values
   ('a', 'b', 10),
   ('b', 'c', 11),
   ('c', 'd', 12);
go

update dbo.TableA
set Col3 = 13;

如果我擷取該UPDATE命令的執行後計劃,您將在此處看到類似的內容:

在此處輸入圖像描述

正如您從上面的螢幕截圖中看到的,更新的索引是聚群索引和我的非聚群索引IX_Col3索引_idx_TableA沒有得到更新。因此,只有包含的索引Col3會受到影響,因此會被您的特定範例更新。

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