Android App becomes very slow after adding SQLCipher

We integrate the SqlCiper SQLite DB into our existing Android project. What we found out that there is some big difference in DB query performance in unencrypted SqlCiper SQLite DB and encrypted SqlCiper SQLite DB. And my app became unbelievably slow. It takes ages to take click actions.

I added
implementation 'net.zetetic:android-database-sqlcipher:4.2.0@aar'

and My code for encryption

private void encryptDataBase(String passphrase) throws IOException {

        File originalFile = mContext.getDatabasePath(mName);

        File newFile = File.createTempFile("sqlcipherutils", "tmp", mContext.getCacheDir());

        String path = mContext.getDatabasePath(mName).getPath();
        SQLiteDatabase existing_db = SQLiteDatabase.openDatabase(path,"", null, SQLiteDatabase.OPEN_READWRITE);
        
        existing_db.rawExecSQL("ATTACH DATABASE '" + newFile.getPath() + "' AS encrypted KEY '" + passphrase + "';");
        existing_db.rawExecSQL("SELECT sqlcipher_export('encrypted');");
        existing_db.rawExecSQL("DETACH DATABASE encrypted;");

        existing_db.close();

        originalFile.delete();

        newFile.renameTo(originalFile);

    }

Hello @sushil_kumar - thanks for getting in touch about SQLCipher. I would recommend that you take a look at this post related to performance optimization when using SQLCipher:

Hi, @sjlombardo thanks for the reply.

I’m using the singleton database connection. but still, my app is slow.
Also one more thing sometimes app became automatically fast. what is the strange issue?

Hello @sushil_kumar. I can’t really say what would cause your application to have significant varying times. It may have to do with caching or the types of queries you are using. If you’d like to debug further it might be worthwhile to try using PRAGMA cipher_profile to look for particularly long running queries. Have you tried any of the other easy performance suggestions, like using transactions or adjusting the memory security settings?

Hi @sjlombardo. I solved this issue. I did some Optimization in open DB connection. open DB connection was taking time.

Thanks for response :slightly_smiling_face:

One way the performance with SQLCipher differs significantly from the default Android SQLite API is with concurrent database operations. SQLCipher’s Android API has a locking behavior that has been improved in the default API since API 16 (Jelly Bean). More details here: Android API Update

@Jeff_Lockhart - I believe that in this case the OP’s issue was that the procedure used to open a DB connection was repeatedly performing key derivation. This is the number one cause of poor performance with SQLCipher. Many applications using SQLite assume that opening a DB connection is a fast operation, and that is not the case with SQLCipher.

As a result SQLCipher’s locking behavior is not relevant to this user’s issue (or to be honest, the vast majority of the performance problems that people report). We definitely recognize and share your interest in improving locking. We also understand that your application is already optimized enough that concurrently issues are the main performance block. However, for most applications, like the one in this thread, the items in the SQLCipher Performance Optimization FAQ are most appropriate as a first line of investigation.