[Go] exploring mysql index structure principles, performance analysis, and optimization of bitsCN.com
Abstract:
|
Part 1: Basic Knowledge
Part 2: index structure of MYISAM and INNODB
1. B-tree B + tree
2. MyisAM index structure
3. Annode index structure
4. Comparison between MyisAM index and InnoDB index
Part 3: MYSQL optimization
1. table data type selection
2. SQL statement optimization
(1) leftmost prefix principle
(1.1) correct use of indexes
(1.2) incorrect index utilization
(1.3) if a query where clause does not require the password column, use "fill holes ".
(1.4) like
(2) Order by optimization
(2.1) filesort optimization algorithm.
(2.2) an index cannot be used for an order by statement. you can add a where clause or a limit clause to an index.
(2.3) where + orerby type, where satisfies the leftmost prefix principle, and the column subsets of the orderby column and the index used by the where clause. That is, (a, B, c) index. where satisfies the leftmost prefix principle and any combination of columns a, B, and c in order
(2.4) where + orerby + limit
(2.5) how to create an index using order
(3) isolate columns
(4) OR, IN, union all, you can try to use UNION ALL
(4.1) or will traverse the table even if there is an index
(4.2) about in
(4.2) UNION All
(5) fan Index selectivity
(6) duplicate or redundant indexes
3. system configuration and maintenance optimization
(1) important variables
(2) Fds optimize, Analyze, check, and repair maintenance operations
(3) update and maintain the table structure
Part 4: Figure 4-1 mysql Query execution process
|
Part 1: Basic Knowledge:
Index
The official introduction to index is a data structure that helps MySQL efficiently obtain data. I understand that indexing is equivalent to a book's directory. through the directory, I will know where the required information is. I don't need to check the required information on one page. Keyword index
-------------------------------------------------------------
Unique index
Unique indicates that the index value must be unique. the keyword unique index
Create an index:
1. createUnique indexIndex Name on table name (column name );
2. alter table name addUnique indexIndex Name (column name );
Delete an index:
1. drop index name on table name;
2. alter table name drop index name;
-------------------------------------------------------------
Primary key
A primary key is a unique index. the primary key must be specified during table creation. Generally, the auto_increatment column is used, and the key word is primary key.
Primary key creation:
Creat table test2 (id int not nullPrimary keyAuto_increment );
-------------------------------------------------------------
Full-text index
InnoDB does not support MySQL. Myisam supports good performance and is generally created on CHAR, VARCHAR, or TEXT columns.
Create table name (id int not null primary anto_increment, title
Varchar (100 ),FULLTEXT(Title) type =Myisam
------------------------------
Single column index and multi-column index
An index can be either a single column index or a multi-column index (also called a composite index ). The index created in the preceding format is a single-column index. now let's look at how to create multiple-column indexes:
Create table test3 (id int not null primary key auto_increment, uname char
(8) not null default '', password char (12) not null,INDEX(Uname, password) type
= Myisam;
Note: INDEX (a, B, c) can be used as an INDEX of a or (a, B), but it can be used with B, c or (B, c) this isLeftmost prefixThe optimization method will be described in detail later, as long as you know there are two concepts
-------------------------------------------------------------
Clustered index
An index. the logical order of the key values in the index determines the physical order of the corresponding rows in the table. The clustered index determines the physical sequence of data in the table. In Mysql, myisam tables do not have clustered indexes. innodb has (primary keys are clustered indexes). clustered indexes are described in detail in the following section about the innodb structure.
-------------------------------------------------------------
View table indexes
Run the Show index from table name command.
For example:
- Mysql> show index from test3;
- + ------- + ------------ + ---------- + -------------- + ------------- + ----------- + ------------- + ---------- + -------- + ---- +
- | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part |
- Packed | Null | Index_type | Comment |
- + ------- + ------------ + ---------- + -------------- + ------------- + ----------- + ------------- + ---------- + -------- + ---- +
- | Test3 | 0 | PRIMARY | 1 | id | A | 0 | NULL |
- NULL | BTREE |
- + ------- + ------------ + ---------- + -------------- + ------------- + ----------- + ------------- + ---------- + -------- + ------ +
Table: Table name
Key_name: what type of index (this is the primary key)
Column_name: Field name of the index column
Cardinality: index base. it is a key parameter. The average value group = index base/total table data rows. the closer the average value group is to 1, the more likely it is to use indexes.
Index_type: If the index is a full-text index, it is fulltext. here it is a B + tree index, and B + tre is also one of the focuses of this article.
For more information, see:
Part 2:MYISAMAndINNODBIndex structure
1,Brief IntroductionB-tree B + treeTree
B-tree structure View
A m-level B-tree has the following properties:
(1) Ki indicates the key value, medium, k1 <…<该关键字值<右子节点关键字值)< p><…<该关键字值<右子节点关键字值)<>
(2) Pi indicates the pointer to the child node, the left pointer to the left child node, and the right pointer to the right child node. That is: p1 [point to value] < p><>
(3) all keywords must have unique values (this is also the reason why primary keys are required for creating myisam and innodb tables). each node contains a keyword indicating the number of keywords of the node, for example, I and n in the second row
(4) nodes:
L each node can have up to m subnodes.
L if the root node is not a leaf node, it must have at least 2 subnodes and a maximum of m subnodes
L each non-root non-leaf node must be at least [m/2] subnode or a sub-tree ([] indicates an upward integer). a maximum of m subnodes can be used.
(5) Keywords:
L number of root node Keywords: 1 ~ M-1
L number of keywords for non-root non-leaf nodes [m/2]-1 ~ If m = 3, the number of keywords for this node is 2-1 ~ 2
(6) relationship between the number of keywords k and the number of pointers to sub-nodes p:
L k + 1 = p. Note that when the left and right pointers are Null, a flag is required to indicate no
The structure of B + tree is as follows:
The B + tree is a variant of the B-tree and also a multi-path search tree:
L The number of subtree pointers and keywords for non-leaf nodes is the same
L add a chain pointer to all leaf nodes (the arrow of the red dot sign)
The B + tree is a variant of the B-tree and also a multi-path search tree:
L The number of subtree pointers and keywords for non-leaf nodes is the same
L add a chain pointer to all leaf nodes (the arrow of the red dot sign)
2,MyisAMIndex structure
MyisAM indexes use B + tree to store data. MyisAM indexes point to the key-value address, which stores data, such:
(1) Structure Description: Level 3 tree. the primary key is Col2, and the Col value is the physical address for saving the modified data. the red part indicates the annotation.
L 1 the annotation part may be confused. I didn't mean that the key value of the right pointer of keyword 15 should be greater than 15. why are there 15 keywords below? Because B + tree, the leaf node contains all the keywords in ascending order (the primary key index is unique, and the secondary index may not be unique), so the data value equal to the keyword is in the right subtree.
L 2 annotation is the physical address for storing the corresponding data with the corresponding keywords. Note that this is also one of the differences between it and InnoDB indexes.
L 2 annotation is also called the MyiAM table index and data are separated, and the index is stored in the "table name. MYI, and the data is stored in the "table name. in the MYD file, the physical address marked by 2 is the table name. the physical address of the corresponding data in the MYD file. (The index file and data file of the InnoDB table are together)
L secondary indexes are similar to primary key indexes. secondary indexes can have duplicate index values (however, InnoDB secondary indexes differ significantly from primary key indexes, please note that)
3,AnnodeIndex structure
(1) first, there is a table. the content and primary key index structure are shown in the following two figures:
|
Col1 |
Col2 |
Col3 |
|
1 |
15 |
Phpben |
|
2 |
20 |
Mhycoe |
|
3 |
23 |
Phpyu |
|
4 |
25 |
Bearpa |
|
5 |
40 |
Phpgoo |
|
6 |
45 |
Phphao |
|
7 |
48 |
Phpxue |
|
......
|
Structure: we can see that the index structure of InnoDB is very different from that of MyisAM.
L The indexes and data in the MyisAM table are separated, and the pointer is used to point to the physical address of the data, while the indexes and data in the InnoDB table are stored together. You can see that a row of data is saved.
L There is also a hidden data column (dotted table) with three more rows. this is because MyisAM does not support transactions, and InnoDB is better at processing transactions in terms of performance concurrency control, as shown in the figure, the DB_TRX_ID in red box 2 is the transaction ID, which increases automatically; db_roll_ptr is the rollback pointer, which is used for data rollback and recovery when a transaction error occurs; db_row_id is the record row number, this value is actually the primary key value in the primary key index. it is marked as repeated here for easy introduction. if it is not a primary key index (secondary index), db_row_id will find the unique column in the table as the value, if no unique column exists, the system automatically creates one. About InnoDB and multi-transaction MVCC point this: http://www.phpben.com /? Post = 72
(2) in the preceding table, Col1 is the primary key (Mismark), While Col2 is a secondary index, the corresponding secondary index structure:
We can see that the InnoDB secondary index does not store all the corresponding column data, but stores the key values of the primary key (1, 2, 3 ....) The advantages and disadvantages of doing so are also obvious:
L existing primary key indexes are used to avoid data redundancy. when modifying data, you only need to modify the secondary index value.
L however, secondary indexes need to be searched twice. First, the corresponding primary key index value is found and then the corresponding data is found in the primary key index. This is also the reason why the "primary key as short as possible" mentioned in mysql Performance Optimization on the internet. the longer the primary key, the larger the secondary index, and the larger the primary key index.
4,MyisAMIndex andInnoDBIndex comparison
L MyisAM supports full-text indexes (FULLTEXT) and compressed indexes, which are not supported by InnoDB.
L AnnoDB supports transactions, but MyisAM does not.
L MyisAM stores data sequentially, and the index leaf node stores the corresponding row address. secondary indexes have very few primary key indexes. primary key nodes in AnnoDB store data rows at the same time. other secondary indexes store the value of primary key indexes.
L MyisAM key-value separation, index loading memory (key_buffer_size), data cache depends on the operating system; InnoDB key values are saved together, and indexes and data are loaded together into the InnoDB buffer pool
L MyisAM primary key (unique) indexes are stored in ascending order, while InnoDB is not necessarily
L The basic values of the MyisAM index (as can be seen by the Cardinality and show index commands) are precise, while InnoDB is the estimated value. This involves information Statistics. the MyisAM statistics information is stored on the disk and updated in the alter table or Analyze table operation, while InnoDB stores the estimated value in the cache zone when the table is opened for the first time.
L MyisAM stores string indexes incrementally. for example, if the first index is 'preform' and the second index is 'preformence ', the second index is '7, ance ', this obvious advantage is to shorten the index, but the defect is that it does not support reverse indexing and must be retrieved through sequential traversal.
Part 3:MYSQLOptimization
Mysql optimization is one of the major issues. here we will focus on mysql optimization, including table data type selection, SQL statement optimization, system configuration and maintenance optimization.
1,Table data type selection
(1) use a smaller value. The first principle of table data type is to use the shortest type that correctly represents and stores data. This reduces the usage of disk space, memory, and cpu cache.
(2) avoid using NULL, which is also the most popular online optimization technology blog. The reason is to add additional bytes and make the index and index statistics and values more complex. Many are ignored.
Count (column) does not count the number of rows whose column value is null.