Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

[MongoDB Learning Notes 20] Index of MongoDB

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report