Concept Flow - Query patterns that cause collection scans
Receive Query
Check for Indexes
Use Index
Return Results
End
The database receives a query, checks if an index can be used, and if not, performs a collection scan to find matching documents.
db.users.find({ age: { $gt: 30 } })| Step | Action | Index Available? | Operation | Documents Examined | Result |
|---|---|---|---|---|---|
| 1 | Receive query { age: { $gt: 30 } } | Unknown | Check indexes | 0 | Proceed |
| 2 | Check if index on 'age' exists | No | Decide operation | 0 | No index found |
| 3 | Perform collection scan | No | Scan all documents | 1000 | Filter documents with age > 30 |
| 4 | Return matching documents | No | Return results | N/A | Documents with age > 30 |
| 5 | End query execution | No | Complete | N/A | Query finished |
| Variable | Start | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|
| indexExists | Unknown | False | False | False |
| documentsExamined | 0 | 0 | 1000 | 1000 |
| matchingDocuments | 0 | 0 | Variable | Variable |
Query patterns without matching indexes cause collection scans. Database checks for indexes first. If no index, scans all documents. Collection scans are slower on large data. Create indexes on queried fields to avoid scans.