# MongoDB Performance<br />ch4 CRUD Optimization
###### tags: `MongoDB University M201`
## Optimizing your CRUD Operations
### Index selectivity
* Equality > Sort > Range
## Covered Queries
* Very performant
* Satisfied entirely by index keys
* 0 documents needs to be examined
:::info
An index covers a query only when **both all fields in the query are a part of the index and all the fields are turned in the result are in the same index**.
:::
### Can't cover a query
* Any of indexed fields are array
* Any of indexed fields are embedded documents
* When run against a mongos if the index does not contain the shard key.
## Regex Performance
正規表達式如果有指定開頭會比較有效率
```javascript=
db.user.find({ username: /^kirby/ })
```
## Isert Performance
## Data Type Implications
Mongodb will be grouping documents and comparing their fields based on different BSON types using a specific comparsion order
1. Minkey(internal type)
2. Null
3. Numbers(int, doubles, longs, decimals)
4. Symbol, String
5. Object
6. Array
7. BinData
8. ObjectId
9. Boolean
10. Date
11. Timestamp
12. Regular Expression
13. MaxKey(internal type)
### Index Structure
## Aggregation Performance
* index usage
* Memory Constraints
* RealTime Processing
* Provide data for applications
* Query performance is very important
* Batch Processing
* Provide data for analytics
* Query performance is less important
* Results are subject to 16MB document limit
* Use $limit and $project
* 100MB of RAM per pipeline Stage
* Use index
* db.orders.aggregate({}, {allowDiskUse: true})
* Doesn't work with graphLookup