Chapter III index operations and performance testing
The importance of indexes under big data is not much to say.
The following test used a MongoDB client tool Robomongo, you can choose to download on the Internet. Official website: http://www.robomongo.org/
Inserting test data
Insert 1 million test data first
for (Var i=1;i<1000000;i++) {var person={name: "Jack" +i,age:i,address:["Henan", "Wuhan"],course:[{name: "Shuxue", Score:i},{name: "Wuli", score:i}]}db. DemoTest.Person.insert (Person)}
Performance analysis functions (explain)
MongoDB gives us a keyword called "explain" for performance analysis.
Perform the query analysis as follows:
Db. DemoTest.Person.find ({Name: "jack5784124"}). Explain ()
从我们可以看出在没有建立索引的情况下,查询的时间大约是是615毫秒。
Create an index
Db. TestCollection.Person.ensureIndex ({name:1})
-----1 indicates the index in ascending alphabetical order
Inquire
As we can see from the graph, the query time becomes about 2 milliseconds and the performance is increased by hundreds of times times.
Unique index
As with SQL Server, you can create a unique index, and duplicate key values cannot be inserted naturally, and the use in MongoDB is as follows:
Db. TestCollection.Person.ensureIndex ({name:1},{"unique": true})
Combined index
Sometimes our query is not single-condition, may be multi-conditional, then we can set up a composite index to speed up the query.
Db. TestCollection.Person.ensureIndex ({name:1,age:1})
Delete Index
Db. TestCollection.Person.dropIndex ("index name")
MongoDB Learning Notes (iii)