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

Slow performance while updating a field with 50000 objects.

Open cchuang6 opened this issue 9 years ago • 7 comments

We recently hit a problem while updating a field for one of 50000 objects. However, creating a new object is totally fine.

Here is our model:

@Index
private Integer id;
@Index
private Date date1;
@Index
private Boolean date2;
@Index
private String string0;

private String string1;
private String string2;
private String string3;
private String string4;
private String string5;
private String string6;
private String string7;
private String string8;
private String string9;
private String string10;
private String string11;
private String string12;
private String string13;
private String string14;
private String string15;

private Integer integer1 = 0;
private Integer integer2 = 0;
private Integer integer3 = 0;
private Integer integer4;
private Integer integer5;

private Date date1;
private Date date2;
private Date date3;

private Boolean bool1;
private Boolean bool2;
private Boolean bool3;
private Boolean bool4;
private Boolean bool5;

private RealmList<RealmObj1> realmOjb1;
private RealmList<RealmObj2> realmOjb2;
private RealmList<RealmObj3> realmOjb3;
private RealmList<RealmObj4> realmOjb4;
private RealmList<RealmObj5> realmOjb5;

Here is the simple code which takes about 2 minute to update one field:

saveTransaction = this.getRealm().executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm bgRealm) {            
                contact.setInteger1(0);      
        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            onGoBack();
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            onGoBack();
        }
    });

Please help us with this problem. Sincerely

cchuang6 avatar Jul 04 '17 07:07 cchuang6

Hi @cchuang6

Realm transactions all block each other, so most likely you have another long-running transaction running in the background that blocks this one. Could that be the case?

cmelchior avatar Jul 04 '17 07:07 cmelchior

@cmelchior I think it is because I have bidirectional link in my schema. #4544 My RealmObj1 also link to my current object. I will try to replace bidirectional links with @LinkingObjects using 3.4.0-SNAPSHOT and see if it works.

cchuang6 avatar Jul 04 '17 07:07 cchuang6

Yes, bi-directional links can cause notifications to be delayed. The write itself is fast enough, but our algorithm for detecting changes afterward can delay things quite a bit.

Let me know if you are running into any issues converting to using @LinkingObjects.

cmelchior avatar Jul 04 '17 07:07 cmelchior

Hi, @cmelchior

I have a question about Realm migration for LinkingObjects. How could I add a new LinkingObjects field ?

Thanks

cchuang6 avatar Jul 04 '17 20:07 cchuang6

@cchuang6 @LinkingObjects fields aren't actually part of the schema, so you can actually just add it without triggering a migration, afaik.

Zhuinden avatar Jul 04 '17 20:07 Zhuinden

@Zhuinden Thanks a lot

cchuang6 avatar Jul 04 '17 21:07 cchuang6

After I changed to @LinkingObjects fields, it works.

Thanks

cchuang6 avatar Jul 07 '17 03:07 cchuang6