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?
7 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...
-
13run this: `sqlite3 /path/to/your/db/foo.db 'VACUUM;'` – Tono Nam Jul 30 '19 at 08:43
-
8You can run `VACUUM;` directly in the query window – IAmGroot Jan 06 '21 at 08:10
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.

- 60,510
- 8
- 78
- 126
14mb is very large for an Android app still, it's better if you can arrange for it to be downloaded from a server and integrated into the app.
You might instead bundle the app with the database file compressed (i.e. a .zip file) and uncompress it when the app is first run (remembering to delete the .zip file). Best to do all of this on an SD card.

- 30,811
- 34
- 116
- 155
-
Can you provide me (links or coding) how to zip and unzip DB file when the application is first run? – PPShein Oct 07 '11 at 03:24
-
1I learned how to do this from the Android docs. A Google search, or a search here, gives you http://stackoverflow.com/questions/3382996/how-to-unzip-files-programmatically-in-android – Ken Oct 07 '11 at 06:20
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.

- 4,875
- 3
- 25
- 30
-
3
-
ya it does. my app had a 12 mb database in assets. On creating an apk, the apj=k file size was just 4mb. i think i can even reduce it further if i use zip files inside assets. – rDroid Oct 11 '11 at 04:22
-
as PPShein mentioned android will zip database but you can using 7zip. then by this library unpack it: https://github.com/hzy3774/AndroidUn7zip – Hadi Ahmadi Nov 28 '19 at 11:49
I've had an experience with sqlite where I imported about 3mb of data, after which I realized that the data had encoding problems. I dropped the table, re-made it, and re-imported the data after fixing the encoding problems. Result: a 6mb sqlite file. My solution was to delete the sqlite db and just make a new one. (Without more details on what you're storing in your db, this is all I can offer...)
-
-
The empty database after dropping the initial 3mb table stayed at 3mb, and adding new data added another 3. – Oct 07 '11 at 04:31
-
14MB is not that large for an app nowadays. Another thing you can try is remove unnecessary table indexes. Some indexes might be lurking in your tables, and they can take up a lot of space. You might have to do your queries differently.

- 48,840
- 22
- 240
- 204