Why running queries in parallel is slow?
Hey, I'm trying to run couple queries in parallel and getting some weird performance results:
// total execution time of this example is ~200ms
const queries = [
db.collection('path').where('propId', '==', 'firestore-prop-id').get()
]
const res = await Promise.all(queries)
but when trying to add one more query exec time increases to ~800ms ( 4 times !!! )
// total execution time of this example is ~800ms
const queries = [
db.collection('path').where('propId', '==', 'firestore-prop-id').get(),
db.collection('path').where('propId', '==', 'firestore-prop-id').get(),
]
const res = await Promise.all(queries)
what is more interesting, when trying to add even more queries, now total exec time remains almost the same ~800ms
// total execution time of this example is ~800ms
const queries = [
db.collection('path').where('propId', '==', 'firestore-prop-id').get(),
db.collection('path').where('propId', '==', 'firestore-prop-id').get(),
db.collection('path').where('propId', '==', 'firestore-prop-id').get(),
db.collection('path').where('propId', '==', 'firestore-prop-id').get(),
db.collection('path').where('propId', '==', 'firestore-prop-id').get(),
]
const res = await Promise.all(queries)
Why running >1 query in parallel has such exec time difference? Is there any bottleneck, or am I missing something?
To mention, I checked query.explain with analyze param and there is no noticeable time difference in executionDuration of the queries.
Hi @sergey-shpak ,
Thank you for filling the ticket!
I tested the code with the same scenario and didn't reproduce the same behaviour.
Usually, when the SDK execute the query for the first time, it takes a little bit longer since it needs to establish the connection with the backend.
After that, every same query just take a little bit more time since they can reuse the previous connection.
So what is happened in the example you provided is, the query execution usually takes ~ 800 ms and the 200 ms one happens occasionally.
If you believe this reason doesn't fully explain your case, you please provide more data for us to look further? For example running the query multiple times to get a average time spent.
Also if you have not, please try the use the latest version of SDK.
This has been closed since a request for information has not been answered for 15 days. It can be reopened when the requested information is provided.