【发布时间】:2016-11-19 06:08:57
【问题描述】:
我正在编写 500 行的大而粗糙的 sql 脚本。目前大致是这样组织的。
-- Declare and set a couple of dozen variables that will be used in the script
-- Do lots of deletes, inserts, and updates
目前这一切都是在一个批次中完成的。原因是许多操作依赖于公共变量,而变量不会跨越GO 边界。将这样的脚本分成更小的批次是否有好处,即使这意味着在每个批次中重复地重新声明和重新设置一些变量?
-- Declare a few variables
-- Run the delete, update, and insert operations that rely on those variables
-- GO
-- Declare a few variables, some new and some the same as from the previous batch
-- Run the delete, update, and insert operations...
-- GO
-- Rinse and repeat about a dozen times
获取变量的值并不昂贵,通常将它们设置为文字或 selecting 从只有 10 行的表中的结果。
每次删除、更新和插入操作都在大约 100 万到 500 万行的集合上进行。
是否存在理论上的内存、存储(用于日志文件)和/或性能改进,可以通过将其分成多个批次来获得,并且这将超过重新声明和重新设置一些的丑陋和烦恼变量多次?
在这种情况下,有哪些资源可以详细了解批次?我知道在某些情况下需要批处理,例如在对新表/列进行操作之前创建或更新表。 this 问题的答案表明,当使用较小的批次时,日志文件的大小可能会有一些好处。我无法找到关于使用小批量的可能方案和性能优势的确切信息来源。
谢谢
【问题讨论】:
标签: sql sql-server database tsql