官术网_书友最值得收藏!

How to do it...

  1. Remove all indexes:
 > db.mockdata.dropIndexes()

{
"nIndexesWas" : 2,
"msg" : "non-_id indexes dropped for collection",
"ok" : 1
}
  1. Add some additional data to increase the size of our collection. Run the following command string in your Terminal window:
 for x in $(seq 20); do mongoimport --headerline --type=csv -d mydb -c mockdata -h localhost chapter_2_mock_data.csv;done
  1. Open two mongo shells, we will create an index in one while we do an insert query in another. Ensure you've selected mydb by executing the command use mydb in both windows.
  2. In the first mongo shell, create an index and immediately shift to the second shell:
> db.mockdata.createIndex({city:1, first_name:1, last_name:1})
  1. In the second shell window, perform a simple insert operation:
 > db.mockdata.insert({foo:'bar'})
  1. Check the mongod server logs:
2017-06-13T03:54:26.296+0000 I INDEX [conn1] build index on: mydb.mockdata properties: { v: 2, key: { city: 1.0, first_name: 1.0, last_name: 1.0 }, name: "city_1_first_name_1_last_name_1", ns: "mydb.mockdata" }
2017-06-13T03:54:26.297+0000 I INDEX [conn1] building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2017-06-13T03:54:36.575+0000 I INDEX [conn1] build index done. scanned 2100001 total records. 10 secs
2017-06-13T03:54:36.576+0000 I COMMAND [conn2] command mydb.mockdata appName: "MongoDB Shell" command: insert { insert: "mockdata", documents: [ { _id: ObjectId('59474af356e41a7db57952b6'), foo: "bar" } ], ordered: true } ninserted:1 keysInserted:3 numYields:0 reslen:29 locks:{ Global: { acquireCount: { r: 1, w: 1 } }, Database: { acquireCount: { w: 1 }, acquireWaitCount: { w: 1 }, timeAcquiringMicros: { w: 9307131 } }, Collection: { acquireCount: { w: 1 } } } protocol:op_command 9307ms
2017-06-13T03:54:36.577+0000 I COMMAND [conn1] command mydb.$cmd appName: "MongoDB Shell" command: createIndexes { createIndexes: "mockdata", indexes: [ { key: { city: 1.0, first_name: 1.0, last_name: 1.0 }, name: "city_1_first_name_1_last_name_1" } ] } numYields:0 reslen:98 locks:{ Global: { acquireCount: { r: 1, w: 1 } }, Database: { acquireCount: { W: 1 } }, Collection: { acquireCount: { w: 1 } } } protocol:op_command 10284ms
  1. Now drop the indexes and get ready to repeat steps 4 and step 5 again.
  2. In the first mongo shell window, recreate the index. As this command will take some time, switch to the second shell window:
 > db.mockdata.createIndex({city:1, first_name:1, last_name:1}, {background:1})
  1. In the second shell window, perform an insert operation, this time it should immediately yield:
 > db.mockdata.insert({foo:'bar'})

You should see the following output:

 WriteResult({ "nInserted" : 1 })
  1. Look at the mongod server logs:
2017-06-13T04:00:29.248+0000 I INDEX [conn1] build index on: mydb.mockdata properties: { v: 2, key: { city: 1.0, first_name: 1.0, last_name: 1.0 }, name: "city_1_first_name_1_last_name_1", ns: "mydb.mockdata", background: 1.0 }
2017-06-13T04:00:32.008+0000 I - [conn1] Index Build (background): 397400/2200004 18%
2017-06-13T04:00:35.002+0000 I - [conn1] Index Build (background): 673800/2200005 30%
2017-06-13T04:00:38.009+0000 I - [conn1] Index Build (background): 762300/2200005 34%
2017-06-13T04:00:41.006+0000 I - [conn1] Index Build (background): 903400/2200005 41%
<< --- output snipped --- >>
2123200/2200005 96%
2017-06-13T04:02:32.021+0000 I - [conn1] Index Build (background): 2148300/2200005 97%
2017-06-13T04:02:35.021+0000 I - [conn1] Index Build (background): 2172800/2200005 98%
2017-06-13T04:02:38.019+0000 I - [conn1] Index Build (background): 2195800/2200005 99%
2017-06-13T04:02:38.566+0000 I INDEX [conn1] build index done. scanned 2100006 total records. 129 secs
2017-06-13T04:02:38.572+0000 I COMMAND [conn1] command mydb.$cmd appName: "MongoDB Shell" command: createIndexes { createIndexes: "mockdata", indexes: [ { key: { city: 1.0, first_name: 1.0, last_name: 1.0 }, name: "city_1_first_name_1_last_name_1", background: 1.0 } ] } numYields:20353 reslen:98 locks:{ Global: { acquireCount: { r: 20354, w: 20354 } }, Database: { acquireCount: { w: 20354, W: 2 } }, Collection: { acquireCount: { w: 20354 } } } protocol:op_command 129326ms
主站蜘蛛池模板: 上蔡县| 马鞍山市| 百色市| 中西区| 山丹县| 盱眙县| 麟游县| 壶关县| 信阳市| 沛县| 台江县| 卢氏县| 嘉兴市| 泸定县| 兰州市| 中卫市| 新河县| 元谋县| 尤溪县| 辽中县| 杭锦后旗| 台前县| 廉江市| 白山市| 扎兰屯市| 忻州市| 深泽县| 东明县| 都江堰市| 梁平县| 连南| 泗阳县| 长子县| 巴彦淖尔市| 石阡县| 高唐县| 应城市| 澄城县| 海安县| 开远市| 永吉县|