Now I write this:

public boolean update(List<MyObject> list){ ContentValues cv = new ContentValues(); try { db.beginTransaction(); for(int i = 0; i < carlist.size(); i++) { cv.put(DataBaseConstants.ID, list.get(i).id); cv.put(DataBaseConstants.NUMBER, list.get(i).attributes.Number); cv.put(DataBaseConstants.MODEL, list.get(i).attributes.model); cv.put(DataBaseConstants.ER, list.get(i).attributes.er); cv.put(DataBaseConstants.IN, list.get(i).attributes.in); cv.put(DataBaseConstants.HIDDEN, list.get(i).attributes.Hidden); //еще несколько строк cv.put if (checkExistItem(db, DataBaseConstants.TABLE_NAME, DataBaseConstants.ID + "=?", new String[]{String.valueOf(list.get(i).id)})) { db.update(DataBaseConstants.TABLE_NAME, cv, DataBaseConstants.ID + "=?", new String[]{String.valueOf(list.get(i).id)}); } else { db.insert(DataBaseConstants.TABLE_NAME, null, cv); } updateType(db, list.get(i).id, list.get(i).ships.Type); updateState(db, list.get(i).id, list.get(i).ships.State); cv.clear(); } db.setTransactionSuccessful(); db.endTransaction(); Log.d("databaseSpeed","end "+new Date().toString()); return true; } catch (Exception e){ e.printStackTrace(); return false; } } public boolean checkExistItem(SQLiteDatabase db, String tableName, String where, String[] whereParams ){ Cursor mCursor = db.rawQuery("SELECT "+DataBaseConstants.ANDROID_TABLE_ID+" FROM " + tableName + " WHERE "+ where, whereParams); if (mCursor != null && mCursor.getCount() > 0){ mCursor.close(); return true; } else { mCursor.close(); return false; } } 

A list of more than 2000 entries, recorded a few seconds.

The update method has 100 entries. In these 100 entries, there are still nested sheets that go into the corresponding updateType and updateState methods . The sheet is transferred and the record takes a second or 2. In general, it takes a very long time. Is it possible to speed up?

    1 answer 1

    Your code has at least a quadratic complexity (although I think that it is still n lg n). The problem is that you make a select for each iteration in order to find out if there is a specified record.

    Since this code is typical, there is a ready solution to the problem - updateWithOnConflict .

    Most likely these lines

     if (checkExistItem(db, DataBaseConstants.TABLE_NAME, DataBaseConstants.ID + "=?", new String[]{String.valueOf(list.get(i).id)})) { db.update(DataBaseConstants.TABLE_NAME, cv, DataBaseConstants.ID + "=?", new String[]{String.valueOf(list.get(i).id)}); } else { db.insert(DataBaseConstants.TABLE_NAME, null, cv); } 

    will need to change to

     db.updateWithOnConflict(DataBaseConstants.TABLE_NAME, cv, DataBaseConstants.ID + "=?", new String[]{String.valueOf(list.get(i).id)}, CONFLICT_REPLACE); 

    But you know better - you know your logic better. There is a insertWithOnConflict, maybe it will be better.

    • Does insertWithOnConflict and updateWithOnConflict perform the same function? with key CONFLICT_REPLACE. Both the first and second methods will add a record if there is no record, and if there is, they will update it ..? - DuosDuo
    • Even as an option, you can make the field unique when creating a column and replace it with a conflict. Column_name integer UNIQUE ON CONFLICT REPLACE and then just insert. By removing checkExistItem checks with the same number of entries, I won about 10 seconds. I think if you use SQLiteStatement instead of ContentValues, then it will be possible for another couple of seconds. to win - DuosDuo