Search code examples
sql-serversql-server-2008shrink

SQL Server database file not being truncated


I have a database which is ~4GB in size. I've copied that database and deleted 99% of the data on it because I need a database with only the schema and basic data (mostly static data is kept).

The problem now is that the MDF file still is ~4GB in size. If I read the size of the tables (using this, for example), they sum less than 20 MB all together. The log file is already shrunk, but none of the scripts I ran worked for shrinking the DB file.

Note: I usually don't do this, but this time I need to shrink the database (I know it's not recommended)

Edit: +Useful info

Command:

exec sp_spaceused

Output:

database_name       database_size   unallocated_space
AccudemiaEmptyDb    3648.38 MB      4.21 MB

Command:

select object_name(id) as objname, SUM(dpages*8) as dpages, COUNT(*) as cnt
from sysindexes
group by id
order by dpages desc

Output:

object_name(id)            sum(dpages*8)    count(*)
sysdercv                   675328           1
sysxmitqueue               359776           1
sysdesend                  72216            1
sysconvgroup               47704            1
sysobjvalues               4760             5
sec_OperationAccessRule    3472             5
sec_PageAccessRule         2232             5
syscolpars                 656              11
AuditObjects               624              2
sysmultiobjrefs            408              5
HelpPage                   376              8
sysschobjs                 352              9
syssoftobjrefs             328              7
sysidxstats                272              10
sysrscols                  200              1
Translation                160              3
sysallocunits              128              3
sysiscols                  128              8
syssingleobjrefs           96               5
sysrowsets                 80               4

Solution

  • Thank you guys, and mainly Richard for all the information!

    To fix the problem, I had to drop and recreate my SERVICES:

    DROP SERVICE [//Audit/DataWriter] 
    GO
    
    CREATE SERVICE [//Audit/DataWriter] 
        AUTHORIZATION dbo 
    ON QUEUE dbo.TargetAuditQueue ([//Audit/Contract])
    

    Once I did that, the database was 5GB! But this time the second query I put in my question, showed sysxmitqueue as first result. Digging a bit more on Internet, I was able to purge the big table doing so:

    ALTER DATABASE [your_database] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
    GO
    ALTER DATABASE [your_database] SET NEW_BROKER WITH ROLLBACK IMMEDIATE
    GO
    ALTER DATABASE [your_database] SET MULTI_USER
    GO
    

    Then, run DBCC SHRINKFILE and that's all!! =) It's now only 40MB

    Thank you guys!