MONGO before background CREATE INDEX--noindexbuildretry

Source: Internet
Author: User

In the case of a large amount of data, any database system is a time-consuming project when creating an index. MongoDB is no exception. Therefore, the creation of MongoDB index has two choices, one is the foreground mode, and the other is the backstage mode. What is the difference between these two approaches, and is it possible to see the progress of indexing completion when creating an index? This article will be a description based on this and also describes the considerations related to index creation.

First, how to create the index
    1. 前台方式

    2. 缺省情况下,当为一个集合创建索引时,这个操作将阻塞其他的所有操作。即该集合上的无法正常读写,直到索引创建完毕

    3. 任意基于所有数据库申请读或写锁都将等待直到前台完成索引创建操作

    4. 后台方式

    5. 将索引创建置于到后台,适用于那些需要长时间创建索引的情形

    6. 这样子在创建索引期间,MongoDB依旧可以正常的为提供读写操作服务

    7. 等同于关系型数据库在创建索引的时候指定online,而MongoDB则是指定background

    8. 其目的都是相同的,即在索引创建期间,尽可能的以一种占用较少的资源占用方式来实现,同时又可以提供读写服务

    9. 后台创建方式的代价:索引创建时间变长

    10. 后台创建索引的示例

    11. db.people.createIndex( { zipcode: 1}, {background: true} )

    12. db.people.createIndex( { city: 1}, {background: true, sparse: true } )

    13. 缺省情况下background选项的值为false

Ii. Considerations during index creation
    1. 如前所述,基于后台创建索引时,其他的数据库操作能被完成。但是对于mongo shell会话或者你正在创建索引的这个连接

    2. 将不可用,直到所有创建完毕。如果需要做一些其它的操作。则需要再建立其它的连接。

    3. 在索引创建期间,即使完成了部分索引的创建,索引依旧不可用,但是一旦创建完成即可使用。

    4. 基于后台创建索引期间不能完成涉及该集合的相关管理操作

    5. repairDatabase

    6. db.collection.drop()

    7. compact

    8. 意外中断索引创建

    9. 如果在后台创建索引期间,mongod实例异常终止,当mongod实例重新启动后,未完成的索引创建将作为前台进程来执行

    10. 如果索引创建失败,比如由于重复的键等,mongod将提示错误并退出

    11. 在一个索引创建失败后启动mongod,可以使用storage.indexBuildRetry or --noIndexBuildRetry跳过索引创建来启动

Iii. performance during index creation
    1. 后台创建索引比前台慢,如果索引大于实际可用内存,则需要更长的时间来完成索引创建

    2. 所有涉及到该集合的相关操作在后台期间其执行效能会下降,应在合理的维护空挡期完成索引的创建

Iv. naming rules for indexes
    1. 缺省情况下,索引名以键名加上其创建顺序(1或者-1)组合而成。

    2. db.products.createIndex( { item: 1, quantity: -1 } )

    3. 比如上面的索引创建后,其索引名为item_1_quantity_-1

    4. 可以指定自定义的索引名称

    5. db.products.createIndex( { item: 1, quantity: -1 } , { name: "inventory_idx" } )

    6. 如上方式,我们指定了了索引名称为inventory_idx

V. View index Creation Progress
  1. 可使用 db.currentOp() 命令观察索引创建的完成进度

  2. > db.currentOp(

  3. {

  4. $or: [

  5. { op: "command", "query.createIndexes": { $exists: true } },

  6. { op: "insert", ns: /\.system\.indexes\b/ }

  7. ]

  8. }

  9. )

  10. //下面通过一个索引创建示例来查看索引完成进度

  11. //首选创建一个500w文档的集合

  12. > db.version() // Author : Leshami

  13. 3.2.10 // Blog : http://blog.csdn.net/leshami

  14. > for (var i=1;i<=5000000;i++){

  15. db.inventory.insert({id:i,item:"item"+i,stock:Math.floor(i*Math.random())})

  16. }

  17. WriteResult({ "nInserted" : 1 })

  18. > db.inventory.find().limit(3)

  19. { "_id" : ObjectId("581bfc674b0d633653f4427e"), "id" : 1, "item" : "item1", "stock" : 0 }

  20. { "_id" : ObjectId("581bfc674b0d633653f4427f"), "id" : 2, "item" : "item2", "stock" : 0 }

  21. { "_id" : ObjectId("581bfc674b0d633653f44280"), "id" : 3, "item" : "item3", "stock" : 1 }

  22. > db.inventory.find().count()

  23. 5000000

  24. //下面开始创建索引

  25. > db.inventory.createIndex({item:1,unique:true})

  26. //使用下面的命令查看索引完成进度

  27. > db.currentOp(

  28. {

  29. $or: [

  30. { op: "command", "query.createIndexes": { $exists: true } },

  31. { op: "insert", ns: /\.system\.indexes\b/ }

  32. ]

  33. }

  34. )

  35. //结果如下

  36. {

  37. "inprog" : [

  38. {

  39. "desc" : "conn1", //连接描述

  40. "threadId" : "139911670933248", //线程id

  41. "connectionId" : 1,

  42. "client" : "127.0.0.1:37524", //ip及端口

  43. "active" : true, //活动状态

  44. "opid" : 5014925,

  45. "secs_running" : 21, //已执行的时间

  46. "microsecs_running" : NumberLong(21800738),

  47. "op" : "command",

  48. "ns" : "test.$cmd",

  49. "query" : {

  50. "createIndexes" : "inventory", //这里描述了基于inventory正在创建索引

  51. "indexes" : [

  52. {

  53. "ns" : "test.inventory",

  54. "key" : {

  55. "item" : 1,

  56. "unique" : true

  57. },

  58. "name" : "item_1_unique_true"

  59. }

  60. ]

  61. },

  62. "msg" : "Index Build Index Build: 3103284/5000000 62%", //这里是完成的百分比

  63. "progress" : {

  64. "done" : 3103722,

  65. "total" : 5000000

  66. },

  67. "numYields" : 0,

  68. "locks" : { //当前持有的锁

  69. "Global" : "w",

  70. "Database" : "W",

  71. "Collection" : "w"

  72. },

  73. "waitingForLock" : false,

  74. "lockStats" : { //锁的状态信息

  75. "Global" : {

  76. "acquireCount" : {

  77. "r" : NumberLong(1),

  78. "w" : NumberLong(1)

  79. }

  80. },

  81. "Database" : {

  82. "acquireCount" : {

  83. "W" : NumberLong(1)

  84. }

  85. },

  86. "Collection" : {

  87. "acquireCount" : {

  88. "w" : NumberLong(1)

  89. }

  90. }

  91. }

  92. }

  93. ],

  94. "ok" : 1

  95. }

  96. //基于后台方式创建索引

  97. > db.inventory.createIndex({item:1,unique:true},{background: true})

Vi. Creating a terminating Index
    

MONGO before background CREATE INDEX--noindexbuildretry

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.