- MongoDB Administrator’s Guide
- Cyrus Dasadia
- 562字
- 2021-07-02 15:47:49
How to do it...
- Remove all indexes:
> db.mockdata.dropIndexes()
{
"nIndexesWas" : 2,
"msg" : "non-_id indexes dropped for collection",
"ok" : 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
- 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.
- 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})
- In the second shell window, perform a simple insert operation:
> db.mockdata.insert({foo:'bar'})
- 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
- Now drop the indexes and get ready to repeat steps 4 and step 5 again.
- 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})
- 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 })
- 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