MongoDB 2.2 Index

 

  • In mysql index has been learned and know to improve the query speed of the index
  • mongodb also supported the index to improve query speed

Step one: create large amounts of data

  • Execute the following code, insert 100,000 documents to the collection

for(i=0;i<100000;i++) {

    db.t1.insert({name:'test'+i, age:i})

}

Step two: Find data Performance Analysis

  • Find the name is 'test10000' documents

db.t1.find({name:'test10000'})

  • Use explain () command to query performance analysis

db.t1.find({name:'test10000'}).explain('executionStats')

  • Which under executionStats executionTimeMillis an overall query time, in milliseconds
  • Performance analysis results as shown below:

 

 

 Step Three: index

  • Creating an index
  • 1 for ascending, descending -1 for

. Db set .ensureIndex ({properties: 1})

db.t1.ensureIndex({name:1})

Step four: the index attribute query

  • Above the same query, and the query performance analysis

db.t1.find({name:'test10000'}).explain('executionStats')

  • Performance analysis results as shown below:

 

The index command

  • The establishment of a unique index, a unique constraint functions to achieve

db.t1.ensureIndex({'name':1}, {'unique':true})

  • Joint index, to establish an index of a plurality of attributes according to find () appears in the order

db.t1.ensureIndex({name:1, age:1})

 

  •  View all indexed documents

db.t1.getIndexes()

 

 

  • Delete Index

db.t1.dropIndexes ( 'Index Name')

Guess you like

Origin www.cnblogs.com/LiuYanYGZ/p/12241961.html