我在 sql server 2019 上工作
更新表时Z2DataCore.parts.SourcingNotMappedParts
我发现更新时缓慢而长时间
更新语句需要 26.5 分钟才能完成对 692488 行计数的更新,因此 How to make Faster 。
语句生成缓慢的过程如下:
UPDATE s
SET s.PriorityLevel = 'I2'
FROM Z2DataCore.parts.SourcingNotMappedParts s
INNER JOIN extractreports.dbo.alldistSuppliersourceid g
ON g.SourcingNotMappedPartsID = s.SourcingNotMappedPartsID
受影响的更新行数将是 692488
我需要更新的表 SourcingNotMappedParts 通常有 7100 万行。
extractreports.dbo.SourcingNotMappedPartsIDI1
我将从它获取要更新的数据的表是 692488 。
表extractreports.dbo.SourcingNotMappedPartsIDI1
只有一SourcingNotMappedPartsID
列int datatype
表extractreports.dbo.SourcingNotMappedPartsIDI1
只有一个索引如下
CREATE clustered INDEX SourcingNotMappedPartsIDI1_IDX ON extractreports.dbo.SourcingNotMappedPartsIDI1(SourcingNotMappedPartsID)
USE [Z2DataCore]
GO
/****** Object: Table [Parts].[SourcingNotMappedParts] Script Date: 3/4/2022 12:05:36 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [Parts].[SourcingNotMappedParts](
[SourcingNotMappedPartsID] [int] IDENTITY(1,1) NOT NULL,
[SearchPart] [nvarchar](200) NULL,
[GivenManufacture] [nvarchar](200) NULL,
[CompanyId] [int] NULL,
[SourceTypeID] [int] NULL,
[PartStatus] [nvarchar](50) NULL,
[StockId] [int] NULL,
[SourceUrl] [nvarchar](2000) NULL,
[PartId] [int] NULL,
[GroupID] [int] NULL,
[PartStatusID] [int] NULL,
[MatchStatus] [nvarchar](200) NULL,
[GivenPartNumber_Non] [nvarchar](200) NULL,
[GivenManufacturer_Non] [nvarchar](200) NULL,
[signatureID] [int] NULL,
[VCompanyId] [int] NULL,
[PriorityLevel] [nvarchar](10) NULL,
[NotMappedCode] [int] NULL,
CONSTRAINT [PK_Parts.SourcingNotMappedParts] PRIMARY KEY CLUSTERED
(
[SourcingNotMappedPartsID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object: Index [IDX_MatchStatus_StatusID] Script Date: 3/4/2022 12:05:37 AM ******/
CREATE NONCLUSTERED INDEX [IDX_MatchStatus_StatusID] ON [Parts].[SourcingNotMappedParts]
(
[PartStatusID] ASC,
[MatchStatus] 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 [PRIMARY]
GO
/****** Object: Index [IDX_Part_status_ID] Script Date: 3/4/2022 12:05:37 AM ******/
CREATE NONCLUSTERED INDEX [IDX_Part_status_ID] ON [Parts].[SourcingNotMappedParts]
(
[PartStatusID] 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 [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object: Index [IDX_SourceURL] Script Date: 3/4/2022 12:05:37 AM ******/
CREATE NONCLUSTERED INDEX [IDX_SourceURL] ON [Parts].[SourcingNotMappedParts]
(
[SourceUrl] 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 [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object: Index [IDX_SourcingNotMappedParts_GroupID_SearchPart] Script Date: 3/4/2022 12:05:37 AM ******/
CREATE NONCLUSTERED INDEX [IDX_SourcingNotMappedParts_GroupID_SearchPart] ON [Parts].[SourcingNotMappedParts]
(
[GroupID] ASC,
[SearchPart] ASC
)
INCLUDE ( [signatureID]) 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 [PRIMARY]
GO
/****** Object: Index [IDX_SourcingNotMappedParts_PartId] Script Date: 3/4/2022 12:05:37 AM ******/
CREATE NONCLUSTERED INDEX [IDX_SourcingNotMappedParts_PartId] ON [Parts].[SourcingNotMappedParts]
(
[PartId] 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 [PRIMARY]
GO
/****** Object: Index [IDX_SourcingNotMappedParts_SignatureID] Script Date: 3/4/2022 12:05:37 AM ******/
CREATE NONCLUSTERED INDEX [IDX_SourcingNotMappedParts_SignatureID] ON [Parts].[SourcingNotMappedParts]
(
[signatureID] ASC
)
INCLUDE ( [PartId]) 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 [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object: Index [IX_NotMapped_NonalphaPartCompany] Script Date: 3/4/2022 12:05:37 AM ******/
CREATE NONCLUSTERED INDEX [IX_NotMapped_NonalphaPartCompany] ON [Parts].[SourcingNotMappedParts]
(
[GivenPartNumber_Non] ASC,
[VCompanyId] 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 [PRIMARY]
GO
/****** Object: Index [IX_NotMapped_NotMappedCode] Script Date: 3/4/2022 12:05:37 AM ******/
CREATE NONCLUSTERED INDEX [IX_NotMapped_NotMappedCode] ON [Parts].[SourcingNotMappedParts]
(
[NotMappedCode] 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 [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object: Index [IX_NotMapped_PriorityLevel] Script Date: 3/4/2022 12:05:37 AM ******/
CREATE NONCLUSTERED INDEX [IX_NotMapped_PriorityLevel] ON [Parts].[SourcingNotMappedParts]
(
[PriorityLevel] 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 [PRIMARY]
GO
/****** Object: Index [IX_NotMapped_SourceType] Script Date: 3/4/2022 12:05:37 AM ******/
CREATE NONCLUSTERED INDEX [IX_NotMapped_SourceType] ON [Parts].[SourcingNotMappedParts]
(
[SourceTypeID] ASC,
[CompanyId] 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 [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object: Index [IX_NotMapped_VCompanyId_sourcetypeid] Script Date: 3/4/2022 12:05:37 AM ******/
CREATE NONCLUSTERED INDEX [IX_NotMapped_VCompanyId_sourcetypeid] ON [Parts].[SourcingNotMappedParts]
(
[VCompanyId] ASC,
[SourceTypeID] ASC,
[PriorityLevel] ASC
)
INCLUDE ( [GivenPartNumber_Non]) 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 [PRIMARY]
GO
SET ANSI_PADDING ON
GO
/****** Object: Index [IX_SourcingNotMappedParts_VCompanyId] Script Date: 3/4/2022 12:05:37 AM ******/
CREATE NONCLUSTERED INDEX [IX_SourcingNotMappedParts_VCompanyId] ON [Parts].[SourcingNotMappedParts]
(
[VCompanyId] ASC
)
INCLUDE ( [CompanyId],
[SourceTypeID],
[StockId],
[GivenPartNumber_Non],
[PriorityLevel]) 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 [PRIMARY]
GO
那么如何解决缓慢的问题呢?
我检查了更新状态,发现等待类型如下:
这与慢或不相关。
您更新的实际执行计划也表明您的
extractreports.dbo.alldistSuppliersourceid
表上没有索引,因为它当前正在对其使用表扫描操作。它至少应该有一个聚集索引。如果该聚集索引在您的查询当前加入的字段上有意义
SourcingNotMappedPartsID
(取决于您按该列查询的频率),那么您可以像这样创建它:如果
SourcingNotMappedPartsID
是唯一的,extractreports.dbo.alldistSuppliersourceid
那么您还可以在UNIQUE
关键字之后指定CREATE
关键字。否则,如果另一个字段可以作为 上的聚集索引
extractreports.dbo.alldistSuppliersourceid
,则使用上面的脚本替换该列来创建聚集索引,您可以在其上创建一个额外的非聚集索引,如下所示:同样,
UNIQUE
如果它也适用,请使用关键字。那个指数至少应该在一定程度上帮助你的表现。