Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change sqlite file size after "DELETE FROM table"

Tags:

sqlite

I am working with sqlite3 file.

First, I entered relatively big database, file size was about 100 mb.

Than I made

$db->exec("DELETE FROM table"); 

and entered just a small part of that database. But file size remained 100 mb.

What should you do to change sqlite file size when deleting it's content?

like image 551
Qiao Avatar asked Jan 27 '10 00:01

Qiao


People also ask

How do I reduce the size of my SQLite database?

Try to zip it and use zipinput stream while unpacking your database from assets. Anyway, android will zip your 14mb database when you create your apk anyway (which will be unzipped during install), so I guess your apk will be around 5-6mb in size. Android will zip automatically when creating/exporting apk file?

How can you delete the existing records from a table in SQLite?

SQLite DELETE query is used to remove existing records from a specified table. You can use the WHERE clause with DELETE queries to delete the selected rows. You have to write a table name after the DELETE FROM clause, from which you want to delete records.

What does VACUUM do in SQLite?

The VACUUM command works by copying the contents of the database into a temporary database file and then overwriting the original with the contents of the temporary file.

Does sqlite3 compress data?

Extension for sqlite that provides transparent dictionary-based row-level compression for sqlite. This basically allows you to compress entries in a sqlite database almost as well as if you were compressing the whole DB file, but while retaining random access.


2 Answers

The command you are looking for is vacuum. There is also a pragma to turn auto-vacuuming on.

From the documentation:

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 151
Trent Avatar answered Sep 20 '22 07:09

Trent


You can do this

$db->exec("DELETE FROM table"); $db->exec("vacuum"); 

and the file size will be changed.

like image 36
kylinking Avatar answered Sep 18 '22 07:09

kylinking