mongo 停止创建索引 --noIndexBuildRetry


在数据量超大的情形下,任何数据库系统在创建索引时都是一个耗时的大工程。MongoDB也不例外。因此,MongoDB索引的创建有两个选择,一个是前台方式,一个是后台方式。那这两种方式有什么差异呢,在创建索引是是否能观察到索引完成的进度呢。本文将是基于此的描述,同时也描述了索引创建相关的注意事项。

一、索引创建方式

  1.  
    前台方式
  2.  
    缺省情况下,当为一个集合创建索引时,这个操作将阻塞其他的所有操作。即该集合上的无法正常读写,直到索引创建完毕
  3.  
    任意基于所有数据库申请读或写锁都将等待直到前台完成索引创建操作
  4.  
     
  5.  
    后台方式
  6.  
    将索引创建置于到后台,适用于那些需要长时间创建索引的情形
  7.  
    这样子在创建索引期间, MongoDB依旧可以正常的为提供读写操作服务
  8.  
    等同于关系型数据库在创建索引的时候指定 online,而MongoDB则是指定background
  9.  
    其目的都是相同的,即在索引创建期间,尽可能的以一种占用较少的资源占用方式来实现,同时又可以提供读写服务
  10.  
    后台创建方式的代价:索引创建时间变长
  11.  
     
  12.  
    后台创建索引的示例
  13.  
    db.people.createIndex( { zipcode: 1}, {background: true} )
  14.  
    db.people.createIndex( { city: 1}, {background: true, sparse: true } )
  15.  
     
  16.  
    缺省情况下 background选项的值为false

二、索引创建期间注意事项

  1.  
    如前所述,基于后台创建索引时,其他的数据库操作能被完成。但是对于 mongo shell会话或者你正在创建索引的这个连接
  2.  
    将不可用,直到所有创建完毕。如果需要做一些其它的操作。则需要再建立其它的连接。
  3.  
     
  4.  
    在索引创建期间,即使完成了部分索引的创建,索引依旧不可用,但是一旦创建完成即可使用。
  5.  
     
  6.  
    基于后台创建索引期间不能完成涉及该集合的相关管理操作
  7.  
    repairDatabase
  8.  
    db.collection.drop()
  9.  
    compact
  10.  
     
  11.  
    意外中断索引创建
  12.  
    如果在后台创建索引期间, mongod实例异常终止,当mongod实例重新启动后,未完成的索引创建将作为前台进程来执行
  13.  
    如果索引创建失败,比如由于重复的键等, mongod将提示错误并退出
  14.  
    在一个索引创建失败后启动 mongod,可以使用storage.indexBuildRetry or --noIndexBuildRetry跳过索引创建来启动

三、索引创建期间性能

  1.  
    后台创建索引比前台慢,如果索引大于实际可用内存,则需要更长的时间来完成索引创建
  2.  
    所有涉及到该集合的相关操作在后台期间其执行效能会下降,应在合理的维护空挡期完成索引的创建

四、索引的命名规则

  1.  
    缺省情况下,索引名以键名加上其创建顺序(1或者 -1)组合而成。
  2.  
    db.products.createIndex( { item: 1, quantity: -1 } )
  3.  
    比如上面的索引创建后,其索引名为 item_1_quantity_-1
  4.  
    可以指定自定义的索引名称
  5.  
     
  6.  
    db.products.createIndex( { item: 1, quantity: -1 } , { name: "inventory_idx" } )
  7.  
     
  8.  
    如上方式,我们指定了了索引名称为 inventory_idx

五、查看索引创建进度

  1.  
    可使用 db.currentOp() 命令观察索引创建的完成进度
  2.  
     
  3.  
    > db.currentOp(
  4.  
    {
  5.  
    $or: [
  6.  
    { op: "command", "query.createIndexes": { $exists: true } },
  7.  
    { op: "insert", ns: /\.system\.indexes\b/ }
  8.  
    ]
  9.  
    }
  10.  
    )
  11.  
     
  12.  
    //下面通过一个索引创建示例来查看索引完成进度
  13.  
    //首选创建一个500w文档的集合
  14.  
     
  15.  
    > db.version() // Author : Leshami
  16.  
    3.2.10 // Blog : http://blog.csdn.net/leshami
  17.  
     
  18.  
    > for (var i=1;i<=5000000;i++){
  19.  
    db.inventory.insert({ id:i,item:"item"+i,stock:Math.floor(i*Math.random())})
  20.  
    }
  21.  
    WriteResult({ "nInserted" : 1 })
  22.  
     
  23.  
    > db.inventory.find().limit( 3)
  24.  
    { "_id" : ObjectId("581bfc674b0d633653f4427e"), "id" : 1, "item" : "item1", "stock" : 0 }
  25.  
    { "_id" : ObjectId("581bfc674b0d633653f4427f"), "id" : 2, "item" : "item2", "stock" : 0 }
  26.  
    { "_id" : ObjectId("581bfc674b0d633653f44280"), "id" : 3, "item" : "item3", "stock" : 1 }
  27.  
     
  28.  
    > db.inventory.find().count()
  29.  
    5000000
  30.  
     
  31.  
    //下面开始创建索引
  32.  
    > db.inventory.createIndex({ item:1,unique:true})
  33.  
     
  34.  
    //使用下面的命令查看索引完成进度
  35.  
    > db.currentOp(
  36.  
    {
  37.  
    $or: [
  38.  
    { op: "command", "query.createIndexes": { $exists: true } },
  39.  
    { op: "insert", ns: /\.system\.indexes\b/ }
  40.  
    ]
  41.  
    }
  42.  
    )
  43.  
     
  44.  
    //结果如下
  45.  
    {
  46.  
    "inprog" : [
  47.  
    {
  48.  
    "desc" : "conn1", //连接描述
  49.  
    "threadId" : "139911670933248", //线程id
  50.  
    "connectionId" : 1,
  51.  
    "client" : "127.0.0.1:37524", //ip及端口
  52.  
    "active" : true, //活动状态
  53.  
    "opid" : 5014925,
  54.  
    "secs_running" : 21, //已执行的时间
  55.  
    "microsecs_running" : NumberLong(21800738),
  56.  
    "op" : "command",
  57.  
    "ns" : "test.$cmd",
  58.  
    "query" : {
  59.  
    "createIndexes" : "inventory", //这里描述了基于inventory正在创建索引
  60.  
    "indexes" : [
  61.  
    {
  62.  
    "ns" : "test.inventory",
  63.  
    "key" : {
  64.  
    "item" : 1,
  65.  
    "unique" : true
  66.  
    },
  67.  
    "name" : "item_1_unique_true"
  68.  
    }
  69.  
    ]
  70.  
    },
  71.  
    "msg" : "Index Build Index Build: 3103284/5000000 62%", //这里是完成的百分比
  72.  
    "progress" : {
  73.  
    "done" : 3103722,
  74.  
    "total" : 5000000
  75.  
    },
  76.  
    "numYields" : 0,
  77.  
    "locks" : { //当前持有的锁
  78.  
    "Global" : "w",
  79.  
    "Database" : "W",
  80.  
    "Collection" : "w"
  81.  
    },
  82.  
    "waitingForLock" : false,
  83.  
    "lockStats" : { //锁的状态信息
  84.  
    "Global" : {
  85.  
    "acquireCount" : {
  86.  
    "r" : NumberLong(1),
  87.  
    "w" : NumberLong(1)
  88.  
    }
  89.  
    },
  90.  
    "Database" : {
  91.  
    "acquireCount" : {
  92.  
    "W" : NumberLong(1)
  93.  
    }
  94.  
    },
  95.  
    "Collection" : {
  96.  
    "acquireCount" : {
  97.  
    "w" : NumberLong(1)
  98.  
    }
  99.  
    }
  100.  
    }
  101.  
    }
  102.  
    ],
  103.  
    "ok" : 1
  104.  
    }
  105.  
     
  106.  
    //基于后台方式创建索引
  107.  
     
  108.  
    > db.inventory.createIndex({ item:1,unique:true},{background: true})

六、终止索引的创建

    db.killOp() 

原文连接:https://blog.csdn.net/zhang123456456/article/details/83002168?utm_source=blogxgwz8


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM