Hi all!
Fairly new to Realm (first post on these forums) using the JS SDK for building an Electron app using Realm local and eventually will be setting up Realm Sync to store and sync data with MongoDB Atlas.
I come from a SQL Server background. Trying to de-program my relational DB normalization mindset, haha. Slowly but surely loving Realm / Mongo document based DB concept more and more everyday!
My question is on query optimization. Lets say I have the following schema:
let Datastore = {
name: 'Datastore',
primaryKey: '_id',
properties: {
_id: 'objectId',
name: 'string',
description: 'string'
}
}
let Block = {
name: 'Block',
primaryKey: '_id',
properties: {
_id: 'objectId',
sourceUid: 'string',
content: 'string',
datastore: 'Datastore?'
}
}
Lets say that Datastore
only has about 10 objects but Block
has 5 million. And then lets say for example I want to do several string CONTAINS and LIKE matches on the Block.content
but also then filter by a specific datastore.name
.
let foundBlocks = realm.Object('Block').filtered('datastore.name == "Source 12345" AND content CONTAINS "cool block" AND content CONTAINS "also this" AND content LIKE "*wildcard*this*that*" AND content BEGINSWITH "starting"');
So I have a couple questions:
- Does Realm query engine optimize for the most “efficient” way automatically to filter down the criteria before applying more resource intensive string operators such as LIKE and CONTAINS?
- Based on 1, would it instead be more efficient to first query/filter by the
Datastore.name
since it only has 10 objects and then through that filters down theBlock
objects more efficiently through the Realm Reference in theDatastore?
property? Basically does it make more sense to filter down the 5 millionBlock
objects based on its datastore property to something like 500k objects BEFORE then separately applying the stringcontent
CONTAINS and LIKE query operators to give me my final filtered results? OR does Realm automatically optimize for this and therefore I can write it all in one single filter/query like I have above? - Like in other query languages like SQL, typically LIKE operators are very expensive. Is this true in Realm as well? What about CONTAINS? Do you have any tips on the performance between these operators and whether it will make a big performance difference if I can avoid using LIKE and instead use CONTAINS… and similarly if at all possible, using “==” exact matches I assume is much more performant than CONTAINS? What about BEGINSWITH and ENDSWITH?
Any other tips would be much appreciated! Thanks a lot and I look forward to my (our) journey into Realm and eventually MongoDB Atlas!
Best,
Shawn