what is a collection scan in mongodb?

2020-07-22 10:04发布

I'm aware what a "full collection scan" is. But i'm a little unsure if the term "collection scan" applies to queries that use B-tree cursor. Do queries that use a cursor other than the basic cursor perform a collection scan?

2条回答
唯我独甜
2楼-- · 2020-07-22 10:51

The short answer is the two terms are the same, or rather there is only "full collection scan".

If your query is using a B-tree cursor it is by definition not scanning the collection put is traversing the index in order to find the queried documents.

A collection scan occurs where no index can satisfy the query and we have to scan the full collection in order to find the required documents. See the link for all the information.

http://docs.mongodb.org/manual/reference/method/cursor.explain/

查看更多
对你真心纯属浪费
3楼-- · 2020-07-22 10:53

A collection scan is, well, literally scanning the whole collection. This happens when the user requests to find documents using some conditions that do cannot be answered using an index. For example lets say, we have a users collection with fields such as name, age, hair color, address, phone number and country

user = {"name" : "ABC",
         "age" : 25,
         "hair color" : "brown",
         "address" : "XYZ",
         "phone number" : 1234567890,
         "country" :"Canada"
       }

Further if we have an index on name and query the DB using,

 db.users.find({"name" : "ABC"});

Here since we have an index on the name field the query optimizer would use the index as a performance optimizing approach.

Suppose you query the DB for some other field. Lets say, address

db.users.find({"address" : "XYZ"});

Here the query optimizer would love to shorten its response time for the query, but since it has no prior information about the records in the collection, it has to go through each and every document in the collection to see if that document's address field matched the one in the query. If it does then we return that document. I am sure you know this is where the index comes in since it maintains pointers by "grouping" docs according to certain criteria.

For more info, you can look here.

For your question, a query that uses a B-tree cursor, does it to avoid performing a collection scan and hence queries using any kind of cursor other than the basic cursor "mostly" avoid a collection scan.

You can force it perform a collection scan even if theres exists an index on the field that is being queried. You can read about it here

查看更多
登录 后发表回答