corey
corey3w ago

Paginatedquery rerunning

Hi, I can't seem to figure out why some paginated queries are being reran (using convex/react), and others aren't, even when the data is not changing at all. here's an example, I have one function called getHotPosts, which the query for looks like this const results = await ctx.db .query("threads") .withIndex("by_hot", (q) => q.gt("score", 0)) .filter((q) => q.and( q.eq(q.field("archived"), undefined), q.gt(q.field("_creationTime"), stableSevenDaysAgo) ) ) .order("desc") .paginate(args.paginationOpts); this function is only being ran once (besides when data changes) then i have a function getNewPosts which is like this const postsQuery = ctx.db .query("threads") .filter(q => q.eq(q.field("archived"), undefined)) .order("desc") This function is being reran without any data changing about every 15 or so seconds. The usage on the frontend is exactly the same, eg all I have to do for it to start running is replace a usage of getHotPosts with getNewPosts and off it goes. How do I prevent this?
4 Replies
Convex Bot
Convex Bot3w 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!
ian
ian3w ago
One big thing is to almost never use filter - I would add archived to your index. It's likely re-running because any change to the "threads" table invalidates your postsQuery. The index I would use, including the time: .index("by_hot", ["archived", "day", "score"]) where you store the date (e.g. "2025-08-05") And then do 7 queries in parallel for the top scores with streams and use mergeStreams to sort them all by score. Or have a "isRecent" boolean set to true when you add it, and have the index on ["archived", "isRecent", "score"] and do descending for [archived=undefined, isRecent=true]] Then have a cron job that runs every hour and flips that flag for old threads (queries threads by creation time for that hour 7 days ago)
corey
coreyOP3w ago
Thanks, question, I have to add a separate created date field, can't use the system one for custom indexing?
ian
ian3w ago
the reason i'd add date is so you can do .eq on the date, then sort by score. databases generally are very inefficient at sorting by two dynamic fields. https://stack.convex.dev/databases-are-spreadsheets
Databases are Spreadsheets
I want to share my mental model of databases: - Databases are just big spreadsheets - An index is just a view of the spreadsheet sorted by one or mor...

Did you find this page helpful?