由於某種原因,TSql合併指令減慢。tsql合併速度減慢
我合併批量批量大小等於10000條記錄的數據批次,我看到從一批到另一批合併花費的時間越來越長。
這裏是合併指令:
MERGE [dbo].[SResult] AS target
USING [dbo].[SResultTemp] AS source
ON (target.QSId = source.QSId
and target.ResultId = source.ResultId
and target.EngineId = source.EngineId)
WHEN NOT MATCHED THEN INSERT
(QSId, ResultId, EngineId, Position)
values
(source.QSId, source.ResultId, source.EngineId, source.Position);
源表聲明如下
CREATE TABLE [dbo].[SResultTemp](
[QSId] int not null,
[ResultId] int not null,
[EngineId] int not null,
[Position] int not null,
CONSTRAINT [PK_SResultTemp] PRIMARY KEY CLUSTERED(
[QSId], [ResultId], [EngineId], [Position] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
目標是相同的,但它有附加字段SResultId作爲主鍵和另一組的索引的:
PK_SResult - primary
IX_SResult_QS Id_ResultId - 非唯一,非聚集
IX_SResult_EngineId - 非唯一的非聚集
UX_SResult_EngineId_QSId_Position - 唯一的非聚集
和這裏是我在日誌中看到:
Results Upload: SResult took 00:00:01.0008344
Results Upload: SResult took 00:00:18.1046734
Results Upload: SResult took 00:00:17.9797846
Results Upload: SResult took 00:00:27.7828817
Results Upload: SResult took 00:01:30.4140091
Results Upload: SResult took 00:03:17.6433416
Results Upload: SResult took 00:03:21.3761251
Results Upload: SResult took 00:06:07.2555342
Results Upload: SResult took 00:06:56.2423653
Results Upload: SResult took 00:06:57.1729179
Results Upload: SResult took 00:07:09.7221083
而且,我的工作與多個表,所以其他表沒有這樣的規律性。誰能幫忙?
謝謝!
公平你的PK有點失控。 – ChaosPandion
由於PK獲取片段插入速度會降低(並且比你想象的要快很多) – Paparazzi