skip to Main Content
const myScore = 50;

await db.collection("scores")
  .where("score", ">=", myScore)
  .orderBy("score")
  .limit(10)
  .get();

or 

await db.collection("scores")
  .where("score", ">=", myScore)
  .limit(10)
  .orderBy("score")
  .get();

I’m expecting that orderBy will work faster if I use it after limit.

2

Answers


  1. The performance of the two queries is exactly the same. It’s not possible to limit before order. The limit is always taken from the final result, after all filters and orders are applied.

    Login or Signup to reply.
  2. Query performance will differ as in first case you are sorting the data and then limiting it and in second case you are extracting the data first and then sorting it. If the data size of the collection is large you will notice the difference. Also you can use explain("executionStats") after the query to know how both the queries are executing (Query execution Plan).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search