Caching Expensive Database Query
Hi All,
I've been playing around trying to come to terms with Convex, its been a nice experience so far but I'm looking for some advice as I've tried finding the answer myself and am just going round in circles 😅.
Essentially I have a database query that computes a very expensive operation due having to calculate time series and format it in a way that can be used.
Currently what I'm experiencing (Which i know is an expected outcome) is that the useQuery i have that takes in args is caching the result after the logic from the database is complete.
To get to my question;
Is it possible for me to somehow cache the expensive operation where it revalidates every hour (how often the data would update) but then utilise this cached value and expose a client end query where args are provided to limit the cached data?
I appreciate your time reading my essay 😅
6 Replies
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!
hi! yeah, you should be able to write an action that updates an aggregate every hour. then just make a cron job that runs that action every hour
the queries will just use this periodically calculated aggregate
this is a pretty common pattern in convex
Hi James,
Forgive me for the most likely stupid response; But if im understanding what you're saying, i should use an Action Cache to store the cached result of the aggregate? I understand that the query would have to use this, but my concern is that the query given the args would then make new cache for each filtered result?
no, just have the action use a final mutation to write the aggregated data out to some kind of summary table
Okay, sounds like I need to rethink my data structure, appreciate the help you've helped me have a direction forward
Thanks for taking time to answer my questions and with quick responses
no problem. also, not a stupid response (or stupid question) at all. if you have more questions, let us know!