question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Poor insertion performance 10k+ records

See original GitHub issue

Goals

I am trying to understand why the performance of inserts is so poor from a react native environment vs xamarin (using the Realm .Net library).

My use case is initial syncing a mobile application with a backend of items (things like street data, assets, jobs etc.) that can range from 10-500k records for use offline over the period of a week to later be synced back on completion.

The .Net library appears to be orders of magnitude faster and has a more linear timing curve for large inserts, realm-js inserts degrade over time given my tests.

Expected Results

I am expecting similar performance on inserting many records in the realm-js library as it is in the .Net library.

Actual Results

Test 1k inserts 10k inserts 100k inserts
realm-js 0.45s 5.8s 200s
Realm.Net 0.5s 1.78s 15.4s
realm-js is… 10% faster 225.8% slower 1198.7% slower
realm-js (no indices) 0.33s 2.78s 26.9s

Steps to Reproduce

Attempt to insert a large number of records into a realm.

Code Sample

The following should be copy-pasteable into a brand new react-native app with typescript as the App.ts file. Alternatively the onPress function and models at the end of the code snippet should be all that’s necessary.

The .Net code is almost like for like.

import React, {Component, Fragment} from 'react';
import {Button, Alert} from 'react-native';
import Realm from 'realm';

// test app component
export default class App extends Component {
  render() {
    return (
      <Fragment>
        <Button title="My Button" onPress={() => this.onPress()} />
      </Fragment>
    );
  }

  // run a fake sync
  onPress() {
    requestAnimationFrame(async () => {
      const config: Realm.Configuration = {
        path: 'myDb',
        schema: [ItemSchema],
      };

      Realm.deleteFile(config);

      // make some items to eventually insert
      const itemsToInsert: Item[] = [];
      for (let i = 0; i < 10000; i++) {
        itemsToInsert.push({
          collection: 'Live',
          colour: '#800855',
          dodiCode: 'designs_myDesign',
          icon: 'icon-teste',
          itemId: '5e4baedd22475918f03acf4a' + i,
          subtitle: 'alright partner',
          title: 'keep on rollin baby!',
        });
      }

      const realm = await Realm.open(config);

      // start timing
      const start = new Date().getTime();

      realm.write(() => {
        for (let i = 0, total = itemsToInsert.length; i < total; i++) {
          realm.create(ItemSchema.name, itemsToInsert[i], Realm.UpdateMode.All);
        }
      });

      // end timing
      const end = new Date().getTime();

      realm.close();

      Alert.alert('finished: ' + (end - start) + 'ms');
    });
  }
}

// schema for item
const ItemSchema: Realm.ObjectSchema = {
  name: 'item',
  primaryKey: 'itemId',
  properties: {
    itemId: {
      type: 'string',
      optional: false,
    },
    dodiCode: {
      type: 'string',
      optional: false,
      indexed: true,
    },
    collection: {
      type: 'string',
      optional: false,
    },
    title: {
      type: 'string',
      optional: true,
      indexed: true,
    },
    subtitle: {
      type: 'string',
      optional: true,
    },
    icon: {
      type: 'string',
      optional: true,
    },
    colour: {
      type: 'string',
      optional: true,
    },
  },
};

// model for item
interface Item {
  itemId: string;
  dodiCode: string;
  collection: string;
  title: string | null;
  subtitle: string | null;
  icon: string | null;
  colour: string | null;
}

Version of Realm and Tooling

  • Realm JS SDK Version: ^4.0.0-beta.0
  • Node or React Native: React Native
  • Client OS & Version: Android Emulator and Physical Device
  • Which debugger for React Native: None

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:19
  • Comments:19 (6 by maintainers)

github_iconTop GitHub Comments

8reactions
noldidritacommented, Aug 29, 2020

Yes, totally! I’ve tried v6 for a couple of releases, then v10, but ultimately switched to v3.6 as it’s the only version that’s consistent enough (had a few crashes on both v6 and v10 that never happen on v3).

6reactions
vikas-singh-fareyecommented, Dec 3, 2020

any update on this? facing same issue, finally reverted it back to v3.6.0. AsyncStorage seems faster than realm v6.1.5 & v10.0.1 😅😅

Read more comments on GitHub >

github_iconTop Results From Across the Web

Poor insert performance when inserting records into a table ...
I can insert 10k records in about 550-600ms with autoincrementing int PK. With guid keys, it takes about 35 seconds per 10k rows....
Read more >
Database crisis: How we improved performance for large ...
Sending updates to Solr would've been fatal since data would be inconsistent across databases. To combat this, we wrapped the 10 lac batched...
Read more >
13 Tips to Improve PostgreSQL Insert Performance - Timescale
Get 13 ways to improve your database ingest (INSERT) performance and speed up your time-series queries using PostgreSQL – plus ...
Read more >
Improving Spring Data JPA/Hibernate Bulk Insert Performance ...
Before my changes we were processing 10,000 records in 47 minutes. This certainly looked bad. After making the changes that I will discuss...
Read more >
Tips for improving INSERT performance in DB2 Universal ...
Commit forces log records to disk, thus ensuring that the inserts to that point are guaranteed to be in the database, and frees...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found