I'm working on large and gnarly sql script that is pushing 500 lines. At the moment it is organized roughly like this.
-- Declare and set a couple of dozen variables that will be used in the script
-- Do lots of deletes, inserts, and updates
At the moment it is all done in a single batch. The reason is that many of the operations rely on common variables, and variables don't cross GO
boundaries. Is there benefit to breaking a script like this up into smaller batches, even if it means redundantly re-declaring and re-setting some of the variables in each batch?
-- 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
Getting the values for the variables is inexpensive, usually setting them to literals or the result of selecting
from tables with only 10s of rows.
Each of the deletes, updates, and inserts operate on sets of roughly 1 million to 5 million rows.
Is there a theoretical memory, storage (for the log files), and/or performance improvement that would be gained by breaking this up into multiple batches and that would outweigh the ugliness and annoyance of re-declaring and re-setting some variables multiple times?
What resources are out there to learn more about batches in this kind of situation? I know there are cases where batches are required, such as creating or updating tables before operating on the new tables/columns. The answers to this question indicate that there may be some benefit for the size of the log file when using smaller batches. What I haven't been able to find is a definitive source of information on possible scenarios and performance benefits for using smaller batches.
Thanks