12

I'm trying to compare Realm with Snappydb (This is my repo for those who would like to have a look at benchmark). I guess my way is wrong, as store-to-db time takes super long time in Realm in compare with Sanppydb.

Benchmark shows following result. As you can see in the image, Realm is around 100-200 times slower than Snappydb. enter image description here

What I'm doing is creating 10,000 objects first and then storing them into the db. So, in my code I store a Booking object in this way (there is a for loop that iterates 10,000 times):

public void storeBooking(final Booking booking) {
        mRealm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                Booking realmBooking = realm.createObject(Booking.class);
                realmBooking.setId(booking.getId());
                realmBooking.setCode(booking.getCode());
                realmBooking.setFareLowerBound(booking.getFareLowerBound());
                realmBooking.setFareUpperBound(booking.getFareUpperBound());
                realmBooking.setPhoneNumber(booking.getPhoneNumber());
                realmBooking.setPickUpTime(booking.getPickUpTime());
                realmBooking.setRequestedTaxiTypeName(booking.getRequestedTaxiTypeName());
                realmBooking.setTaxiTypeId(booking.getTaxiTypeId());
            }
        });
    }

Update

This is Snappydb method for storing Booking object.

public void storeBooking(final String key, final Booking booking) {
        try {
            mSnappyDb.put(key, booking);
        } catch (SnappydbException e) {
            e.printStackTrace();
        }
    }

Update

New results using insertOrUpdate() method and one transaction

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Hesam
  • 52,260
  • 74
  • 224
  • 365
  • Can you please share the SnappyDB code you're comparing this with? – bdash Sep 08 '16 at 03:22
  • @bdash I just updated my question. Have a look please. thanks. – Hesam Sep 08 '16 at 03:25
  • as far as I know, with your implementation about Realm, you have to create a instance of realm and it will take time and memory (here we have to create 10.000 times). So in my opinion, you should try to run a loop to create 10.000 Bookings first, then do store them in one transaction with only one realm instance. – ThaiPD Sep 08 '16 at 04:05
  • Show the for-loop. It's important to answer this question. – EpicPandaForce Sep 08 '16 at 07:11
  • But technically you're saving 10000 objects in 10000 transactions and you create 10000 objects for it, so that's pretty much the worst possible approach, lol – EpicPandaForce Sep 08 '16 at 07:18
  • Be aware that a write transaction in Realm guarantees you that the data is written safely to disk, and the method will only return once that has happened. Should your app crash during that operation, the transaction will not happen, and your database will be consistent after the crash. So if you want to insert fast, do it in one transaction. Also be aware that Ream Java now has new even faster method for bulk inserts if you really need it for anything practical. Also take care to really benchmark what you need in your application. Realms write speed is mainly limited by disk-write speed. – bmunk Sep 08 '16 at 07:48

1 Answers1

11

Your original solution saves 10000 objects in 10000 transactions and creates 10000 objects for it, so that's pretty much the worst possible approach.


Technically the right way should be this:

public void storeBookings(final List<Booking> bookings) {
    mRealm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            realm.insertOrUpdate(bookings);
        }
    });
}

In most cases when the saved object is not the same as the original object, what I do is this:

public void storeBookings(final List<Booking> bookings) {
    mRealm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            RealmBook realmBook = new RealmBook();
            for(Booking booking : bookings) {
                realmBook = mapper.toRealm(booking, realmBook); // does not create new instance
                realm.insertOrUpdate(realmBook);
            }
        }
    });
}

This solution uses 1 detached object to map the content of the list.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428