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 - 主要
IX_SResult_QSId_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
此外,我使用多个表,因此对于其他表没有这样的规律性。任何人都可以帮忙吗?
谢谢!