Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

Slow response time for queries in Sanity.io project

5 replies
Last updated: Jun 22, 2023
export async function getFilteredVideos( limit,
filterGrade,
filterCategory,
search,
sort,
type = "Lesson Video",
subCategory
) {
console.time("videos fetch");
let query = `*[_type == "video"]{${videoFields}}`;

query += `[type.name == "${type}"]`;

if (filterGrade && filterGrade !== "All Grades") {
query += `[ "${filterGrade}" in grade[].label ]`;
}

if (filterCategory && filterCategory !== "All Categories") {
query += `[ "${filterCategory}" in category[].title ]`;
}

if (subCategory && subCategory !== "All Subtopic") {
query += `[subCategory.title match "${subCategory}"]`;
}

if (search && search !== "All") {
query += `[name match "${search}*"]`;
}

if (sort && sort === "longest") {
query += `| order(duration desc)`;
}

if (sort && sort === "shortest") {
query += `| order(duration asc)`;
}

if (sort && sort === "relevance") {
query += `| order(type.name desc)`;
}

query += `${limit}`;

const results = await client.fetch(query);
console.timeEnd("videos fetch");
return results;
}

export const getAllSubCategories = async (topic, grade) => {
const fields = `title,category->{title,'grade': grade->label}`;
const results = await client.fetch(

*[_type == 'videoSubCategory']{${fields}}[category.title == "${topic}"] | order(title asc)
);
const filteredForGrade = results.filter((subtopic) => {
return subtopic.category.grade === grade;
});
return filteredForGrade;
};

export async function getAllSubCatgoriesVideos(topic, grade) {
console.time("subcategory");
const fields = `title,category->{title,'grade': grade->label}`;
const results = await client.fetch(

*[_type == 'videoSubCategory']{${fields}}[category.title == "${topic}"] | order(title asc)
);
const filteredForGrade = results.filter((subtopic) => {
return subtopic.category.grade === grade;
});

const subCategoriesWithVideos = await Promise.all(
filteredForGrade.map(async (subcategory) => {
const videos = await getFilteredVideos(

[0..3]
, grade,
topic,
false,
false,
"Concept Video",
subcategory.title
);
return { ...subcategory, videos };
})
);
console.timeEnd("subcategory");
return subCategoriesWithVideos;
}

export async function getFilteredVideosCount(
filterGrade,
filterCategory,
search,
type = "Lesson Video",
subCategory
) {
console.time("count");
let query = `*[_type == "video"]{${videoFields}}`;

query += `[type.name == "${type}"]`;

if (filterGrade && filterGrade !== "All Grades") {
query += `[ "${filterGrade}" in grade[].label ]`;
}

if (filterCategory && filterCategory !== "All Categories") {
query += `[ "${filterCategory}" in category[].title ]`;
}

if (subCategory && subCategory !== "All Subtopic") {
query += `[subCategory.title match "${subCategory}"]`;
}

if (search && search !== "All") {
query += `[name match "${search}*"]`;
}

const results = await client.fetch(
count(${query})
); console.timeEnd("count");
return results;
}
these are some of my queries i believe the data i am fetching is not that large how much time should it take for the query to fetch data it is taking 4-8seconds for each req and acc to me it is too much for this small amount of data have i wrote queries wrong what can be the reasons of response being this slow?
Jun 19, 2023, 10:51 AM
Jun 19, 2023, 12:41 PM
yes
Jun 19, 2023, 12:54 PM
Well, I don't know how GROQ works internally, but usually
match/regex
type queries would be slower and also
order
. Try to remove the
order
and do it through code.
Jun 19, 2023, 12:56 PM
no change in req time after removing order
Jun 19, 2023, 1:03 PM
This guide will help you fix the issues with your query.
Jun 22, 2023, 4:34 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?