Search code examples
androidsqliteandroid-sqlitesqliteopenhelper

Android Improve SQLite Performance?


Can I have Insert query faster than bellow code :

public void InsertFast2(List<Marketing_Points_B> values) {

    String sql = "INSERT INTO " + tableName2 + " ( Count, Date, Time, Lat, Lng, UserCode, LatLng ) VALUES ( ?, ?, ?, ?, ?, ?, ? )";

    SQLiteDatabase db = this.getWritableDatabase();
    /*?*/db.execSQL("PRAGMA synchronous=OFF");
    db.beginTransactionNonExclusive();

    SQLiteStatement stmt = db.compileStatement(sql);

    for (int i = 0; i < values.size(); i++) {
        stmt.bindString(1, values.get(i).getCounts());
        stmt.bindString(2, values.get(i).getDate());
        stmt.bindString(3, values.get(i).getTime());
        stmt.bindString(4, String.valueOf(values.get(i).getLat()));
        stmt.bindString(5, String.valueOf(values.get(i).getLng()));
        stmt.bindString(6, values.get(i).getUserCode());
        stmt.bindString(7, String.valueOf(values.get(i).getmPosition()));
        stmt.execute();
        stmt.clearBindings();
    }

    db.setTransactionSuccessful();
    db.endTransaction();
    /*?*/db.execSQL("PRAGMA synchronous=NORMAL");
    db.close();
}

And what is (Can I use from these)?

db.execSQL("PRAGMA synchronous=OFF");

db.execSQL("PRAGMA synchronous=NORMAL");

Solution

  • You cannot go faster, except by shipping a complete database so that you would not have to do any inserts at all.

    The PRAGMA statements are documented in the documentation. The synchronous setting trades off speed against safety.