MySQL defines the index as:
A data structure that helps MySQL to get a high-efficiency database.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4D/F4/wKioL1Rdfr3QNdE1AAE_RU-UYP8119.jpg "title=" Index01.png "alt=" Wkiol1rdfr3qnde1aae_ru-uyp8119.jpg "/>
Analysis: A. Rows are stored in the file. B. Rows, tables are the concept of a database, and the operating system does not know these logic. C.mysql's MyISAM storage engine stores data from one table to one file, and InnoDB by default, you will data from different tables is stored in a single file. D. Each file can be divided into pages. For example, 5 pages are divided. E. To add data, MySQL inserts data on the last line of the last page, and if the last page is full, You need to be reborn into a page. F. If there are gaps in each page, how is this done? According to E, it is certainly not the result of adding data. This is caused by the deletion of data. In other words, the deletion causes the page to fragment. So why doesn't MySQL automatically fill this void when it's added? If a table is large, Every time MySQL adds data, it needs to go from top to down to find gaps, so there's a big delay. G. How many lines can a page have? For example, a page size of 4KB, a row of records occupy 90 bytes, then 4*1024/90=45, that is to say probably a page to place 45 data. The size of the page is related to the operating system and database. H. page is the basic unit of I/O. The database could not instruct the operating system to get some records or get a few bytes of data, only to tell the loading Which pages to memory, and then the database finds the desired data in the page. In other words, a record should be The page mark + line marks together constitute the only indication of this record.
If a table is large, there are millions of rows, each time using the sequential search line, it is bound to inefficient. And the index, this data structure, provides another way to find a table.
Look at a simplified version of the MySQL index: "Build the No column of the table in the diagram above"
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/4D/F5/wKiom1RdhW_TZFceAAFQ4WQgjnA855.jpg "title=" Index05.png "alt=" Wkiom1rdhw_tzfceaafq4wqgjna855.jpg "/>
A. The data structure of an index is represented as a multi-node tree B. There is a root node C. The values within the node are ordered D. You can see that the value of the node that the root node of 44 P1 points to is less than or equal to 44. Actually the other nodes It's all the same. E. The pointer px in the node, either pointing to the next node, such as P3, or to a record in the table, such as P6. for P6 this point The node that is recorded in the table is the leaf F. The leaves are linked, meaning that a leaf points to the next leaf G. Actually P6 equivalent to the page Mark + line mark
Case one: Find all rows of no=39 using an index
The first step, starting from the root node, at which point the root becomes the active node The second step is to determine if the active node is not a leaf, and if it is not a leaf, proceed to the third step; Fourth Step The third step, because of the 39<44, so P1 point to the node becomes the active node, continue the second step Fourth, find the value in the active node, remove the pointer, that is, the value of the page marked Fifth, the database instructs the operating system to load these pages, and then find the no=39 rows in these pages
Visible, find a specific value for all the rows, MySQL takes advantage of the index and does not need to browse all the rows that can be quickly found.
Case two: Get all rows sorted by no
Find the leaf directly, get all the pages of P6 first, then all the pages of P7. Because there are leaves behind the leaves, the next leaf continues the process.
no=2, I need to take the second page. No=6, you need to take the first page No=7, I need to take the third page. No=8, I need to take page fourth. no=27, I need to take the second page. ...
It can be found that because the rows in the file are not sequential, some pages must be fetched multiple times, which increases Processing time. (The number of pages in memory is limited, and it is likely that some pages are not already in memory, so you must Get the page back from the hard drive)
In order to speed up this process, the rows in the file must be stored in an orderly fashion, so it is possible that each page only needs to get Once, for example no=2 in x page, then no=6 does not need to fetch the page again, because the correct page already exists memory , MySQL understands this. "Above is the concept of a clustered index"
|