realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

How to Create faster write transaction?

Open koz01 opened this issue 10 years ago • 14 comments

I have the following code snippet mimic sqlite bulk insert for over 4000 transactions, I would like to know what should be the default fastest and alternative best method to perform block writes to Realm memory or persist to Realm files without any expensive overhead loading ? - See more at: http://www.mzan.com/article/33358880-realm-java-fastest-bulk-write-methodology.shtml#sthash.YmBNOR7f.dpuf

koz01 avatar May 23 '16 05:05 koz01

copyToRealm uses createObject under the hood, so doing that manually is slightly faster (although not by much). copyToRealm is a lot safer though as that automatically handle duplicate objects and circular data structures. For inserting many objects I would recommend using copyToRealmOrUpdate. It is not as fast as we would like it yet, but we have room for a lot of improvement and it is being tracked here: #1684

cmelchior avatar May 23 '16 06:05 cmelchior

I would like to know best practices for realm write transaction methods.Because, i'm doing synchronization 1 millions records from ERP server to android app. I want to taking within 1 min for 5000 records. Now, i'm using the follwing methods..

public T saveOrUpdate(Realm realm, T t) { T result; if (realm.isInTransaction() == false) { realm.beginTransaction(); } result = realm.copyToRealmOrUpdate(t); realm.commitTransaction(); return result; }

Thank....

koz01 avatar May 23 '16 07:05 koz01

It might be better to add objects in batches instead of do many small write transactions.

kneth avatar May 23 '16 07:05 kneth

Realm realm = null;
try {
    realm = Realm.getInstance(config);
    realm.beginTransaction();
    RealmObj defaultInstance = new RealmObj();
    for(InputObj input : inputObjs) {
        defaultInstance = mapper.toRealmObject(realm, input, defaultInstance);
        realm.copyToRealmOrUpdate(defaultInstance);
    }
    realm.commitTransaction(); 
} catch(Throwable e) {
    Log.e(TAG, "An error occurred", e);
    try {
        realm.cancelTransaction();
    } catch(Exception e) {
        Log.w(TAG, "Probably not in a transaction.");
    }
    throw e;
} finally {
    if(realm != null) {
        realm.close();
    }
}

is what I do.

Zhuinden avatar May 23 '16 09:05 Zhuinden

I'll close the issue. We are working on faster inserts (see #1684).

kneth avatar May 30 '16 08:05 kneth

@Zhuinden I have this method:

public void insertOrUpdateJSON(InputStream inputStream, Class<? extends RealmModel> clazz){
        long time = System.currentTimeMillis();
        if (db.isInTransaction()) {
            db.createOrUpdateAllFromJson(clazz, inputStream);
        } else {
            try {
                db.beginTransaction();
                db.createOrUpdateAllFromJson(clazz,inputStream);
                db.commitTransaction();
            } catch (Exception e) {
                db.cancelTransaction();
                throw e;
            }
        }
        long end = System.currentTimeMillis() - time;
        Log.d("Base DAO","Tiempo de ejecucion insertar json by InputStream: "+ end);
    }

My JSON File have aproximately 100K values and for 3 files JSON took 2 minutes loading the data. Exist another way to do it faster?

andresvd1 avatar Jun 07 '18 01:06 andresvd1

Well.... are you in transaction when this happens? Because one transaction is faster than many transactions.

Zhuinden avatar Jun 07 '18 01:06 Zhuinden

This method is called by this action:

for(InputStream stream : streams) {
            catLocalidadesDAO.insertOrUpdateJSON(stream, CatLocalidadesInegi.class);
        }

andresvd1 avatar Jun 07 '18 02:06 andresvd1

But is it in a transaction???? :D

Zhuinden avatar Jun 07 '18 10:06 Zhuinden

No, no is in a transaction :(, what do you suggest?

andresvd1 avatar Jun 07 '18 13:06 andresvd1

    try(Realm realm = Realm.getDefaultInstance()) {
        realm.executeTransaction(new Realm.Transaction() {
              @Override
              public void execute(Realm realm) {                        
                  for(InputStream stream : streams) {
                       catLocalidadesDAO.insertOrUpdateJSON(stream, CatLocalidadesInegi.class);
                  }
             }
        });
    }

Zhuinden avatar Jun 07 '18 14:06 Zhuinden

Thanks a lot, it's true is faster using transaction

andresvd1 avatar Jun 07 '18 19:06 andresvd1

@andresvd1 have you also tried using insert* methods instead of copy*? copy* are generally good if you need to use/manipulate the managed object you just add to the Realm, whereas insert* are relatively faster since they don't return the managed object...

nhachicha avatar Jun 09 '18 15:06 nhachicha

@nhachicha he is using JSON import api

Zhuinden avatar Jun 09 '18 19:06 Zhuinden