firestore: Is it faster to retrieve documents by id or by query?
P粉291886842
P粉291886842 2023-09-14 08:54:32
0
1
346

My collection of documents has an id (of course), but also has a field that holds the same id. Question: Is it faster to retrieve this document by id

collection.doc(id).get().then(....)

or by query

collection.where('id', '==', id).get().then(...)

I just refactored my function code. I used to use queries, but now I directly use the ids of the documents in the collection. But my impression is that this direct approach takes much longer. I'm just wondering if this is just an impression...

P粉291886842
P粉291886842

reply all (1)
P粉232409069

Theoretically, there is no difference in performance between the two. Firestore queries scale with the number of documents returned by the query. If both queries return 1 document, then they should perform the same operation. They essentially use indexes to find unique values.

See:Queries scale with the size of the result set, not the size of the data set

If you really want to microbenchmark this, you should perform your own tests to find out which one is faster. But I think you're going to spend a lot of time optimizing something that doesn't need to be optimized. You can decide for yourself whether this is worth your time.

My opinionated advice: You should worry about code clarity, not performance. Which one makes more sense to you when you read it in the context of the program? Generally, a single fetch of a document reference is shorter and easier to read, and there's no need to look at a set of document results when you're only expecting a single document, but your opinion may vary.

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!