Mongo indexing
Opened this issue · 0 comments
natac13 commented
Notes
-For queries that do not use an index field, all the documents of the collection have to be scanned to fulfill the query.
- Indexes are for common queries. Therefore for what will be searched when hitting the API routes that was discussed.
_id
already is indexed- requires 8kb of data space
- is this a lot for our scale?
- negative performance for some write.. better to index of low write-to-read ratios
- indexes consume disk space and memory and most be tracked??
- probably looking at using compound indexes
- Need to plan the covered queries.
- which are queries that only have criteria and projections for the indexed fields
- creating index on common queries and only using those fields in the criteria and projection mongoDB will search the index and not the collection which greatly speeds things up
- can be used on the
sort()
operation as well
unique indexes
- use with the
sparse
option as well so that docs without a value in the indexed field will be skipped over instead of getting a null insertion; which cause a duplication and therefore an error
Created like
db.collection.createIndex({ a: 1 }, { unique: true })
Wondering if I need to do a unique index on say the email field if I specify in the Schema that that field is unique already
filtered indexed/ partial indexes
- use a filter to only include certain document in the index
db.collection.createIndex( { username: 1 }, { partialFilterExpression: { active: true } })
Only include document in the index that have the active field set to true
text indexes
- use the name option when creating so that the index name length limit is not excceed. By default mongoDB will generate a name based of all the fields in the index. WOW
Questions
- Indexing in the background.
- So each time the app starts up and connects to mongoDB do these indexes have to be built?
- Is it not a one time thing that gets stored in the database along with the records?
- If so should there be unit tests that check for the creation of the indexes we desire
- Indexing stores the documents in RAM? So effectively making the queries like Redis?
- Answer Maybe if there is a check to
totalIndexSize()
- once the value is known then there needs to be that much RAM now and for the rest of the working set??
- Answer Maybe if there is a check to
- Clarification
- do I have the partial filter option correct?