Android Inserting Large Amount of Data Inside ROOM Database

Viewed 1965

I have around 10 models each having records of more than 120K rows and 90 columns containing double array values. Inserting any of the models inside Room took more than 125-130 seconds. Can anyone suggest what I need to do in order to save all these 120K with some bulk insertion technique which takes around 1 or 2 seconds. Fetching any range of records are smooth and returns the sublist inside the bearable amount of time (less than a second). Below is DAO and repository methods which i am currently using.

DAO looks like this

@Transaction
@Insert(onConflict = OnConflictStrategy.IGNORE) // IGNORE/REPLACE has no effect (just to clear out things)
void insertAllDynStabRecord(List<DynStab> dynStabList);

Repository method 1st approach

Executors.newSingleThreadExecutor().execute(() -> {
        pdDao.insertAllDynStabRecords(dynStabList);
    });

Repository method 2nd approach

private static class WriteDynStabRecords extends AsyncTask<List<DynStab>, Void, Void> {
    private PDDao pdDao;

    private WriteDynStabRecords(PDDao pdDao) {
        this.pdDao = pdDao;
    }

    @Override
    protected Void doInBackground(List<DynStab>... dynStabList) {
        pdDao.insertAllDynStabRecords(dynStabList[ 0 ]); 
        return null;
    }
}

Both of these approaches takes more than 120 seconds to write around 120K records. What am i doing wrong or what are the best and fastest way to insert bulk data inside ROOM database.

0 Answers
Related