Index of "MongoDB Learning note 20" MongoDB

Source: Internet
Author: User

The index and relational database indexing concepts and functions of MongoDB are the same:

(1) Non-indexed search can be called full table sweep, that is, the server must find a complete table in order to query the entire result;

(2) After the index search, the query search in the index, the entry in the index to find the entry, you can jump directly to the location of the target document, such a search than the whole table to improve the speed of several orders of magnitude;

To add 1 million documents to a collection blog first:

> for (i=0;i<1000000;i++) {... db.users.insert (... {"I": I, ... "username": "User" +1, ... "Age": Math.floor (Math.random () *120), ...    "Created": New Date ()}); Writeresult ({"ninserted": 1}) >


Randomly query a document in the above collection and use the Explain function to view the information in the search process:

> db.users.find ({"username": "user101"}). Explain ()    {          "cursor"  :  "Basiccursor",         " Ismultikey " : false,        " n " : 1,          "Nscannedobjects"  : 1000000,          "nscanned"  : 1000000,         " Nscannedobjectsallplans " : 1000000,        " Nscannedallplans " : 1000000,        " Scanandorder " : false,          "IndexOnly"  : false,          "Nyields"  : 7812,         "Nchunkskips" &NBSP;:  0,         "Millis"  : 344,         "Server"  :  " localhost.localdomain:27017 ",        " FilterSet " : false     }    >


Where millies indicates that the number of milliseconds spent in the search is 344 milliseconds;

where n means the number of results after scanning the full table is 1, and the search does not know the number of username as user101, limiting the results of the query to 1 for the optimization query, so that the search stops after the first document is found:

> db.users.find ({"username": "user101"}). Limit (1). Explain ()    {          "cursor"  :  "Basiccursor",         " Ismultikey " : false,        " n " : 1,          "Nscannedobjects"  : 102,          "nscanned"  : 102,         " Nscannedobjectsallplans " : 102,        " Nscannedallplans "  : 102,         "Scanandorder"  : false,          "IndexOnly"  : false,          "Nyields"  : 0,         "Nchunkskips"  : 0,          "MIllis " : 0,        " Server " : " localhost.localdomain:27017 ",        " FilterSet " : false     }    >


You can see that Millis is 0 because the number of scanned documents is greatly reduced, and the query is almost instantaneous;

But this method has flaws, and if you look for users999999, you still almost scan the entire collection.

> db.users.find ({"username": "user999999"}). Limit (1). Explain ()    {          "cursor"  :  "Basiccursor",          "Ismultikey"  : false,         "n"  : 1,          "Nscannedobjects"  : 1000000,          "nscanned"  : 1000000,         " Nscannedobjectsallplans " : 1000000,        " Nscannedallplans " : 1000000,        " Scanandorder " : false,          "IndexOnly"  : false,          "Nyields"  : 7812,         "Nchunkskips" &NBSP;:  0,         "Millis"  : 321,         "Server" &NBSP;:   "localhost.localdomain:27017",         "FilterSet"  :  False    }    >


The time spent almost as much as the search for the entire collection is millis to 321, and the longer the query takes, as the number of documents increases;

To create an index on the username field:

> Db.users.ensureIndex ({"username": 1}) {"createdcollectionautomatically": false, "Numindexesbefore": 1, "Numindexesafter": 2, "OK": 1} >

To query users999999 users again:

> db.users.find ({"username": "user999999"}). Limit (1). Explain ()    {          "cursor"  :  "Btreecursor username_1",          "Ismultikey"  : false,         "n"  : 1,         "Nscannedobjects"  : 1,          "nscanned"  : 1,         " Nscannedobjectsallplans " : 1,        " Nscannedallplans "  : 1,         "Scanandorder"  : false,          "IndexOnly"  : false,          "Nyields"  : 0,         "Nchunkskips"  : 0,          "Millis"  : 85,         "Indexbounds"  : {              "username"  : [                 [                      "user999999",                       "user999999"                  ]            ]         },         "Server"  :  " localhost.localdomain:27017 ",        " FilterSet " : false     }  &Nbsp; > 

The time spent Millis is 85, much less than the 321 before the index was created;

Of course, the index will speed up the query, but there are drawbacks, each time to add, delete, update a document, MongoDB not only to update the document, but also to update the index on the document;

There can be only 64 collections per collection, and it is important to pick the appropriate fields to index.



This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1600482

Index of "MongoDB Learning note 20" MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.