Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to shrink sqlite database?

Once done storing all of my data into sqlite, it reach 14 MB already. That's why I'm afraid that some people cannot download my application from connection slow area. Is there any way to shrink sqlite database?

like image 701
PPShein Avatar asked Oct 07 '11 02:10

PPShein


People also ask

How do I limit the size of a SQLite database?

The maximum number of attached databases can be lowered at run-time using the sqlite3_limit(db,SQLITE_LIMIT_ATTACHED,size) interface. SQLite is able to limit the size of a database file to prevent the database file from growing too large and consuming too much disk space.

How does SQLite VACUUM work?

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. When overwriting the original, a rollback journal or write-ahead log WAL file is used just as it would be for any other database transaction.

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.

How do I truncate a table in SQLite?

SQLite does not have an explicit TRUNCATE TABLE command like other databases. Instead, it has added a TRUNCATE optimizer to the DELETE statement. To truncate a table in SQLite, you just need to execute a DELETE statement without a WHERE clause. The TRUNCATE optimizer handles the rest.


2 Answers

Have you tried VACUUM?

The VACUUM command rebuilds the entire database. There are several reasons an application might do this:

  • Unless SQLite is running in "auto_vacuum=FULL" mode, when a large amount of data is deleted from the database file it leaves behind empty space, or "free" database pages. This means the database file might be larger than strictly necessary. Running VACUUM to rebuild the database reclaims this space and reduces the size of the database file.
  • Frequent inserts, updates, and deletes can cause the database file to become fragmented - where data for a single table or index is scattered around the database file. Running VACUUM ensures that each table and index is largely stored contiguously within the database file. In some cases, VACUUM may also reduce the number of partially filled pages in the database, reducing the size of the database file further...

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. When overwriting the original, a rollback journal or write-ahead log WAL file is used just as it would be for any other database transaction. This means that when VACUUMing a database, as much as twice the size of the original database file is required in free disk space...

like image 96
Hot Licks Avatar answered Sep 28 '22 11:09

Hot Licks


a few hints:

  • SQLite is somewhat lazy to reclaim unused space; use the VACUUM command or auto vacuum mode.

  • the datbase format trades space efficiency for speedy access. if you just dump the data contents, either as SQL dump or simply a CVS file for each database table, you'll likely get smaller files.

  • either using databse files or plain data, try compressing them.

like image 23
Javier Avatar answered Sep 28 '22 12:09

Javier