# SQLCipher Performance Optimization

**URL:** <https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14>\
**Category:** SQLCipher FAQ\
**Created:** [July 23, 2014, 1:33pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14 "2014-07-23T13:33:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [July 23, 2014, 1:33pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/1 "2014-07-23T13:33:02Z")

</div>

### Optimization Guidelines

The latest guidance has been moved to the [SQLCipher Performance Optimization](https://www.zetetic.net/sqlcipher/performance/) documentation. This topic has been archived to preserve discussion.

---

<div class="post-metadata">

**Author:** ![Rohan\_Patel](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Rohan\_Patel](https://discuss.zetetic.net/u/Rohan_Patel)\
**Post date:** [September 26, 2014, 9:57am UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/2 "2014-09-26T09:57:37Z")

</div>

I am using a singleton object to perform database operations. I made bulk insertion demo and making insertion by both SQLCipher library with empty encryption key("") & DataBaseHelper.

I observed that DataBaseHelper taking less time than SQLCipher for bulk insertions.

Can you please elaborate what extra things doing with SQLCipher at time of db operations. Is there any way to overcome this issue?

Note: I have 4 databases, 2 out of 4 are needs encryption and other 2 doesn’t need encryption.  
But i am using common access helper class for use Databases.  
Where encryption doesn’t required, I pass ("") empty string as an argument to open database. Is it correct way of use SQLCipher for non-encrypted database. Whats your suggestion for this kind of requirement?

After examines both SQLCipher and android default DatabaseHelper, i come up with one conclusion is that, SQLCipher takes sightly more time than DataBaseHelper for db operations. Is it right?

Kindly, suggest me solutions.

Thanks

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [September 26, 2014, 3:30pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/3 "2014-09-26T15:30:26Z")

</div>

Hello @Rohan_Patel

SQLCipher is doing more that the default SQLite implementation in that it is encrypting the contents of your data as you perform inserts. In terms of your specific slow down, it would be difficult to discern the cause without further information. Have you tried the recommendations above to identify the timing of the specific queries you are executing on the database?

---

<div class="post-metadata">

**Author:** ![Rohan\_Patel](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Rohan\_Patel](https://discuss.zetetic.net/u/Rohan_Patel)\
**Post date:** [September 29, 2014, 4:27am UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/4 "2014-09-29T04:27:36Z")

</div>

I have also performed single record insertion with both. DatabaseBaseHelper takes 11-19 milliseconds and SQLCipher takes 21-29 milliseconds to execute query with opening db.

I performed this action several times, in each time SQLCipher looks more expensive compare to DataBaseHelper.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [September 29, 2014, 2:26pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/5 "2014-09-29T14:26:12Z")

</div>

Hello @Rohan_Patel

Can you share the schema and sample data you are using for your timings? Have you tried running this type of scenario as a test through the SQLCipher for Android [test suite](https://github.com/sqlcipher/sqlcipher-android-tests)?

---

<div class="post-metadata">

**Author:** ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)\
**Post date:** [September 30, 2014, 1:44pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/6 "2014-09-30T13:44:17Z")

</div>

@Rohan_Patel SQLCipher does have extra overhead for encryption. It would be impossible for the library to perform all of its operations with exactly the same performance as standard SQLite. That said, you mentioned that you are seeing poor performance on bulk inserts. I have a few comments and questions:

1. Are you starting the measurement after the database has been opened? Opening the database is a deliberately slow process due to key derivation, as mentioned earlier.

2. When doing inserts, are you batching up multiple inserts into a single transaction? If not, you will see much poorer performance. Be sure to wrap the inserts into a transaction.

---

<div class="post-metadata">

**Author:** ![sharmasneha](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@sharmasneha](https://discuss.zetetic.net/u/sharmasneha)\
**Post date:** [February 1, 2016, 5:28pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/7 "2016-02-01T17:28:06Z")

</div>

Hi Nick,  
Thanks for sharing that. Could you please elaborate point 2 and 4.

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [February 1, 2016, 5:57pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/8 "2016-02-01T17:57:07Z")

</div>

Hello @sharmasneha

If you are new to transactions within SQLite as referenced in item 2, I would recommend reading both [this](https://www.sqlite.org/transactional.html) page and [this](https://www.sqlite.org/atomiccommit.html) page for more details.

With regard to indexes covered in item 4, I would recommend reviewing [this](https://www.sqlite.org/queryplanner.html) page.

---

<div class="post-metadata">

**Author:** ![sharmasneha](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@sharmasneha](https://discuss.zetetic.net/u/sharmasneha)\
**Post date:** [March 14, 2016, 3:43pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/9 "2016-03-14T15:43:12Z")

</div>

Hi Nick, Thanks for your help. It worked.

---

<div class="post-metadata">

**Author:** ![Maverick2805](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@Maverick2805](https://discuss.zetetic.net/u/Maverick2805)\
**Post date:** [March 5, 2019, 11:49am UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/10 "2019-03-05T11:49:30Z")

</div>

I know this topic is quite old. However I have wonder if I understand the operation of sqlcipher correctly.  
When the connection to the database is opened and the key derivation has been done, the content of the database is still not readable through a hex dump, correct?  
I wasn’t sure about that and to protect the data, I opened and closed the database connection with every query, which of course results in very slow performance.

Thanks for your help.

Cheers Mav

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [March 5, 2019, 2:32pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/11 "2019-03-05T14:32:30Z")

</div>

Hello @Maverick2805

> [@Maverick2805](#):
>
> When the connection to the database is opened and the key derivation has been done, the content of the database is still not readable through a hex dump, correct?

Correct, the content that is written to disk is encrypted (when operating on a SQLCipher encrypted database). When a SQL query is executed, SQLCipher will read pages within the database and decrypt the content in memory to resolve the query.

> [@Maverick2805](#):
>
> I wasn’t sure about that and to protect the data, I opened and closed the database connection with every query, which of course results in very slow performance.

If possible, it is better to maintain an open connection for each query to avoid the time cost associated with the key derivation process.

---

<div class="post-metadata">

**Author:** ![Maverick2805](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@Maverick2805](https://discuss.zetetic.net/u/Maverick2805)\
**Post date:** [March 5, 2019, 8:56pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/12 "2019-03-05T20:56:31Z")

</div>

Thank you very much, exactly what I needed to know. Like that I rewrote the application again to use the database as a singleton and only open it once, but still have the data encrypted.  
Cheers  
Mav

---

<div class="post-metadata">

**Author:** ![developernotes](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/developernotes/32/1309_2.png) [@developernotes](https://discuss.zetetic.net/u/developernotes)\
**Post date:** [October 2, 2019, 1:40pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/13 "2019-10-02T13:40:12Z")

</div>

A post was split to a new topic: [What is the purpose of PRAGMA cipher\_memory\_security?](https://discuss.zetetic.net/t/what-is-the-purpose-of-pragma-cipher-memory-security/3953)

---

<div class="post-metadata">

**Author:** ![marcardar](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/marcardar/32/1524_2.png) [@marcardar](https://discuss.zetetic.net/u/marcardar)\
**Post date:** [December 22, 2020, 12:10pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/14 "2020-12-22T12:10:41Z")

</div>

I’ve just switched from the Android platform SQLite to sqlcipher but using null passphrase (as a first step in migration) and noticed a major slowdown in populating databases. I was expecting the points mentioned above to be only relevant when using a passphrase, but I guess not. Which of the above points are relevant when using null passphrase?

UPDATE: the `PRAGMA cipher_memory_security = OFF` brings the speed back to a reasonable level.

---

<div class="post-metadata">

**Author:** ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)\
**Post date:** [December 22, 2020, 1:52pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/15 "2020-12-22T13:52:24Z")

</div>

@marcardar - yes, the memory security feature hooks into the memory allocation system so it will operate even with unencrypted databases. Setting `PRAGMA cipher_memory_security = OFF;` should remove that performance impact.

---

<div class="post-metadata">

**Author:** ![marcardar](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/marcardar/32/1524_2.png) [@marcardar](https://discuss.zetetic.net/u/marcardar)\
**Post date:** [December 23, 2020, 2:38am UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/16 "2020-12-23T02:38:05Z")

</div>

@sjlombardo understood, thanks. Would it be better for it to be the automatic default when there is no encryption?

---

<div class="post-metadata">

**Author:** ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)\
**Post date:** [December 24, 2020, 3:01pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/17 "2020-12-24T15:01:15Z")

</div>

Hi @marcardar, I totally agree that would be nice, but unfortunately the memory management system is low level and global to the library. Thus the memory security can’t be applied separately for non-encrypted connections. If it was off by default, or only turned it on after an encrypted database was opened, there could be edge cases in which memory segments could be treated inconsistently. As a result we opted to enable it by default.

---

<div class="post-metadata">

**Author:** ![marcardar](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/marcardar/32/1524_2.png) [@marcardar](https://discuss.zetetic.net/u/marcardar)\
**Post date:** [March 14, 2021, 2:01am UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/18 "2021-03-14T02:01:36Z")

</div>

Is there any performance benefit of using `PRAGMA cipher_use_hmac = OFF`? Or is it only necessary for when opening legacy databases (or databases created with the same `PRAGMA`)?

---

<div class="post-metadata">

**Author:** ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)\
**Post date:** [January 14, 2022, 5:08pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/19 "2022-01-14T17:08:12Z")

</div>



---

<div class="post-metadata">

**Author:** ![sjlombardo](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.zetetic.net/sjlombardo/32/3_2.png) [@sjlombardo](https://discuss.zetetic.net/u/sjlombardo)\
**Post date:** [January 14, 2022, 5:08pm UTC](https://discuss.zetetic.net/t/sqlcipher-performance-optimization/14/20 "2022-01-14T17:08:22Z")

</div>


