Rangers4me
Rangers4me

Reputation: 177

Compressing Encrypted SQLite FTS3 Database for iOS

My app uses a SQLite database with FTS3 to do product search. I am trying to encrypt the database using SQLCipher but it causes the database size to balloon (7mb --> ~20mb).

It doesn't look like there is a great way to compress an encrypted FTS SQLite database. Does anyone have a suggestion on this? I have to shrink it in order to keep the app under the 20mb 3g download limit.

Thanks!

Upvotes: 3

Views: 979

Answers (1)

Stephen Lombardo
Stephen Lombardo

Reputation: 1523

There is no reason that a database using SQLCipher would be that large in comparison to one without SQLCipher. Each page of the SQLCipher database only uses 48 bytes of the page for IV and HMAC. Thus with a 1024 byte page size, if you had a 7mb unencrypted database, the SQLCipher encrypted version should be only 7.34 mb.

A far more likely explanation is that your database has grown as a result of deletes and inserts. If auto vacuum is not enabled on the database, deleted rows can continue to take up space. Try running VACUUM on the database to reclaim space.

If vacuuming the database doesn't take care of it, here are a few follow-up questions;

  1. Are you sure that the FTS3 indexes are not simply responsible for the the file size?
  2. Have you tried creating the same exact schema and table structure with FTS3 in a non-encrypted database to compare file sizes to make sure that this is actually a problem with SQLCipher? Is the non-encrypted database with FTS3 7 MB?
  3. If the answers to 1 and 2 are both yes, can you please provide exact steps for how you are converting from the unencrypted to the encrypted database?

To answer your general question, a SQLCipher database is probably not going to compress well. If the file size growth is actually a result of FTS3 and not SQLCipher, then you could consider shipping the data on the app and then building the FTS3 virtual table on the device.

Upvotes: 1

Related Questions