Results 1 to 7 of 7

Thread: [RESOLVED] Shrink DB (SQL Server 2005)

  1. #1

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Resolved [RESOLVED] Shrink DB (SQL Server 2005)

    I have the following senerio...

    I backup up a database and restored it to a new name. Ran some SQL against this restored database to drop all but one table. And all but 3 columns from the remaining table. When first restored the DB size was 59 Gig. After dropping and doing a DBCC ShrinkDatabase the size went to 34 Gig.
    The column that was left was and Identity (int), GUID (uniqueidentifier) and a document (Varbinary).

    In the original database I drop the document column for the same table. After doing a DBCC Shrink Database there is no change in DB size. There appears to be approx 4% free space in the database after the column drop. How do I go about reclaiming space after the drop.

    I would think that I should be able to reclaim at least 30 Gig of space.

    Thanks
    Gary
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Shrink DB (SQL Server 2005)

    You can truncate the log files too if they are no use to you. They too take a lot of space.
    I think your missing 30 Gig is in log files. Have you checked the log file size?

    e.g.
    Dbcc shrinkfile('templog')
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Shrink DB (SQL Server 2005)

    No in the log..... Already di that. I'm looking at the data file size.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Shrink DB (SQL Server 2005)

    ShrinkDatabase will not shrink a database file less than its minimum size. The minimum size is set by Create Database or Alter Database Modify File statements. Also, I think the Database option "Automatically Grow File" changes/sets the minimum size as well.

    The returned result set of ShrinkDatabase should help in determining how large of a file is required.

    You might want to try DBCC ShrinkFile instead.
    Last edited by brucevde; Jan 23rd, 2009 at 04:32 PM.

  5. #5

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Shrink DB (SQL Server 2005)

    Bruce

    I'm already doing that (I'll post the code I'm using later on my home system now). The bigger question I'm trying to answer is the issue of the free space. When I drop the column from the table there is now difference in the amount of free space in the database. Is it because the rows are still spread around the datapages? Would I need to re-orginize the table to get the space back?

    I hope I'm making sense.... Do I still have rows spread accros multiple datapages because they were spread due to the size of the data in that column I'm droping. It was a created as an Image datatype. In the database that this information is moving to after useing ShrinkFile I get the datafile size down to 34 gig from 57 gig. I can't get the same reducion on the orginial database.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Shrink DB (SQL Server 2005)

    Back on work machine this the the script I'm using for shrink:

    sql Code:
    1. DECLARE @DataFileFreeSpaceGB INT
    2. DECLARE @LogFileFreeSpaceGB INT
    3.  
    4. SELECT @DataFileFreeSpaceGB = 10
    5. SELECT @LogFileFreeSpaceGB = 1
    6.  
    7.  
    8. DECLARE @DBName  VARCHAR(100)  --Database Name
    9. DECLARE @filenameD VARCHAR(500) -- DataFile
    10. DECLARE @filenameL VARCHAR(500)  -- Logfile
    11. DECLARE @DSpaceUsed DECIMAL(10) --Spaceused in Meg
    12. DECLARE @LSpaceUsed DECIMAL(10) --Spaceused in Meng
    13. DECLARE @sStr VARCHAR(200)  -- A string SQL statement to EXECute
    14.  
    15. SELECT @DBName = DB_NAME()
    16. SET @sStr = 'ALTER DATABASE ' + @DBName + ' SET RECOVERY Simple'
    17. EXEC (@sStr)
    18.  
    19. --DataFile
    20. SELECT @fileNameD = name From sys.database_files Where type = 0
    21. SELECT @DSpaceUsed = (FILEPROPERTY (@filenameD,'SpaceUsed ') * 8192.0)/1024/1024
    22. SET @DSpaceUsed = @DSpaceUsed + (@DataFileFreeSpaceGB * 1000)
    23. SET @sStr = 'DBCC ShrinkFile (' + @fileNameD + ',' + Convert(VARCHAR(50),@DSpaceUsed) + ')'
    24. EXEC (@sStr)
    25.  
    26. --Logfile
    27. SELECT @fileNameL = name From sys.database_files Where type = 1
    28. SELECT @LSpaceUsed = (FILEPROPERTY (@filenameL,'SpaceUsed ') * 8192.0)/1024/1024
    29. SET @LSpaceUsed = @LSpaceUsed + (@LogFileFreeSpaceGB * 1000)
    30. SET @sStr = 'DBCC ShrinkFile (' + @fileNameL + ',' + Convert(VARCHAR(50),@LSpaceUsed) + ')'
    31. EXEC (@sStr)
    32.  
    33. SET @sStr = 'ALTER DATABASE ' + @DBName + ' SET RECOVERY FULL'
    34. EXEC (@sStr)
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  7. #7

    Thread Starter
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Shrink DB (SQL Server 2005)

    Found the answer in Books On-Line. I need to Rebuild the Primary Key index. Once I did that I was able to use the script above to reclaim the space.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width