Get the App
SLTechnology News&Howtos  ›  Database  › 

[MongoDB Learning Notes 20] Index of MongoDB

Shulou Source: shulou.com Published: 2022-06-01 16:40:52 10月24日 Update

The indexing concepts and functions of MongoDB are the same as those of relational databases:

(1) A search that does not use an index can be called a full table scan, that is, the server must find the entire table before it can query the entire result.

(2) after the index is established, the query searches in the index, and after finding the entry in the index, it can jump directly to the location of the target document; this kind of search is several orders of magnitude faster than the whole table search.

First add 1000000 documents to the collection blog:

> for (iSuppli

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

> 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": 0, "millis": 344, "server": "localhost.localdomain:27017", "filterSet": false} >

Where millies indicates that the number of milliseconds spent on the search is 344ms

Where n means the number of search results after scanning the full table is 1. The search does not know how many username is user101. To optimize the query, limit the query result to 1, 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,102 "nscanned", "nscannedObjectsAllPlans": 102,102 "nscannedAllPlans", "scanAndOrder": false, "indexOnly": false "nYields": 0, "nChunkSkips": 0, "millis": 0, "server": "localhost.localdomain:27017", "filterSet": false} >

You can see that the millis is 0 because the number of scanned documents has been greatly reduced, and the query is completed almost instantly.

But this method is flawed, and if you look for users999999, you will still scan almost 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": 0, "millis": 321, "server": "localhost.localdomain:27017", "filterSet": false} >

The millis takes almost as long as searching the entire collection, and as the number of documents increases, the longer the query takes.

Create an index on the username field:

> db.users.ensureIndex ({"username": 1}) {"createdCollectionAutomatically": false, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1} >

Users who query users999999 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} >

The time spent on millis is 85, which is much less than that of 321 before indexing.

Of course, the index will speed up the query, but it also has drawbacks. Each time a document is added, deleted, or updated, MongoDB updates not only the document, but also the index on the document.

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

Tags: Index search document query quantity time result update field entry speed appropriate same important similar number that is to say representative location Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info NVidia Docker MariaDB OPPO Reno