Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does clearing an SQLite database not reduce its size?

Tags:

sqlite

I have an SQLite database.

I created the tables and filled them with a considerable amount of data.

Then I cleared the database by deleting and recreating the tables. I confirmed that all the data had been removed and the tables were empty by looking at them using SQLite Administrator.

The problem is that the size of the database file (*.db3) remained the same after it had been cleared.

This is of course not desirable as I would like to regain the space that was taken up by the data once I clear it.

Did anyone make a similar observation and/or know what is going on?

What can be done about it?

like image 816
Thorsten Lorenz Avatar asked Mar 30 '10 11:03

Thorsten Lorenz


2 Answers

From here:

When an object (table, index, trigger, or view) is dropped from the database, it leaves behind empty space. This empty space will be reused the next time new information is added to the database. But in the meantime, the database file might be larger than strictly necessary. Also, frequent inserts, updates, and deletes can cause the information in the database to become fragmented - scrattered out all across the database file rather than clustered together in one place.

The VACUUM command cleans the main database by copying its contents to a temporary database file and reloading the original database file from the copy. This eliminates free pages, aligns table data to be contiguous, and otherwise cleans up the database file structure.

like image 168
Aseem Gautam Avatar answered Oct 21 '22 11:10

Aseem Gautam


Databases sizes work like water marks e.g. if the water rises the water mark goes up, when the water receeds the water mark stays where it was

You should look into shrinking databases

like image 3
Nick Allen Avatar answered Oct 21 '22 11:10

Nick Allen