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
另外,我使用多个表,因此对于其他表,没有这样的规律性。有人可以帮忙吗?
谢谢!
答案 0 :(得分:1)
当然,对于具有四个字段的群集PK,您需要更长的时间吗?您在合并期间不断重新排序表中的所有记录。
答案 1 :(得分:1)
索引片段插入速度降低 正如你所看到的那样可以迅速降级 如果你可以按照PK的顺序插入,那么这将减少碎片 填充索引会减慢碎片的速度 是否可以在插入完成后删除所有非聚簇索引然后重建?
SResultId是一个身份吗?
为什么指数设计?
您有一个与SResultTemp上的PK不同的唯一约束。
索引加速选择但会减慢插入和更新 联接的索引将有所帮助,但现在索引被拆分。
我的建议是将其分解为唯一性和测试所需的索引。
答案 2 :(得分:0)
有两个问题:
ON-CLAUSE
ON (target.QSId = source.QSId
and target.ResultId = source.ResultId
and target.EngineId = source.EngineId)
导致对目标中检查的需求增加,因为存在越来越多需要比较的行。
删除索引会极大地减慢ON子句的检查速度。我们在这里存在利益冲突。