script to shrink db

  • can any one provide sql script for shrink db by small increment

  • (as posted over at SQLTeam)

    Why do you want to shrink? Databases tend to grow as more data gets put in them. It's in their nature.

    Shrinking causes massive fragmentation and will just result in the data file growing again next time data gets added. When that happens, the entire system will slow down as the file is expanded. Also repeated shrinks and grows will cause fragmentation at the file-system level, which is hard to fix.

    See - http://sqlinthewild.co.za/index.php/2007/09/08/shrinking-databases/

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • we have a PROD DB which is about 300Gig, however we are only using 50%.

    Shrinking the DB will probably have some slight benefit to our backup process, but more importantly will make it easier to restore the DB on local servers

    we need a script that will shrink a small amount of the DB, and schedule it to run every night so that we can see period decreases in size

  • Shrinking will do nothing for your backups. Backups only back up used space, not empty space.

    If you're going to shrink, do a once-off shrink, then rebuild all your indexes to fix what the shrink did to them. Shrinking a little every night is a really bad idea. You'll be causing massive index fragmentation every time, necessitating an index rebuild of every index every night.

    What I would suggest is a once-off shrink, shrink the DB to about 10% free, then leave it.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • I tend to agree with Gail, but I'd look over the last few backups. How large are they? How much are they growing? Each one should be in the 150GB range, but how much change is there? I try to grow databases only every few months, so I'd figure out how much space you need for the next xx months, leave that free, and then shrink to that level.

    As Gail mentioned, shrink once, not regularly.

  • we want to shrink it down from it's enormous size now

    i understand it will grow again

    we need to look at those numbers and start to pre-grow the file

    we have scheduled maintenance for defragging both at the db and disk (o/s) level

  • Use DBCC SHRINKFILE to reduce the size of the file. You need to leave free space immediately, as you want to reindex your tables as the shrink will fragment the indexes.

    Grow periodically, in big chunks.

  • dba_neo (8/9/2010)


    we want to shrink it down from it's enormous size now

    Do a once-off shrinkfile and shrink it down to around 170GB.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • thanks,but we need script to shrink it every night in small shunks as we already have a reorg/rebuild job running every night

  • Why do you think you need to shrink every night? I'd submit to you that you don't, and that you don't really understand the implications of doing that. you're also wasting resources.

  • Why? What does shrinking in small chunks each day give you that a once off shrink doesn't? (other than a massive waste of time and resources)

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • dba_neo (8/9/2010)


    thanks,but we need script to shrink it every night in small shunks as we already have a reorg/rebuild job running every night

    Isn't that also a waste of resources to do every night ?

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply