Sive
Sive3mo ago

How can I query a count of messages with particular fields when there could be 100s.

Otherwise it would time out if i try to query many messages with .collect().
4 Replies
Convex Bot
Convex Bot3mo 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!
Sive
SiveOP3mo ago
const processingMessages = await ctx.db
.query("messages")
.withIndex("by_user_role_status", (q) =>
q.eq("user", user)
.eq("role", "assistant")
.eq("status", "processing") // Second query for processing status
.gt("_creationTime", startWindow.getTime()) // Start of window
.lt("_creationTime", endWindow.getTime()) // End of window
)
.count();
const processingMessages = await ctx.db
.query("messages")
.withIndex("by_user_role_status", (q) =>
q.eq("user", user)
.eq("role", "assistant")
.eq("status", "processing") // Second query for processing status
.gt("_creationTime", startWindow.getTime()) // Start of window
.lt("_creationTime", endWindow.getTime()) // End of window
)
.count();
mikeysee
mikeysee3mo ago
hey, as you have suggested in your code above, the way to effeciently query in convex is to use an index. If you want to query a count you might want to checkout this component: https://www.convex.dev/components/aggregate or this one to help you with that: https://www.convex.dev/components/sharded-counter
Convex
Aggregate
Keep track of sums and counts in a denormalized and scalable way.
Convex
Sharded Counter
Scalable counter that can increment and decrement with high throughput.
Pierre
Pierre2mo ago
GitHub
convex-backend/npm-packages/convex/src/server/query.ts at 4583592bf...
The open-source reactive database for app developers - get-convex/convex-backend

Did you find this page helpful?