ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MongoDB索引创建技巧(二)

2021-04-28 19:51:14  阅读:284  来源: 互联网

标签:技巧 04 MongoDB 0800 50 conn7 索引 2021 28T18


    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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有