[MySQL study notes (9)] single table access method, index merge

This article is published by the official account [Developing Pigeon]! Welcome to follow! ! !


Old Rules-Sister Town House:

One. Single table access method

(1) Access method

       The query statement is essentially a declarative grammar, just telling MySQL which rules the data to be obtained conforms to, and how MySQL queries the data is the responsibility of MySQL itself. The way MySQL executes a query is called an access male method. The same query can be executed using a variety of different access methods, and the efficiency of different access methods is greatly different.

(Two) const

       Directly locate a record by comparing the equivalent value of the primary key column or the unique secondary index with the constant. Note that the two points are UNIQUE in common, and only one record can be queried. This direct access method is called const , Because the query cost is a constant level, the cost is negligible. For the unique secondary index column, when the query column has a NULL value, since the number of NULL values ​​is not limited, multiple records can be accessed, so the query NULL cannot use const.

(Three) ref

       The search condition is the equivalent comparison between the secondary index column and the constant, the scan interval formed is a single point scan interval, and the access method that uses the secondary index to execute the query is called ref. You can use the ref method to query NULL values.

(四) ref_or_null

       Sometimes we not only want to find out the record whose value in a secondary index column is equal to a certain constant, but also want to find out the record whose value in the column is NULL. This is one more access to query NULL value than the ref access method. The method is ref_or_null.

(Five) range

       For non-equivalent queries, such as range queries, the corresponding scan interval is a number of single-point scan intervals or range scan intervals. The access method is called range. The access method that only contains a single-point scan interval is not a range access method, nor is the scan interval of negative infinity-positive infinity.

(6) index

       The access method that directly scans all the secondary index records is called the index access method, and does not need to return to the table. It is also the index access method for adding ORDER BY.


(7) all

       Full table scan, directly scan all clustered index records as the all access method.


two. Index merge

       When using indexes to reduce the number of records that need to be scanned, under normal circumstances, only a scan interval is generated for a single index, but there are special cases. MySQL may also generate scan intervals for multiple indexes. This uses multiple indexes to complete a query The execution method is called index merge, and there are three types of index merge.

(1) Consolidation of intersection index

       Use multiple indexes to execute the query at the same time, query the query result of the first secondary index record and the query result of the second secondary index record at the same time, find the records with the same primary key value in the two results, and then according to These shared primary key values ​​are returned to the table, which saves a lot of cost of returning to the table, that is, take the intersection.

       To use this kind of index merging, the requirement is that the secondary index records obtained from each index are sorted according to the primary key. First of all, it is easier to take the intersection of an ordered set, and because if the secondary index records are in accordance with The primary key is sorted, then it is no longer random I/O when returning to the table based on these primary keys, because the records in the index page are also sorted according to the primary key, which improves efficiency.

(Two) Union index merge

       Taking the union of the results, the secondary index records obtained are also required to be sorted according to the primary key.

(Three) Sort-Union index merger

       Because Union index merging conditions are too harsh, we can use Sort-Union index merging, or perform index query at the same time, and then sort the queried secondary index records according to the primary key, and then merge it with the Union index.

Guess you like

Origin blog.csdn.net/Mrwxxxx/article/details/113829708