👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Query optimization suggestion for improving performance in Sanity.io.

6 replies
Last updated: Mar 27, 2023
I have a query that runs very slow. It returns 250 objects, but takes 20 secs.

*[_type=='event' && _id =='SOME_ID'] { 
      "participants": *[_id in ^.participants[]._ref] | order(firstName) {
        _id, 
        firstName,
        lastName,
        position,
        company->{
          _id,
          name,
          url,
          companyId,
          enonicId,
          "imageUrl": logo.asset->url,
          type,
          slotSets,
          contacts[]->{
             _id, 
            firstName,
            lastName,
            position,
            company,
            companyId,
            code,
            phone,
            email,
            "imageUrl": image.asset->url
          },
         "employees": *[_type=="participant" && company->_id == ^._id],
         uvps[]->{
            _id,
            title,
            synopsis,
            description,
            cluster,
            categories,
            references,
            trl,
            attachements[] {
                ...asset-> {
                    url,
                    "name": originalFilename
                }
            }
          },
        },
        bio,
        linkedin,
        code,
        phone,
        email,
        "imageUrl": image.asset->url,
        organizer->{
            _id,
            name,
            logo,
            url
        },
        "referedByCompany": *[_type=='company' && references(^._id)],
        role,
        notifications
      }
    }
Any ideas why?
Mar 23, 2023, 1:04 PM
Here’s a thing you can probably take a look to optimize this: https://www.sanity.io/docs/high-performance-groq#2b35f6bb8ac2
Mar 23, 2023, 1:14 PM
One idea that I could think of is to separate
participants
,
employees
referedByCompany
, etc. and run them in parallel. You can pull them together with say
Promise.all
Mar 23, 2023, 1:15 PM
Thnx. I'll check it out!
Mar 23, 2023, 1:15 PM
I wish you success!
Mar 24, 2023, 2:43 PM
It was the line

"employees": *[_type=="participant" && company->_id == ^._id],
that caused the delay. I moved that to another query, and now it works smoothly.
Mar 27, 2023, 7:10 AM
awesome. 😊
Mar 27, 2023, 7:53 AM

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?