MongoDB Index
Indexes can usually greatly improve query efficiency. Without an index, MongoDB must scan each file in the collection when reading data and select those records that meet the query conditions.
The query efficiency of scanning the entire collection is very low. Especially when processing a large amount of data, the query can take dozens of seconds or even minutes, which is very fatal to the performance of the website.
An index is a special data structure. The index is stored in a data collection that is easy to traverse and read. The index is a structure that sorts the values of one or more columns in a database table
The above is the official explanation ()
What I want to know is that after creating an index, how does the database work to find the data when querying? Isn’t it the same as crawling all the databases? ? How to save time and how to work this Baidu for a long time, no one can explain,
To emphasize, I am not asking how to use indexes, I am asking how indexes achieve their purpose and save time
Essentially you can understand it as a 2-point search
Let’s search on Baidu. There are many documents. MongoDB's index is mainly a B-Tree index, and the principle of the B-Tree index of most databases is the same.