πŸ‘€ Our most exciting product launch yet πŸš€ Join us May 8th for Sanity Connect

Dealing with slow queries with multiple joins in data modeling

3 replies
Last updated: Feb 10, 2023
How can I get around slow queries with multiple joins? I have the following.
β€’ First I join on module to get
slug.current
β€’ Than on reference twice, to get
_type
and
territories[]._ref

*[_type == "careFeedItem" 
  && module->slug.current in $modules
  && reference->_type == "task"
  && $territory in reference->territories[]._ref
]
β€’ I could start keeping the module _id so I don't have to join to get the slug, but slugs are
nice 😐 β€’ An experiment with expanding reference into array just blew up in my face with 3x query time
😝
*[_type == "careFeedItem" 
  && module->slug.current in $modules
  && count([reference->][_type == "task" && $territory in territories[]._ref]) > 0 // :boom:
]
Feb 10, 2023, 8:58 AM
I recommend modeling your data to avoid this sort of thing. Finding something by looking up whether it has a reference to something is "data model smell". In particular, slugs should be fast and within immediate reach, as a rule.
Feb 10, 2023, 2:59 PM
As a general rule, using
->
inside a filter like this is going to be slow. (We are actually working on an internal optimization that should make this much faster, but we don't know when we'll be able to release it.)
Feb 10, 2023, 3:00 PM
Ok, thanks
user L
πŸ™‡β€β™‚οΈ I guess I'm damaged goods after decades modeling on an SQL stack. I'll have to look for another approach.
Feb 10, 2023, 3:13 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?