0

I have a this function below which is working fine:

I use it because in a synchronisation process I have to insert a lot of rows (sometimes more than 30 000 in the same sync) in different tables and for some of them, the value I recieve from my Json object is not the real one. So before inserting the row, I have to replace the value with another. That is why I use this function:

public void myFunction( String tableName, String attrName, ContentValues currentVal ) { 

    SQLiteDatabase database = this.getReadableDatabase();
    long realValue = -1;

    Cursor c = database.rawQuery("SELECT value FROM "+tableName+" WHERE "+tableName+".other_value = " + currentVal.getAsString(attrName), null);

    if( c.moveToFirst() ) { 
        realValue = c.getLong(0);
    }
    if( c != null ) c.close();

    currentVal.put( attrName, realValue );  

}

My problem is the Cursor... The c.moveToFirst() function affects the speed of the process.

I did researches and I found so many explanations like :

https://stackoverflow.com/a/15738831/5778152

I specify that I use indexes and I already try transactions.

So, what I wanted to do now (or to know) is: Is there a way to perform something like:

currentVal.put( attrName, SELECT value FROM "+tableName+" WHERE "+tableName+".other_value = " + currentVal.getAsString(attrName) );

Because with this trick I will not have to iterate into my Cursor (which is actually my problem) and when I will insert the content of the ContentValues it will automatically process the sub-query to retrieve the real value to insert.

I also thought about creating a temporary table. I insert the values founded in my Json object without changing them and when I've inserted all the values for the current table, I copy them into the real table unsing in the same time a sub-query to relpace the incorrect values by the correct ones.

Any ideas, suggestions, snippets, ... will be appreciated

13
  • The best approach I've tried inserting over 100,000 elements onto SQLite is using it through NDK layer, and using some simple native code to access and write onto DB which is quite fast in comparison to application layer writing. Commented Jan 19, 2016 at 13:18
  • Inserts do not take much time, the problem is in the replacement part because until now I didn't found how to go through this without using the cursor.moveToFirst() function Commented Jan 19, 2016 at 13:25
  • You must finally cursor.close() after every operation. Then reopen cursor object. Are you doing it? Commented Jan 19, 2016 at 13:34
  • Are you using the right indexes? Show the output of EXPLAIN QUERY PLAN for the SELECT query. Commented Jan 19, 2016 at 13:43
  • Yes, I'm using the right indexes. In my question I said it was for 30 000 rows but this amount of rows is almost the same for each tables (about 15). So the sync operation is taking between 40min to 2hours... Sorry, I can't show the output of the EXPLAIN QUARY PLAN but the bottleneck is the cursor.moveToFirst() Commented Jan 19, 2016 at 13:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.