Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

SQL- Shrinking Database taking forever

Options
  • 13-03-2014 12:02pm
    #1
    Banned (with Prison Access) Posts: 32,865 ✭✭✭✭


    Hi Guys, I'm currently shrinking a database in SSMS that's taking an age, and I was just wondering a: why it's taking so long, b: if there's anything I can do to speed it up & c: What I can do differently in future to stop it taking so long.

    The reason I'm doing this is the disk has a 400GB capacity and it's down to around 7GB available. The Database itself is 210GB with 70GB available, it's not a live DB so there are no apps dependent on it or anything, it's just used for reporting purposes.

    After 30 minutes of starting the process it was 69% complete, since then though I'm only at 80% complete, elapsed time is 22 hours thus far, CPU time 3 hours. Estimated completed time is 5 hours and RISING.

    MS Windows Server 2003 Enterprise Edition, Xeon CPU 2.4GHz, 7.75GB RAM.


Comments

  • Banned (with Prison Access) Posts: 32,865 ✭✭✭✭MagicMarker


    Got this sorted, for anyone who is interested, I cancelled the DbccFilesCompact process and instead ran the following script which shrinks the db in increments. It's only been running 45 minutes and already freed up about 50GB of disk space.


    -- Shrink_DB_File.sql
    /*
    This script is used to shrink a database file in
    increments until it reaches a target free space limit.

    Run this script in the database with the file to be shrunk.
    1. Set @DBFileName to the name of database file to shrink.
    2. Set @TargetFreeMB to the desired file free space in MB after shrink.
    3. Set @ShrinkIncrementMB to the increment to shrink file by in MB
    4. Run the script
    */

    declare @DBFileName sysname
    declare @TargetFreeMB int
    declare @ShrinkIncrementMB int

    -- Set Name of Database file to shrink
    set @DBFileName = 'MyDatabaseFileName'

    -- Set Desired file free space in MB after shrink
    set @TargetFreeMB = 1000

    -- Set Increment to shrink file by in MB
    set @ShrinkIncrementMB = 50

    -- Show Size, Space Used, Unused Space, and Name of all database files
    select
    [FileSizeMB] =
    convert(numeric(10,2),round(a.size/128.,2)),
    [UsedSpaceMB] =
    convert(numeric(10,2),round(fileproperty( a.name,'SpaceUsed')/128.,2)) ,
    [UnusedSpaceMB] =
    convert(numeric(10,2),round((a.size-fileproperty( a.name,'SpaceUsed'))/128.,2)) ,
    [DBFileName] = a.name
    from
    sysfiles a

    declare @sql varchar(8000)
    declare @SizeMB int
    declare @UsedMB int

    -- Get current file size in MB
    select @SizeMB = size/128. from sysfiles where name = @DBFileName

    -- Get current space used in MB
    select @UsedMB = fileproperty( @DBFileName,'SpaceUsed')/128.

    select [StartFileSize] = @SizeMB, [StartUsedSpace] = @UsedMB, [DBFileName] = @DBFileName

    -- Loop until file at desired size
    while @SizeMB > @UsedMB+@TargetFreeMB+@ShrinkIncrementMB
    begin

    set @sql =
    'dbcc shrinkfile ( '+@DBFileName+', '+
    convert(varchar(20),@SizeMB-@ShrinkIncrementMB)+' ) '

    print 'Start ' + @sql
    print 'at '+convert(varchar(30),getdate(),121)

    exec ( @sql )

    print 'Done ' + @sql
    print 'at '+convert(varchar(30),getdate(),121)

    -- Get current file size in MB
    select @SizeMB = size/128. from sysfiles where name = @DBFileName

    -- Get current space used in MB
    select @UsedMB = fileproperty( @DBFileName,'SpaceUsed')/128.

    select [FileSize] = @SizeMB, [UsedSpace] = @UsedMB, [DBFileName] = @DBFileName

    end

    select [EndFileSize] = @SizeMB, [EndUsedSpace] = @UsedMB, [DBFileName] = @DBFileName

    -- Show Size, Space Used, Unused Space, and Name of all database files
    select
    [FileSizeMB] =
    convert(numeric(10,2),round(a.size/128.,2)),
    [UsedSpaceMB] =
    convert(numeric(10,2),round(fileproperty( a.name,'SpaceUsed')/128.,2)) ,
    [UnusedSpaceMB] =
    convert(numeric(10,2),round((a.size-fileproperty( a.name,'SpaceUsed'))/128.,2)) ,
    [DBFileName] = a.name
    from
    sysfiles a


Advertisement