Back

Efficiency of getDocument with query (on indexed key) VS getDocument by id

  • 0
  • Databases
Elødran
15 Dec, 2023, 17:10

In my appwrite application, I have a collection containing Stores (with attributes like name, address,...), and then a different collection containing Products (with attributes like name, price, owner_store_id,...)

At some point I need to show all products of a given store and I have the following two options:

  • Using listDocuments with a query that returns only the products having owner_store_id = the id of the wanted store (and in this case, I would also create an index on the Products collection for the owner_store_id key
  • Storing the ids of the owned products in a owned_products[] attribute of the Stores collection (by adding them each time a new product is created) and then calling getDocument for each of the ids in the owned_products array of the wanted store

Which one of the two ways would you say is better? I mean, if they are equivalent efficiency-wise I would go for the first one so I can avoid storing the owned_products array for each store, but are they actually equivalent? Or getting documents by index is still more efficient than getting them by query even if the query is on an indexed key?

TL;DR
The user wants to know which approach is more efficient: using `getDocument()` with a query or using `getDocument()` by ID. They have a collection of `Stores` and another collection of `Products`. They need to show all the products of a given store. The first option is to use `listDocuments()` with a query that filters products based on the store's ID. They would also create an index on the `owner_store_id` key. The second option is to store the IDs of the owned products in an `owned_products[]` array in the `Stores` collection. Then, they would use `
Drake
15 Dec, 2023, 17:17

server-side, getDocument() is faster because that goes to the cache rather than the database.

The trade-off, though is the multiple getDocument() calls you'll need to make vs the one listDocuments() call so... ⚖️

Elødran
15 Dec, 2023, 17:37

Oh ok I didn't thought about the impact of having to do multiple requests instead of a single one, thank you! I'll probably go fot the Query option then to avoid having to keep the same information duplicated in two different places of the database then

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more