Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a full collection scan has been done in MongoDB

I understand that using the output of .explain() on a MongoDB query, you can look at the difference between n and nscanned to determine if a full collection scan has been performed, or if an index has been used. The docs state

You want n and nscanned to be close in value as possible.

Kyle Banker's excellent book MongoDB in Action says something very similar:

Generally speaking, you want the values of n and nscanned to be as close together as possible. When doing a collection scan, this is almost never the case.

Clearly neither of these statements are definitive about comparing n and nscanned. What proportion of difference generally infers a full collection scan - 10%, 20%, 30%+? Are there any other ways to check if a full collection scan has been done?

like image 758
br3w5 Avatar asked Mar 09 '13 21:03

br3w5


People also ask

How do I show all collection data in MongoDB?

To obtain a list of MongoDB collections, we need to use the Mongo shell command show collections . This command will return all collections created within a MongoDB database.

How fetch all data from collection in MongoDB?

Fetch all data from the collection If we want to fetch all documents from the collection the following mongodb command can be used : >db. userdetails. find(); or >db.

What is a collection scan MongoDB?

Without indexes, MongoDB must perform a collection scan, i.e. scan every document in a collection, to select those documents that match the query statement. If an appropriate index exists for a query, MongoDB can use the index to limit the number of documents it must inspect.


1 Answers

The answers above are NOT completely correct.

A collection scan will also be performed where an index is used for sorting but cannot assist the match criteria. In such a case, all the docs are scanned (in index order) to find docs which match the find criteria. Another possibility is that there may be a partial collection scan, where the index is able to narrow the subset of docs according to one or more find criteria but still needs to scan this subset of docs to find matches for the full find criteria.

In these situations, explain will show an index being used and not BasicCursor. So whilst the presence of BasicCursor in explain is indicative of a collection scan being performed, the absence of it doesn't mean a collection scan was not performed.

Also, using --notablescan also won't help where an index is used for sorting. Because queries only raise an exception where an index is not used. It doesn't look for whether the index was used for the match or the sort.

The only foolproof method of determining whether a collection scan was performed is to compare the index keys with the match criteria from the query. If the index selected by the query optimiser (and shown in explain) is not capable of answering the query match criteria (ie different fields) then a collection scan is needed.

like image 56
Matt Avatar answered Sep 21 '22 09:09

Matt