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

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
主站蜘蛛池模板: 嘉黎县| 龙口市| 郑州市| 兴城市| 阳西县| 大洼县| 濉溪县| 临邑县| 婺源县| 明水县| 栾城县| 哈密市| 江油市| 吴江市| 银川市| 高要市| 鄂托克前旗| 旬邑县| 鄄城县| 绵阳市| 大荔县| 新宾| 通榆县| 龙里县| 吉木乃县| 来凤县| 都兰县| 娱乐| 四子王旗| 赣州市| 遵义市| 翁牛特旗| 昌邑市| 临颍县| 进贤县| 广饶县| 邵武市| 科技| 自治县| 翁源县| 宁波市|