gpt4 book ai didi

sql-server - 表越大,数据库插入越慢

转载 作者:行者123 更新时间:2023-12-05 07:36:53 24 4
gpt4 key购买 nike

我遇到了数据库插入速度随着表变大而变慢的问题。我正在插入大约 130 万条记录,其中前 100,000 条以合理的速度插入,但随后插入速度逐渐变慢。

该表一开始是空的,确实有三个外键和五个索引,我读过这些可能会导致速度变慢,因为每次插入都必须重新编制索引。我可以确认这是问题的可能原因吗?

无论如何,我可以通过在插入语句后禁用索引并在最后再次执行(不删除索引)来提高性能吗?

有没有人对提高性能有任何进一步的建议。

我正在使用 SQL Server,它在我的第二个硬盘上。

最佳答案

可能会有很多问题。参见 here有关优化批量插入的 MSDN 建议。但是,我会尝试将批处理限制为 10,000 或 100,000 行,然后看看结果如何。如果它有很大帮助,那么您应该花一些时间来确定最佳批量大小。我已经多次看到这是一个问题,因为

a) 事务日志如何增长,人们不了解它如何影响日志以及日志需要如何保持增长。

b) 大量行的索引对内存有何影响。来自关于批处理的文章 here

SQL Server optimizes the load automatically, according to the batch size value, which may result in better performance. In general, you should make the batch size as large as practical. Typically, the larger the batch size, the better the performance of the bulk-import operation. A few exceptions exist, however. If one or more indexes exist on the target table, then a larger batch size might put memory pressure for sorting.

批处理文章中还有其他说明,说明更改批处理大小如何提高性能以及原因,这些只是几个示例(也是我最常看到的示例)。

关于sql-server - 表越大,数据库插入越慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48890373/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com