Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting column doesn't reduce database size

I have a test database with 1 table having some 50 million records. The table initially had 50 columns. The table has no indexes. When I execute the "sp_spaceused" procedure I get "24733.88 MB" as result. Now to reduce the size of this database, I remove 15 columns (mostly int columns) and run the "sp_spaceused", I still get "24733.88 MB" as result.

Why is the database size not reducing after removing so many columns? Am I missing anything here?

Edit: I have tried database shrinking but it didn't help either

like image 988
Vinod Avatar asked Jan 24 '13 08:01

Vinod


People also ask

How do I reduce the size of my database after deleting records?

Deleting rows in a database will not decrease the actual database file size. You need to compact the database after row deletion. Look for this After running this, you'll want to rebuild indexes. Shrinking typically causes index fragmentation, and that could be a significant performance cost.

Does dropping a column delete the data?

When you drop the column from the table, all of the data is deleted also! This can't be undone! If the column is a primary key or a foreign key to another table, it cannot be deleted.

How do I shrink a SQL database after deleting data?

Use SQL Server Management StudioPoint to Tasks, point to Shrink, and then select Database.

When I delete data from a table does SQL Server reduce the size of table?

In MS SQL server, when you delete data from the database, the database file (. MDF) does not automatically become smaller. This is by design. Increasing the size of the database then shrinking all the time is inefficient.


3 Answers

Try running the following command:

DBCC CLEANTABLE ('dbname', 'yourTable', 0)

It will free space from dropped variable-length columns in tables or indexed views. More information here DBCC CLEANTABLE and here Space used does not get changed after dropping a column

Also, as correctly pointed out on the link posted on the first comment to this answer. After you've executed the DBCC CLEANTABLE command, you need to REBUILD your clustered index in case the table has one, in order to get back the space.

ALTER INDEX IndexName ON YourTable REBUILD
like image 116
Yaroslav Avatar answered Oct 17 '22 19:10

Yaroslav


When any variable length column is dropped from table, it does not reduce the size of table. Table size stays the same till Indexes are reorganized or rebuild.

There is also DBCC command DBCC CLEANTABLE, which can be used to reclaim any space previously occupied with variable length columns. Here is the syntax:

DBCC CLEANTABLE ('MyDatabase','MySchema.MyTable', 0)WITH NO_INFOMSGS;
GO

Raj

like image 45
Raj Avatar answered Oct 17 '22 18:10

Raj


The database size will not shrink simply because you have deleted objects. The database server usually holds the reclaimed space to be used for subsequent data inserts or new objects.

To reclaim the space freed, you have to shrink the database file. See How do I shrink my SQL Server Database?

like image 1
Matt Avatar answered Oct 17 '22 19:10

Matt