Reputation: 1105
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?
Upvotes: 6
Views: 12747
Reputation: 2305
Collection Scan: performed when querying one or more fields that do not have an index, so in that case, all documents of the same collection will be scanned one by one to find all documents that satisfy the condition.
in that case, the cost will be O(N) where N is the number of documents in the scanned collection.
Upvotes: 0
Reputation: 151132
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/
Upvotes: 10
Reputation: 1121
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
Upvotes: 6