When an optional key in a collection is queried , performance varied with respect to the following two queries.
Good response : When it queried with a specific value - a value other than null, it resulted good response time.
Poor response : When it queried with the operator $exists, it resulted poor response time.
This difference in performance should be due to difference in the execution plans chose by query optimiser. However I am not able to understand the reasoning behind it.
Therefore I request if you can explain the reason for this difference in performance, it would help me to increase and confirm my understanding.
Collection Statistics
- Number of documents : 100,000
- Name of the optional key : field_A
- Present status of field_A : It does not exists in any of the documents.
- Index : { field_A : 1}
The two queries:
Query 1 - with Good response time
- Query document : { field_A : }
- Execution time : 0.5 seconds
- Number of documents returned : 0
Query 2 - with poor response time
- Query document : { field_A : { $exists : true } }
- Execution time : 5 seconds
- Number of documents returned : 0
Most likely it uses the index.
An index (more precisely a B*Tree index) hss a sorted list of all your values. So, in your case the index hold 100k times the value
undefinedor similar. You have to read all these values to determine whether the value is undefined or not.On the other scenario, when you look for
some_value, then you know the result already after reading the first entry from the index, because it would appear before the firstundefinedvalue in case the value would exists.Don't take above explanation too literally, it just explains the principle.
Just a note, many database do not store any value in index if the value does not exist. In your case the index size would be 0 Bytes. In such database your index would be useless. However, MongoDB stores also undefined values in the index.