One large query vs. multiple small queries
Hello again! 🙂 I’m curious whether it’s better to make one large query and put everything into an object or to use multiple
useQuery
calls.
I’m creating a table and using a single query function to retrieve rows, columns, cell values, and various things like dropdown selections, returning them as an object. I could also use separate useQuery
calls for each row and cell.
What are the pros and cons between these two approaches? Specifically, I’m interested in the potential for function timeouts and their impact on caching. Thank you.5 Replies
For a table you probably want
usePaginatedQuery
unless you know your table will be small.
To the question as stated, it's not a simple tradeoff atm. Multiple small function calls will lead to more function calls, but potentially less bandwidth during updates (as the subscription is more granular). We recommend doing what seems easiest and optimizing only if needed.@Michal Srb Thanks Michal!
Also, this is a bit unrelated, but I have another question. Within a server query, I am looping
ctx.db.get()
inside Promise.all()
to get the options selected by the user. Most of the time, half of the selected options are the same. In this case, would putting the IDs into a set beforehand to make them unique create a performance improvement, and is there a caching mechanism here?Would be curious to hear what you find experimentally because performance can be surprising, but yes there is some caching at this layer.
Okay, I did the test:
Fetching a list of 3000 items consisting of a total of 3 unique IDs (["id1" x1000..., "id2" x1000...]) within a Promise.all took 750ms.
When the same list was converted to a Set, and reducing its length to 3 items, it took 500ms to fetch.
Even though it takes longer, it's clear that there's a caching mechanism at play, the 250ms difference is very short given the difference in item count.
Nice! I don't know if the test is sensitive enough but curious what would happen if fetch "id1", "id2", "id3" first and then fetch the rest; ie change the order of the promises in the Promise.all array.
And caveat to you and anyone else looking at these; feel free to optimize like this, but don't count on this always working quite like this; the design of the backend query system has room for a lot of optimizations that have not been made yet.