Shrinking SQL Server database that had most of the data deleted












2















I am working on a database that has been restored from a backup. It is then getting a large portion of the data deleted from it so that we can have a smaller subset of the data for dev and testing efforts. For example, we are deleting old orders.



All of this results in a database that was about 2 TB only having 100 GB of data in it. So I need to shrink it. The problem is that the script I was given by the previous dev is taking longer to run than the entire rest of the restore, and data reduction process.



The previous dev gave me the below script that loops over and reduces by a portion of the needed amount each time using DBCC SHRINKFILE. This is what we are using right now but I am sure this is not the best solution as it takes a really long time and I'm sure the amount it is fragmenting things is horrifying.



If it is any help we are using both Redgate and SQL Server to create backups that the developers can use to create their working databases.



DECLARE @FileSpace int;
DECLARE @MinSize int
DECLARE @Size int;
DECLARE @FileName nvarchar(100);

SET @Size = @FileSpace;

WHILE @Size > @MinSize
BEGIN
SET @Size = @Size - 1000;

DBCC SHRINKFILE (@FileName , @Size)

SET @Size = (select database_files.size/128 from sys.database_files where database_files.name = @FileName);
END


I'm really needing something that will be much faster because at present it has taken well over 3 days.










share|improve this question

























  • Which version of SQL Serveer are you using?

    – PhilS
    Jan 2 at 18:15











  • Create a new database and copy all the tables into it? SHRINKFILE is notorious for its inefficiency. You could also increase the shrink delta.

    – SqlACID
    Jan 2 at 18:15











  • Have you tried just using DBCC SHRINKDATABASE

    – PhilS
    Jan 2 at 18:16











  • Is it OK that your size is set to megabytes (MB)=1,024 KB at the end of the cycle? Why would you need to shrink it more than once again?

    – Pavel Nefyodov
    Jan 2 at 18:16






  • 2





    What is @minsize set to? as written this will loop forever since the size will never go to zero

    – SqlACID
    Jan 2 at 18:21
















2















I am working on a database that has been restored from a backup. It is then getting a large portion of the data deleted from it so that we can have a smaller subset of the data for dev and testing efforts. For example, we are deleting old orders.



All of this results in a database that was about 2 TB only having 100 GB of data in it. So I need to shrink it. The problem is that the script I was given by the previous dev is taking longer to run than the entire rest of the restore, and data reduction process.



The previous dev gave me the below script that loops over and reduces by a portion of the needed amount each time using DBCC SHRINKFILE. This is what we are using right now but I am sure this is not the best solution as it takes a really long time and I'm sure the amount it is fragmenting things is horrifying.



If it is any help we are using both Redgate and SQL Server to create backups that the developers can use to create their working databases.



DECLARE @FileSpace int;
DECLARE @MinSize int
DECLARE @Size int;
DECLARE @FileName nvarchar(100);

SET @Size = @FileSpace;

WHILE @Size > @MinSize
BEGIN
SET @Size = @Size - 1000;

DBCC SHRINKFILE (@FileName , @Size)

SET @Size = (select database_files.size/128 from sys.database_files where database_files.name = @FileName);
END


I'm really needing something that will be much faster because at present it has taken well over 3 days.










share|improve this question

























  • Which version of SQL Serveer are you using?

    – PhilS
    Jan 2 at 18:15











  • Create a new database and copy all the tables into it? SHRINKFILE is notorious for its inefficiency. You could also increase the shrink delta.

    – SqlACID
    Jan 2 at 18:15











  • Have you tried just using DBCC SHRINKDATABASE

    – PhilS
    Jan 2 at 18:16











  • Is it OK that your size is set to megabytes (MB)=1,024 KB at the end of the cycle? Why would you need to shrink it more than once again?

    – Pavel Nefyodov
    Jan 2 at 18:16






  • 2





    What is @minsize set to? as written this will loop forever since the size will never go to zero

    – SqlACID
    Jan 2 at 18:21














2












2








2


1






I am working on a database that has been restored from a backup. It is then getting a large portion of the data deleted from it so that we can have a smaller subset of the data for dev and testing efforts. For example, we are deleting old orders.



All of this results in a database that was about 2 TB only having 100 GB of data in it. So I need to shrink it. The problem is that the script I was given by the previous dev is taking longer to run than the entire rest of the restore, and data reduction process.



The previous dev gave me the below script that loops over and reduces by a portion of the needed amount each time using DBCC SHRINKFILE. This is what we are using right now but I am sure this is not the best solution as it takes a really long time and I'm sure the amount it is fragmenting things is horrifying.



If it is any help we are using both Redgate and SQL Server to create backups that the developers can use to create their working databases.



DECLARE @FileSpace int;
DECLARE @MinSize int
DECLARE @Size int;
DECLARE @FileName nvarchar(100);

SET @Size = @FileSpace;

WHILE @Size > @MinSize
BEGIN
SET @Size = @Size - 1000;

DBCC SHRINKFILE (@FileName , @Size)

SET @Size = (select database_files.size/128 from sys.database_files where database_files.name = @FileName);
END


I'm really needing something that will be much faster because at present it has taken well over 3 days.










share|improve this question
















I am working on a database that has been restored from a backup. It is then getting a large portion of the data deleted from it so that we can have a smaller subset of the data for dev and testing efforts. For example, we are deleting old orders.



All of this results in a database that was about 2 TB only having 100 GB of data in it. So I need to shrink it. The problem is that the script I was given by the previous dev is taking longer to run than the entire rest of the restore, and data reduction process.



The previous dev gave me the below script that loops over and reduces by a portion of the needed amount each time using DBCC SHRINKFILE. This is what we are using right now but I am sure this is not the best solution as it takes a really long time and I'm sure the amount it is fragmenting things is horrifying.



If it is any help we are using both Redgate and SQL Server to create backups that the developers can use to create their working databases.



DECLARE @FileSpace int;
DECLARE @MinSize int
DECLARE @Size int;
DECLARE @FileName nvarchar(100);

SET @Size = @FileSpace;

WHILE @Size > @MinSize
BEGIN
SET @Size = @Size - 1000;

DBCC SHRINKFILE (@FileName , @Size)

SET @Size = (select database_files.size/128 from sys.database_files where database_files.name = @FileName);
END


I'm really needing something that will be much faster because at present it has taken well over 3 days.







sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 19:08









marc_s

581k13011221268




581k13011221268










asked Jan 2 at 18:05









startrek2365startrek2365

113




113













  • Which version of SQL Serveer are you using?

    – PhilS
    Jan 2 at 18:15











  • Create a new database and copy all the tables into it? SHRINKFILE is notorious for its inefficiency. You could also increase the shrink delta.

    – SqlACID
    Jan 2 at 18:15











  • Have you tried just using DBCC SHRINKDATABASE

    – PhilS
    Jan 2 at 18:16











  • Is it OK that your size is set to megabytes (MB)=1,024 KB at the end of the cycle? Why would you need to shrink it more than once again?

    – Pavel Nefyodov
    Jan 2 at 18:16






  • 2





    What is @minsize set to? as written this will loop forever since the size will never go to zero

    – SqlACID
    Jan 2 at 18:21



















  • Which version of SQL Serveer are you using?

    – PhilS
    Jan 2 at 18:15











  • Create a new database and copy all the tables into it? SHRINKFILE is notorious for its inefficiency. You could also increase the shrink delta.

    – SqlACID
    Jan 2 at 18:15











  • Have you tried just using DBCC SHRINKDATABASE

    – PhilS
    Jan 2 at 18:16











  • Is it OK that your size is set to megabytes (MB)=1,024 KB at the end of the cycle? Why would you need to shrink it more than once again?

    – Pavel Nefyodov
    Jan 2 at 18:16






  • 2





    What is @minsize set to? as written this will loop forever since the size will never go to zero

    – SqlACID
    Jan 2 at 18:21

















Which version of SQL Serveer are you using?

– PhilS
Jan 2 at 18:15





Which version of SQL Serveer are you using?

– PhilS
Jan 2 at 18:15













Create a new database and copy all the tables into it? SHRINKFILE is notorious for its inefficiency. You could also increase the shrink delta.

– SqlACID
Jan 2 at 18:15





Create a new database and copy all the tables into it? SHRINKFILE is notorious for its inefficiency. You could also increase the shrink delta.

– SqlACID
Jan 2 at 18:15













Have you tried just using DBCC SHRINKDATABASE

– PhilS
Jan 2 at 18:16





Have you tried just using DBCC SHRINKDATABASE

– PhilS
Jan 2 at 18:16













Is it OK that your size is set to megabytes (MB)=1,024 KB at the end of the cycle? Why would you need to shrink it more than once again?

– Pavel Nefyodov
Jan 2 at 18:16





Is it OK that your size is set to megabytes (MB)=1,024 KB at the end of the cycle? Why would you need to shrink it more than once again?

– Pavel Nefyodov
Jan 2 at 18:16




2




2





What is @minsize set to? as written this will loop forever since the size will never go to zero

– SqlACID
Jan 2 at 18:21





What is @minsize set to? as written this will loop forever since the size will never go to zero

– SqlACID
Jan 2 at 18:21












1 Answer
1






active

oldest

votes


















0














First



Why you should NOT be shrinking alot. with emphasis on the included links in that article.



And then there is this parameter



As written this would never execute since @MinSize is null @Size > null returns null and thus is false, preventing the execution. If you are setting this to something, like a low number, you could have an infinite loop if the condition @Size > @MinSize is never met.



But it's not that... so now what



Another culprit could be blocking. Yep, DBCC SHRINKFILE can cause blocking. To see if this is true, I'd start with Adam Machanic's sp_WhoIsActive. It's well documented and will allow you to do get the blocking tree via @find_block_leaders parameter.



It's not fast, usually



As many articles have illustrated, SHRINKFILE eats up CPU and kills your I/O, can cause blocking, produces internal fragmentation which can leave you back to the same spot you are in after fixing that via, hopefully Ola's Index Optimize scripts.. So maybe your server is just under spec'd. This is the least likely but is always a potential.



What would you do if you were me?



Back the database up, and then restore it over your current version. The backup processes doesn't backup empty pages. Hopefully your fill factor is set to 100% and this isn't a bloating issue. See more on that here. So a quick backup and restore would fix your issue.



If I really needed to shrink a data file, I wouldn't use a looping script. I'd do them one at a time. You can even use the GUI! At a minimum, add some PRINT commands to your script to see what could be causing the infinite loop, if it is in fact infinite. I'd print out all of the parameter values each iteration to start with.






share|improve this answer


























  • I know why I shouldn't be shrinking a lot but this is a situation that it makes sense that's part of why I want to change it. This is clearly the wrong way and a loop doesn't add any benefit while also creating lots more processing and fragmentation. It is on its own server with this being the only kind of thing that it will be doing. I have tried doing a backup and restore but the file size in the result is the same as before I back it up. Maybe I have an option that isn't set correctly? It was a Redgate backup so maybe that is a factor.

    – startrek2365
    Jan 2 at 20:28











  • Well RedGate should have a compression setting to, which would reduce the backup size. If you are deleting the data, backing it up, restoring it and the size is the same then maybe your fill factor is way off. I'd expect it to be somewhat smaller. You could also defrag those indexes before you backup, that'd make it even SMALLER. Was their blocking? What was the CPU and I/O for the statement?

    – scsimon
    Jan 2 at 20:39













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011102%2fshrinking-sql-server-database-that-had-most-of-the-data-deleted%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














First



Why you should NOT be shrinking alot. with emphasis on the included links in that article.



And then there is this parameter



As written this would never execute since @MinSize is null @Size > null returns null and thus is false, preventing the execution. If you are setting this to something, like a low number, you could have an infinite loop if the condition @Size > @MinSize is never met.



But it's not that... so now what



Another culprit could be blocking. Yep, DBCC SHRINKFILE can cause blocking. To see if this is true, I'd start with Adam Machanic's sp_WhoIsActive. It's well documented and will allow you to do get the blocking tree via @find_block_leaders parameter.



It's not fast, usually



As many articles have illustrated, SHRINKFILE eats up CPU and kills your I/O, can cause blocking, produces internal fragmentation which can leave you back to the same spot you are in after fixing that via, hopefully Ola's Index Optimize scripts.. So maybe your server is just under spec'd. This is the least likely but is always a potential.



What would you do if you were me?



Back the database up, and then restore it over your current version. The backup processes doesn't backup empty pages. Hopefully your fill factor is set to 100% and this isn't a bloating issue. See more on that here. So a quick backup and restore would fix your issue.



If I really needed to shrink a data file, I wouldn't use a looping script. I'd do them one at a time. You can even use the GUI! At a minimum, add some PRINT commands to your script to see what could be causing the infinite loop, if it is in fact infinite. I'd print out all of the parameter values each iteration to start with.






share|improve this answer


























  • I know why I shouldn't be shrinking a lot but this is a situation that it makes sense that's part of why I want to change it. This is clearly the wrong way and a loop doesn't add any benefit while also creating lots more processing and fragmentation. It is on its own server with this being the only kind of thing that it will be doing. I have tried doing a backup and restore but the file size in the result is the same as before I back it up. Maybe I have an option that isn't set correctly? It was a Redgate backup so maybe that is a factor.

    – startrek2365
    Jan 2 at 20:28











  • Well RedGate should have a compression setting to, which would reduce the backup size. If you are deleting the data, backing it up, restoring it and the size is the same then maybe your fill factor is way off. I'd expect it to be somewhat smaller. You could also defrag those indexes before you backup, that'd make it even SMALLER. Was their blocking? What was the CPU and I/O for the statement?

    – scsimon
    Jan 2 at 20:39


















0














First



Why you should NOT be shrinking alot. with emphasis on the included links in that article.



And then there is this parameter



As written this would never execute since @MinSize is null @Size > null returns null and thus is false, preventing the execution. If you are setting this to something, like a low number, you could have an infinite loop if the condition @Size > @MinSize is never met.



But it's not that... so now what



Another culprit could be blocking. Yep, DBCC SHRINKFILE can cause blocking. To see if this is true, I'd start with Adam Machanic's sp_WhoIsActive. It's well documented and will allow you to do get the blocking tree via @find_block_leaders parameter.



It's not fast, usually



As many articles have illustrated, SHRINKFILE eats up CPU and kills your I/O, can cause blocking, produces internal fragmentation which can leave you back to the same spot you are in after fixing that via, hopefully Ola's Index Optimize scripts.. So maybe your server is just under spec'd. This is the least likely but is always a potential.



What would you do if you were me?



Back the database up, and then restore it over your current version. The backup processes doesn't backup empty pages. Hopefully your fill factor is set to 100% and this isn't a bloating issue. See more on that here. So a quick backup and restore would fix your issue.



If I really needed to shrink a data file, I wouldn't use a looping script. I'd do them one at a time. You can even use the GUI! At a minimum, add some PRINT commands to your script to see what could be causing the infinite loop, if it is in fact infinite. I'd print out all of the parameter values each iteration to start with.






share|improve this answer


























  • I know why I shouldn't be shrinking a lot but this is a situation that it makes sense that's part of why I want to change it. This is clearly the wrong way and a loop doesn't add any benefit while also creating lots more processing and fragmentation. It is on its own server with this being the only kind of thing that it will be doing. I have tried doing a backup and restore but the file size in the result is the same as before I back it up. Maybe I have an option that isn't set correctly? It was a Redgate backup so maybe that is a factor.

    – startrek2365
    Jan 2 at 20:28











  • Well RedGate should have a compression setting to, which would reduce the backup size. If you are deleting the data, backing it up, restoring it and the size is the same then maybe your fill factor is way off. I'd expect it to be somewhat smaller. You could also defrag those indexes before you backup, that'd make it even SMALLER. Was their blocking? What was the CPU and I/O for the statement?

    – scsimon
    Jan 2 at 20:39
















0












0








0







First



Why you should NOT be shrinking alot. with emphasis on the included links in that article.



And then there is this parameter



As written this would never execute since @MinSize is null @Size > null returns null and thus is false, preventing the execution. If you are setting this to something, like a low number, you could have an infinite loop if the condition @Size > @MinSize is never met.



But it's not that... so now what



Another culprit could be blocking. Yep, DBCC SHRINKFILE can cause blocking. To see if this is true, I'd start with Adam Machanic's sp_WhoIsActive. It's well documented and will allow you to do get the blocking tree via @find_block_leaders parameter.



It's not fast, usually



As many articles have illustrated, SHRINKFILE eats up CPU and kills your I/O, can cause blocking, produces internal fragmentation which can leave you back to the same spot you are in after fixing that via, hopefully Ola's Index Optimize scripts.. So maybe your server is just under spec'd. This is the least likely but is always a potential.



What would you do if you were me?



Back the database up, and then restore it over your current version. The backup processes doesn't backup empty pages. Hopefully your fill factor is set to 100% and this isn't a bloating issue. See more on that here. So a quick backup and restore would fix your issue.



If I really needed to shrink a data file, I wouldn't use a looping script. I'd do them one at a time. You can even use the GUI! At a minimum, add some PRINT commands to your script to see what could be causing the infinite loop, if it is in fact infinite. I'd print out all of the parameter values each iteration to start with.






share|improve this answer















First



Why you should NOT be shrinking alot. with emphasis on the included links in that article.



And then there is this parameter



As written this would never execute since @MinSize is null @Size > null returns null and thus is false, preventing the execution. If you are setting this to something, like a low number, you could have an infinite loop if the condition @Size > @MinSize is never met.



But it's not that... so now what



Another culprit could be blocking. Yep, DBCC SHRINKFILE can cause blocking. To see if this is true, I'd start with Adam Machanic's sp_WhoIsActive. It's well documented and will allow you to do get the blocking tree via @find_block_leaders parameter.



It's not fast, usually



As many articles have illustrated, SHRINKFILE eats up CPU and kills your I/O, can cause blocking, produces internal fragmentation which can leave you back to the same spot you are in after fixing that via, hopefully Ola's Index Optimize scripts.. So maybe your server is just under spec'd. This is the least likely but is always a potential.



What would you do if you were me?



Back the database up, and then restore it over your current version. The backup processes doesn't backup empty pages. Hopefully your fill factor is set to 100% and this isn't a bloating issue. See more on that here. So a quick backup and restore would fix your issue.



If I really needed to shrink a data file, I wouldn't use a looping script. I'd do them one at a time. You can even use the GUI! At a minimum, add some PRINT commands to your script to see what could be causing the infinite loop, if it is in fact infinite. I'd print out all of the parameter values each iteration to start with.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 19:38

























answered Jan 2 at 19:30









scsimonscsimon

22.2k51637




22.2k51637













  • I know why I shouldn't be shrinking a lot but this is a situation that it makes sense that's part of why I want to change it. This is clearly the wrong way and a loop doesn't add any benefit while also creating lots more processing and fragmentation. It is on its own server with this being the only kind of thing that it will be doing. I have tried doing a backup and restore but the file size in the result is the same as before I back it up. Maybe I have an option that isn't set correctly? It was a Redgate backup so maybe that is a factor.

    – startrek2365
    Jan 2 at 20:28











  • Well RedGate should have a compression setting to, which would reduce the backup size. If you are deleting the data, backing it up, restoring it and the size is the same then maybe your fill factor is way off. I'd expect it to be somewhat smaller. You could also defrag those indexes before you backup, that'd make it even SMALLER. Was their blocking? What was the CPU and I/O for the statement?

    – scsimon
    Jan 2 at 20:39





















  • I know why I shouldn't be shrinking a lot but this is a situation that it makes sense that's part of why I want to change it. This is clearly the wrong way and a loop doesn't add any benefit while also creating lots more processing and fragmentation. It is on its own server with this being the only kind of thing that it will be doing. I have tried doing a backup and restore but the file size in the result is the same as before I back it up. Maybe I have an option that isn't set correctly? It was a Redgate backup so maybe that is a factor.

    – startrek2365
    Jan 2 at 20:28











  • Well RedGate should have a compression setting to, which would reduce the backup size. If you are deleting the data, backing it up, restoring it and the size is the same then maybe your fill factor is way off. I'd expect it to be somewhat smaller. You could also defrag those indexes before you backup, that'd make it even SMALLER. Was their blocking? What was the CPU and I/O for the statement?

    – scsimon
    Jan 2 at 20:39



















I know why I shouldn't be shrinking a lot but this is a situation that it makes sense that's part of why I want to change it. This is clearly the wrong way and a loop doesn't add any benefit while also creating lots more processing and fragmentation. It is on its own server with this being the only kind of thing that it will be doing. I have tried doing a backup and restore but the file size in the result is the same as before I back it up. Maybe I have an option that isn't set correctly? It was a Redgate backup so maybe that is a factor.

– startrek2365
Jan 2 at 20:28





I know why I shouldn't be shrinking a lot but this is a situation that it makes sense that's part of why I want to change it. This is clearly the wrong way and a loop doesn't add any benefit while also creating lots more processing and fragmentation. It is on its own server with this being the only kind of thing that it will be doing. I have tried doing a backup and restore but the file size in the result is the same as before I back it up. Maybe I have an option that isn't set correctly? It was a Redgate backup so maybe that is a factor.

– startrek2365
Jan 2 at 20:28













Well RedGate should have a compression setting to, which would reduce the backup size. If you are deleting the data, backing it up, restoring it and the size is the same then maybe your fill factor is way off. I'd expect it to be somewhat smaller. You could also defrag those indexes before you backup, that'd make it even SMALLER. Was their blocking? What was the CPU and I/O for the statement?

– scsimon
Jan 2 at 20:39







Well RedGate should have a compression setting to, which would reduce the backup size. If you are deleting the data, backing it up, restoring it and the size is the same then maybe your fill factor is way off. I'd expect it to be somewhat smaller. You could also defrag those indexes before you backup, that'd make it even SMALLER. Was their blocking? What was the CPU and I/O for the statement?

– scsimon
Jan 2 at 20:39






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54011102%2fshrinking-sql-server-database-that-had-most-of-the-data-deleted%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'