See Sanity in action 👀 Join us for a live product demo + Q&A →

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
• Than on reference twice, to get

*[_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 – build remarkable experiences at scale

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

Categorized in