MySQL Execute Plan - Index Merge characteristics

Index Merge characteristics

In MySQL versions prior to 5.5, the query or sub-query are limited to the use of an index (excluding queries back to the table) in a table.

Suppose the table TB1001 C1 and C2, respectively, have single column index, such as the following query:

SELECT * FROM TB1001 WHERE C1='XXX' OR C2='XXX';

Either index alone can not meet all the conditions to obtain the data, so queries can only use a full table scan.

Index Merge introduced characteristic MySQL 5.5 version, allows:
a query on a table of a plurality of indexes and the plurality of scan range scan results are combined (UNION / INTERSECT).

Index Merge three kinds merging algorithm:

. 1 , Index the Merge the Intersect : intersection of a plurality of result sets
 2 , Index the Merge of Union : a plurality of result sets requirements UNION set (not sort the result sets)
 . 3 , Index the Merge the Sort - of Union : the first ordering of the plurality of result sets and then UNION seeking collection

 

 

Index Merge Intersect algorithm

When a plurality of filter conditions associated AND KEY different conditions of the query filter (WHERE part), such as:

Table # TB1001 main key index a PRIMARY KEY (ID) 
# TB1001 auxiliary table index IDX_C1 (C1) and secondary indexes IDC_C2 (C2) 

the SELECT  *  the FROM TB1001 the WHERE a C1 = ' XXX '  the AND C2 = ' XXX ' ;

When the pseudo-code implementation plan for the Index Merge Intersect algorithm does not use:

SELECT * FROM TB1001
WHERE ID IN (
SELECT ID FROM TB1001 WHERE C1='XXX')
AND C2='XXX';

Pseudo code execution plan when using Index Merge Intersect algorithms:

SELECT T2.* FROM (
SELECT ID FROM TB1001 WHERE C1='XXX'
INTERSECT
SELECT ID FROM TB1001 WHERE C2='XXX'
) AS T1
INNER JOIN TB1001 AS T2
ON T1.ID=T2.ID;

 

1 is assumed operating costs:

Assumptions: 
meet a C1 = ' XXX ' is recorded with 10,000 lines: each data page on the index IDX_C1 row index record 500 is stored, the condition data: 
    A, "sequentially stored" in the index IDX_C1 "continuous" index page 20 . 
    B, "dispersed stored" in the primary key "random" 2000 pages of data. 
Satisfies C2 = ' XXX ' is recorded with 20,000 lines, the data page is stored for each index IDX_C2 row index record 500, data satisfying the condition: 
    A, "sequentially stored" in the index IDX_C2 "continuous" index page 40. 
    B, "dispersed stored" in the primary key "random" 4000 pages of data. 
Satisfies a C1 = ' XXX '  the AND C2 = ' XXX ' in the line 200 is recorded, the data satisfy the condition: 
    A, "dispersed stored" on the primary key "random"

Using Index Merge Intersect algorithm requires "sequential read" index page 20 IDX_C1 + "sequential read" index page 40 IDX_C2 + "random access" primary key index data of 40 pages 
for the above case, using the algorithm Index Merge Intersect reduce the number of lookups, and the number of random read (down from 2,000 to 40) back to the table for the primary key.


2 operating cost assumptions:

Assumptions: 
meet a C1 = ' XXX ' in row 20 are recorded: the data page is stored for each index IDX_C1 row index record 500, data satisfying the condition: 
    A, "sequentially stored" in the index IDX_C1 "continuous" is an index page . 
    B, "dispersed stored" in the primary key "random" data of 20 pages. 
Satisfies C2 = ' XXX ' of row 200000 is recorded, the page data is stored for each index IDX_C2 row index record 500, data satisfying the condition: 
    A, "sequentially stored" in the index IDX_C2 "continuous" 400 of the index page. 
    B, "dispersed stored" in the primary key "random" 40,000 pages of data. 
Satisfies a C1 = ' XXX '  the AND C2 = ' XXX ' in row 19 are recorded, the data satisfy the condition: 
    A, "dispersed stored" on the primary key "random"

Using Index Merge Intersect algorithm requires "reading order" an index page IDX_C1 + "sequential read" index page 400 IDX_C2 + "random access" primary key index data of 19 pages 
for the above case, the algorithm needs Index Merge Intersect additional reading 400 IDX_C2 index pages to reduce back to the table once the primary key query and random read, is clearly worse performance.


Index Merge Intersect algorithms and characteristics Index condition Pushdown

In MySQL official documentation, Index Merge Intersect algorithm can be applied to each query using the primary and secondary key index, as:

SELECT *
FROM innodb_table
WHERE primary_key < 10
AND key_col1 = 20;

 

In earlier versions of MySQL process without introducing ICP characteristic, the filter condition (primary_key <10) is not "pushed down" key to query the primary key_col1 = 20 satisfies the condition, it is possible to use Index Merge Intersect back to the table lookup algorithm to reduce the number of .

During the introduction of ICP characteristic MySQL version, since the index records secondary indexes are included in the primary key column data, and therefore the filter condition (primary_key <10) can be "pushed down" to the primary key query satisfies key_col1 = 20 condition, no longer use Index Merge Intersect algorithms.

## in the MySQL . 5 testing .7 release
 the SELECT  * 
the FROM TB001
 the WHERE a C1 = 10 
the AND ID < 100 ; 
## execution plan: 
********************************************************** ******  1 Row. *************************** 
           the above mentioned id: 1 
  SELECT_TYPE: SIMPLE 
        the Table : TB001 
   Partitions: NULL 
         of the type: REF 
The possible_keys: a PRIMARY , IDX_C1
           Key : IDX_C1 
      The key_len: . 5 
          REF: const 
         rows: . 1
     Filtered: 33.33 
        Extra: the Using the WHERE ; the Using index 
## is not part of the implementation plan Extra information INDEX MERGE

 

Index Merge Intersect performance optimization problem

 

In some scenes, use the Index Merge Intersec algorithm will cause serious performance problems, DBA can turn this feature off by MySQL parameters optimizer_switch. 

For the benefit by Index Merge Intersec algorithm queries, consider using a combination of index or index coverage to replace the single-column index. 

As for the above query, the index IDX_C1 (C1) may be adjusted to IDX_C1_C2 (C1, C2), which query performance better.

 

 

Index Merge Union algorithm
when the query filter criteria (WHERE portion) of a plurality of filter conditions with OR KEY different association, such as:

Table # TB1001 main key index a PRIMARY KEY (ID) 
# TB1001 auxiliary table index IDX_C1 (C1) and secondary indexes IDC_C2 (C2) 
the SELECT  *  the FROM TB1001 the WHERE a C1 = ' XXX '  OR C2 = ' XXX ' ;

The steps are as follows:

1 , using IDX_C1 index obtaining satisfying the condition of [ a C1, ID ] recording, the recording default according ID sort
 2 , using IDX_C1 index obtaining satisfying the condition of [ a C1, ID ] recording, the recording default according ID sort
 3 , which has been in accordance with the ID data sorting steps 1 and 2 are combined to re-ID.
4 , to find and return back to the table by ID

Pseudo-code:

The SELECT T2. *  The FROM (
 the SELECT ID the FROM TB1001 the WHERE a C1 = ' XXX ' 
the UNION 
the SELECT ID the FROM TB1001 the WHERE C2 = ' XXX ' 
) the AS Tl
 the INNER  the JOIN TB1001 the AS T2
 the ON t1.id = t2.ID 

create the index IDX_C1 (ID) when, equivalents, recording the same value C1 sorted ID value IDX_C1 (C1, ID), so that the two operations in the see UNION ordered result set when the ID.

 

Index Merge Sort-Union algorithms

When a plurality of filter conditions associated OR KEY different conditions of the query filter (WHERE part), such as:

Table # TB1001 main key index a PRIMARY KEY (ID) 
# TB1001 auxiliary table index IDX_C1 (C1) and secondary indexes IDC_C2 (C2) 
the SELECT  *  the FROM TB1001 the WHERE a C1 > ' XXX '  OR C2 < ' XXX ' ;

The steps are as follows:

1 , using IDX_C1 index obtaining satisfying the condition of [ a C1, ID ] recording, and then sorted by ID
 2 using IDX_C1 index obtaining satisfying the condition of [ a C1, ID ] recording, then in accordance ID sort
 3 , Step 1 and step 2 have been consolidated by the sorted data deduplication ID ID.
4 , to find and return back to the table by ID

Pseudo-code:

The SELECT T2. *  The FROM (
 the SELECT ID the FROM TB1001 the WHERE a C1 > ' XXX ' 
the ORDER  BY ID
 the UNION 
the SELECT ID the FROM TB1001 the WHERE C2 > ' XXX ' 
the ORDER  BY ID 
) the AS Tl
 the INNER  the JOIN TB1001 the AS T2
 the ON t1.id = t2.ID 

in when creating an index IDX_C1 (ID), which is equivalent to IDX_C1 (C1, ID), a data range of the column C1 query returns data in accordance with C1 +ID sort, in the ID column is disordered, thus the need to sort the result sets before the two intermediate UNION operation.


Index Merge Union related optimization
when disabled Index Merge feature, the OR operator can rewrite the SQL UNION ALL operation, the query uses multiple indexes.

As used above query Index Merge Union algorithm can be rewritten as:

#改写前:
SELECT * FROM TB1001 WHERE C1='XXX' OR C2='XXX';

# 改写后
SELECT T2.* FROM (
SELECT ID FROM TB1001 WHERE C1='XXX'
UNION ALL
SELECT ID FROM TB1001 WHERE C2='XXX' AND (C1<>'XXX' OR C1 IS NULL)
) AS T1
INNER JOIN TB1001 AS T2
ON T1.ID=T2.ID

PS: The IDX_C2 (C2) is rewritten as IDX_C2_C2 (C1, C2) to avoid back to the table before the UNION operation query.

Guess you like

Origin www.cnblogs.com/gaogao67/p/12167967.html