Ok so i've been banging my head against the wall for couple of days now and i finally came to conclusion.
I am trying to insert one record only when i need to, my response time was pretty slow, around 120ms per record. I've seen most of the threads here on SO about optimising insert statements and i implemented most of the recommended practices; using transaction, using prepared statements etc.
Logging and testing showed me that most of the delay is coming from the basic step "databaseHelper.getWritableDatabase()" it takes around 90ms to get writable database.
Is there any acceptable solution for this problem?
This is my method for inserting:
public static void insertEBlago3(Context context, EItemInvoiceDto itemDto, EParamDto eparamDto) throws Exception {
long startTime = System.currentTimeMillis();
DatabaseHelper databaseHelper = new DatabaseHelper(context);
SQLiteDatabase database = null;
try {
Log.v("Dodaj", "get writable database " + (System.currentTimeMillis() - startTime));
database = databaseHelper.getWritableDatabase();
// Begin transaction.
Log.v("Dodaj", "begin transaction " + (System.currentTimeMillis() - startTime));
database.beginTransaction();
Log.v("Dodaj", "Preparing sql " + (System.currentTimeMillis() - startTime));
String sqlInsertEBlago3 = "INSERT INTO emaloprod3 (leto, gd, strmst, zaporedna, datdok, casdok, idlokacije, blago, kolicina, "
+ "osnova, cena, ddv, popust, ncena, rabat, marza, narocilo, naroc, naroc_kol, opomba) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
Log.v("Dodaj", "Before compile " + (System.currentTimeMillis() - startTime));
SQLiteStatement stat = database.compileStatement(sqlInsertEBlago3);
Log.v("Dodaj", "After compile " + (System.currentTimeMillis() - startTime));
/***binding data****/
// End transaction.
stat.executeInsert();
Log.v("Dodaj", "Insert " + (System.currentTimeMillis() - startTime));
database.setTransactionSuccessful();
} catch (Exception e) {
throw e;
// Log.e(TAG, e.getMessage(), e);
// e.printStackTrace();
} finally {
// End transaction.
database.endTransaction();
DBHelper.closeAllConnections(null, null, database);
Log.v("Dodaj", "Finish " + (System.currentTimeMillis() - startTime));
}
}
And this is my log:
10-21 10:12:27.278 3759-3759/? V/Dodaj: get writable database 0
10-21 10:12:27.370 3759-3759/? V/Dodaj: begin transaction 92
10-21 10:12:27.370 3759-3759/? V/Dodaj: Preparing sql 92
10-21 10:12:27.370 3759-3759/? V/Dodaj: Before compile 92
10-21 10:12:27.370 3759-3759/? V/Dodaj: After compile 92
10-21 10:12:27.384 3759-3759/? V/Dodaj: Insert 106
10-21 10:12:27.399 3759-3759/? V/Dodaj: Finish 121
As you can see getting writable database takes 92 ms
Aucun commentaire:
Enregistrer un commentaire