Slow query performance with GraphQL relationships

Hi all!

I’ve been working on an iOS app that links to my MongoDB Realm using Apollo and the GraphQL integration. I noticed that I was getting really slow API response times with GraphQL, and figured that it was because I am currently in Australia (quite far from the server), but after some additional playing around with my queries, I realised something else was making the requests slow!

Basically, my request is using the _id_in query for an array of roughly 40 ids, and returns a response with some properties, as well as 3 related documents stored in other collections. When I include these 3 relationships in the query, the query takes ~10 seconds. When I don’t include these relationships, the query takes <1 second.

It seems like when I include the relationships in the query, the complexity jumps to O(N).

All the related document collections are indexed by _id (as I figure the GraphQL integration would use), but is there another index that I should be making to speed up this query, or is this just a limitation of the Realm GraphQL integration? It’s really slowing down the performance of my app pretty much everywhere!

Thanks so much for your help!

Hi @Pierre_Rodgers,

We constantly improve the graphql performance. However, with relationships each document that we process against the relationship needs to be validated against the Realm rules on the realm service side and not during the time the query is executed against the database.

This slowdown the retrieval. Perhaps for a 1 to many relationship I would suggest to perform 2 queries or have a view on the Atlas side and expose the joined view.

Best
Pavel

Thanks so much Pavel! That makes sense re performance. I’m not quite sure what you mean by the joined view on the Atlas side – do you have any more info?

Hi @Pierre_Rodgers

What I meant is to create a view on the database where the pipeline will have the needed lookup. Then you can expose this view for combined queries in graphql .

This way the engine will treat it as a single query

Thanks
Pavel

Thanks for that! I got a view set up and the GraphQL API works perfectly, however this has now broken Realm Sync – I get the following error:

recoverable event subscription error encountered: error issuing collMod command for music.fullReview: (InvalidOptions) option not supported on a view: recordPreImages

I gather that Realm Sync relies on the recordPreImages collMod command for syncing. I don’t need the view synced, but I do need it accessible in the GraphQL API – is there any way of setting up my Realm such that this view is ignored for Sync, but still available in GraphQL requests? Or is there another way around this?

Hi @Pierre_Rodgers,

Oh, I see how it can conflict if sync is in use.

The only workaround I can see is to have a separate Realm application for the view without the sync and query it.

Best
Pavel