MongoDB索引创建技巧(二)
作者:互联网
MongoDB早起版本对创建索引消耗内存没有做限制,对于大集合添加索引容易造成性能抖动,甚至OOM。
从3.4版本引入了maxIndexBuildMemoryUsageMegabytes参数,默认为500MB,用来限制单条索引创建所消耗的内存。4.2.3版本之后该参数默认为200MB。
下面我们来测试一下此参数使用效果。
查看MongoDB版本
mongo --version
MongoDB shell version v4.2.12
git version: 5593fd8e33b60c75802edab304e23998fa0ce8a5
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64
可以看到我们的MongoDB版本为4.2.12,maxIndexBuildMemoryUsageMegabytes默认值为200MB。
创建索引
在默认参数情况下创建索引。
> db.usertable.createIndex({"field1":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
查看日志
2021-04-28T18:50:40.865+0800 I INDEX [conn7] index build: starting on db5.usertable properties: { v: 2, key: { field1: 1.0 }, name: "field1_1", ns: "db5.usertable" } using method: Hybrid
2021-04-28T18:50:40.865+0800 I INDEX [conn7] build may temporarily use up to 200 megabytes of RAM
2021-04-28T18:50:43.003+0800 I - [conn7] Index Build: scanning collection: 178200/982334 18%
2021-04-28T18:50:46.001+0800 I - [conn7] Index Build: scanning collection: 528700/982334 53%
2021-04-28T18:50:49.014+0800 I - [conn7] Index Build: scanning collection: 738500/982334 75%
2021-04-28T18:50:52.031+0800 I - [conn7] Index Build: scanning collection: 889000/982334 90%
2021-04-28T18:50:54.737+0800 I INDEX [conn7] index build: collection scan done. scanned 982334 total records in 13 seconds
2021-04-28T18:51:00.265+0800 I INDEX [conn7] index build: inserted 982334 keys from external sorter into index in 5 seconds
2021-04-28T18:51:02.806+0800 I INDEX [conn7] index build: done building index field1_1 on ns db5.usertable
2021-04-28T18:51:03.515+0800 I COMMAND [conn7] command db5.usertable appName: "MongoDB Shell" command: createIndexes { createIndexes: "usertable", indexes: [ { key: { field1: 1.0 }, name: "field1_1" } ], lsid: { id: UUID("dad5266c-9403-4cde-acbd-1ceecc495d24") }, $db: "db5" } numYields:7830 reslen:114 locks:{ ParallelBatchWriterMode: { acquireCount: { r: 7832 } }, ReplicationStateTransition: { acquireCount: { w: 7833 } }, Global: { acquireCount: { r: 1, w: 7832 } }, Database: { acquireCount: { r: 1, w: 7832 } }, Collection: { acquireCount: { r: 7832, w: 1, R: 1, W: 2 } }, Mutex: { acquireCount: { r: 4 } } } flowControl:{ acquireCount: 7831, timeAcquiringMicros: 3830 } storage:{ data: { bytesRead: 876234565, timeReadingMicros: 11704476 } } protocol:op_msg 22690ms
日志显示:build may temporarily use up to 200 megabytes of RAM
标签:技巧,04,MongoDB,0800,50,conn7,索引,2021,28T18 来源: https://blog.51cto.com/u_12592884/2740348