Rebuild & Compact
當你建立index時db.myCollection.ensureIndex({..})
, mongo會馬上建立index
foreground (blocking)
- Default 預設的作法
- 因為是blocking, 在該document下, 無法進行任何query
background ()
db.myCollection.ensureIndex({..},{background: true})
- index建立中依然可以read and wiite, 但是要等index建立完成才能使用該index
- 但是會花掉更多的時間
reIndex()
- 重建一個document內的indexes
- 非必要不會用到這個
The reIndex command drops all indexes on a collection and recreates them. This operation may be expensive for collections that have a large amount of data and/or a large number of indexes.
compact
- Rebuild collection
- blocking operation
- 可以 Make sure your collections storage are structurally optimized
db.myCollection.runCommand({ compact: 'myCollection'})
- 需要去看你使用的Storage Engine, 是否需要這個操作
Rewrites and defragments all data and indexes in a collection. On WiredTiger databases, this command will release unneeded disk space to the operating system.
- Defragments documents
- Rebuild all indexes