sanity-io / hydrogen-sanity

A Sanity toolkit for Hydrogen
MIT License
59 stars 12 forks source link

loadQuery increases load time #80

Open dvnrsn opened 5 months ago

dvnrsn commented 5 months ago

We have one query that takes over 3 minutes once moving to loadQuery. With sanityclient.fetch it's 2 seconds.

sanityClient.fetch: 2.429s
sanity.loadQuery: 3:04.895 (m:ss.mmm)

I'm working on optimizing all our queries and yes I can get by using sanityClient here but I'm not sure why there is such a significant difference between the two.

What is different about this particular query (from our others)? It is probably a somewhat complicated nested fragment that looks like:

  "PE": *[_type == "p" 
      && wasDeleted != true 
      && isDraft != true 
      && (t match $q || sd match $q)
      && ($s == "*" || (sn->nn == $s))
    ] | order(number desc){
    ${PE}
  },

This is not the only place of complexity but it's certainly the source of delay (i.e., when I delete this groq fragment the query is fast again.

How I got the original logs:


console.time('sanityClient.fetch');
const data = await sanityClient.fetch<Page>(
  PAGE,
  { q, z  },
);
console.timeEnd('sanityClient.fetch');

console.time('sanity.loadQuery');

const {sanity} = context;
const test = await sanity.loadQuery(PAGE, {
  q, z
});
console.timeEnd('sanity.loadQuery');
nkgentile commented 2 months ago

That's quite a discrepancy! 😅

Are you able to confirm in the subrequest profiler what the duration is?

I wonder if it has something to do with hashing the query key 🤔 That's quite a long wait though.

Could you also provide the full query you're sending once all the fragments are resolved?