Starlord
Starlord2w ago

Index query with "OR" statement

hello, is there a way to optimize this query using index because "or" statement is not possible in index? this way full table data is fetched and only filtered afterwards.

// Get variations for filtered products
const allProductVariations = await ctx.db
.query("productVariations")
.filter(q =>
q.or(...results.page.map(product =>
q.eq(q.field("productId"), product._id)
))
)
.collect();

// Get variations for filtered products
const allProductVariations = await ctx.db
.query("productVariations")
.filter(q =>
q.or(...results.page.map(product =>
q.eq(q.field("productId"), product._id)
))
)
.collect();
5 Replies
Convex Bot
Convex Bot2w ago
Thanks for posting in <#1088161997662724167>. Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets. - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.) - Use search.convex.dev to search Docs, Stack, and Discord all at once. - Additionally, you can post your questions in the Convex Community's <#1228095053885476985> channel to receive a response from AI. - Avoid tagging staff unless specifically instructed. Thank you!
Starlord
StarlordOP2w ago
thanks. is there a reason why index doesnt have or statement? would make life much easier. i have tons of code where its needed
lee
lee2w ago
index queries look at a contiguous range of the table when sorted by the index fields. with an OR statement, it's no longer contiguous, so it's no longer a single db query. this is part of the convex philosophy to make the performance of a query obvious from how it's written; so you don't accidentally query too much data.
Starlord
StarlordOP2w ago
ok undersstand

Did you find this page helpful?