Created
April 17, 2017 17:57
-
-
Save ignas-sakalauskas/c02887ead0155679490879f1e11bb4e6 to your computer and use it in GitHub Desktop.
Clean and shrink SQL database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SET NOCOUNT ON; | |
DECLARE @rows int = 1; | |
DECLARE @now datetime = GETDATE(); | |
WHILE @rows > 0 | |
BEGIN | |
BEGIN TRANSACTION; | |
DELETE TOP (500) [ASPState].dbo.ASPStateTempSessions | |
WHERE Expires < @now | |
SET @rows = @@ROWCOUNT; | |
COMMIT TRANSACTION; | |
CHECKPOINT; | |
END | |
-- https://ignas.me/tech/clean-and-shrink-sql-database/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALTER DATABASE ASPState SET RECOVERY SIMPLE | |
DBCC SHRINKFILE (ASPState_Log, 1) | |
-- https://ignas.me/tech/clean-and-shrink-sql-database/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment