Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MDF file size much larger than actual data

For some reason my MDF file is 154gigs, however, I only loaded 7 gigs worth of data from flat files. Why is the MDF file so much larger than the actual source data?

More info:

Only a few tables with ~25 million rows. No large varchar fields (biggest is 300, most are less than varchar(50). Not very wide tables < 20 columns. Also, none of the large tables are indexed yet. Tables with indexes have less than 1 million rows. I don't use char, only varchar for strings. Datatype is not the issue.

Turned out it was the log file, not the mdf file. The MDF file is actually 24gigs which seems more reasonable, however still big IMHO.

UPDATE:

I fixed the problem with the LDF (log) file by changing the recovery model from FULL to simple. This is okay because this server is only used for internal development and ETL processing. In addition, before changing to SIMPLE I had to shrink the LOG file. Shrinking is not recommended in most cases, however, this was one of those cases where the log file should have never grown so big and so fast. For further reading see this

like image 222
O.O Avatar asked Aug 16 '10 23:08

O.O


People also ask

How do I reduce the size of an MDF file?

In the left pane where your databases are listed, right-click on the "SampleDataBase" and from the "Tasks" option select "Shrink" then "Files", as in the image below. On the next dialog box, make sure the File type is set to "Data" to shrink the mdf file.

What is the default size of MDF file?

The default initial size for both files are 8 MB. The initial size is not as important as Autogrowth.

What is the difference between MDF and LDF file?

mdf is the data file, and ldf is the log file. You should use the Attach database feature to get these into your SQL Server instance.

How can I increase MDF file size in SQL Server?

Using SQL Server Management StudioExpand Databases, right-click the database to increase, and then click Properties. In Database Properties, select the Files page. To increase the size of an existing file, increase the value in the Initial Size (MB) column for the file.


1 Answers

Because the MDF was allocated with 154Gb, or has grown to 154Gb through various operations. A database file has at least the size of the data in it, but it can be larger than the used amount by any amount.

An obvious question will be how do you measure the amount of data in the database? Did you use sp_spaceused? Did you check sys.allocation_units? Did you guess?

If the used size is indeed 7Gb out of 154Gb, then you should leave it as it is. The database was sized by somebody at this size, or has grown, and it is likely to grow back. If you believe that the growth or pre-sizing was accidental, then the previous point still applies and you should leave it as is.

If you are absolutely positive the overallocation is a mistake, you can shrink the database, with all the negative consequences of shrinking.

like image 105
Remus Rusanu Avatar answered Sep 27 '22 19:09

Remus Rusanu