Android Inserting Large Amount of Data Inside ROOM Database

人走茶凉 提交于 2020-07-09 12:13:45

问题


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.

来源:https://stackoverflow.com/questions/61741592/android-inserting-large-amount-of-data-inside-room-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!