Lesson 3.9 Understanding Explain Part I

Hello Everyone,
In this video @5:01, it is explained that stage:SORT_KEY_GENERATOR, means it is in memory sort,

But in detailed explanation of Quiz section for the same video, it is said that in-memory sort was performed. The stage “SORT” tells us the index was not used for sort and sort was done in memory.
I need to understand, which of below stage would let us know if sort is done in memory.

  1. stage = SORT
    Thanks in advance.

Hi @Mandar_Kulkarni1

All sorting in MongoDB is done in memory, the amount of memory used can vary. Ideally, queries should be constructed to use indexes to support sorting (see this page for more details).

That page describes what happens when an index cannot be used to support the sorting for the query.

If MongoDB cannot use an index or indexes to obtain the sort order, MongoDB must perform a blocking sort operation on the data. A blocking sort indicates that MongoDB must consume and process all input documents to the sort before returning results. Blocking sorts do not block concurrent operations on the collection or database.

In terms of the SORT_KEY_GENERATOR stage, that is where results are sorted in-memory in that stage. It’s equally true to say the SORT stage is done in memory. BOTH of the stages you highlighted indicated that in-memory sorting occurred.

Ideally, if you can construct an index where the results from the index are returned in the sorting order you desire then you will not have to perform any sorting as the index will have done this for you. This requires consideration for how best to design your queries and how to design your indexes to support both the queries and those queries that need to have their results returned in a particular sorted order.

Hope this makes sense and good luck with the course!