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: