mysql-index; explain parameter

Mysql index

  • The definition
    of index Mysql's official definition of index is: index (index) is a data structure that helps Mysql to efficiently obtain data.
    Simply understood as: sorted and fast-checked data structure index will affect order by and where queries
  • The purpose
    of indexing is to efficiently improve query efficiency, which can be analogous to a dictionary

Insert picture description here
The index itself is also very large, it is impossible to store all of it in memory, so the index is often stored on the disk in the form of an index file

The indexes we usually talk about, unless otherwise specified, refer to the data-organized index of the B-tree (multiple search tree, not necessarily the binary tree), including clustered index, secondary index, composite index, prefix index, and unique Index, the default is to use B+ tree index, collectively referred to as index. Of course, in addition to the B+ tree type of index, there are hash indexes and so on.

Advantages of indexing

  • Advantages
    Reduce the IO cost of the database
    Sort data through index columns, reduce the cost of data sorting, and reduce CPU consumption

  • Disadvantages
    In fact, the index is also a kind of table. The table sets the primary key and index fields, and points to the records of the entity table, so the index column also takes up space.
    Although the index greatly improves the query speed, it will also reduce the speed of updating the table, such as INSERT, UPDATE and DETLETE on the table, because when updating the table, MYSQL not only saves the data, but also saves the index file. Each update adds an index. Column fields will adjust the index information after the key value changes brought about by the update.
    Indexes are just a factor to improve efficiency. If there are tables with a large amount of data in MYSQL, you need to spend time researching the best indexes for resumes or optimizing query statements.

Index classification

  1. Single-value index
    means that an index contains only a single column, and a table can have multiple single-column indexes.

  2. Unique index
    The value of the index column must be unique, but null values ​​are allowed

  3. Composite index
    Use bank card number and ID card number resume index in the banking system
    Suggestion: no more than five indexes should be established in one table

Ways to create and delete indexes

1. Ordinary index
CREATE INDEX indexName ON mytable(username(length));
directly specify when creating the table:

CREATE TABLE mytable(
ID INT NOT NULL,
username VARCHAR(16) NOT NULL,
INDEX [indexName] (username(length))
);
Syntax to delete an index:
DROP INDEX [indexName] ON mytable;

2. Unique index
It is similar to an ordinary index, except that the value of the index column must be unique, but null values ​​are allowed. If it is a composite index, the combination of column values ​​must be unique.

Create an index:
CREATE UNIQUE INDEX indexName ON mytable(columname(length))
Modify the table structure:
ALTER mytable ADD UNIQUE [indexName] ON (columname(length))

Specify directly when creating the table:
CREATE TABLE mytable(
ID INT NOT NULL,
username VARCHAR(16) NOT NULL,
UNIQUE [indexName] (username(length))
);

  • There are four ways to add the index of the data table:

1. ALTER TABLE tbl_name ADD PRIMARY KEY (column_list): This statement adds a primary key, which means that the index value must be unique and cannot be NULL.
2. ALTER TABLE tbl_name ADD UNIQUE index_name (column_list): The value of the index created by this statement must be unique (except for NULL, NULL may appear multiple times).
3. ALTER TABLE tbl_name ADD INDEX index_name (column_list): Add a common index, the index value can appear multiple times. 4. ALTER TABLE tbl_name ADD FULLTEXT index_name (column_list): This statement specifies that the index is FULLTEXT, which is used for full-text indexing.

For example:
create index: ALTER TABLE testalter_tbl ADD INDEX ©;
delete index: ALTER TABLE testalter_tbl DROP INDEX ©;

Show index information
SHOW INDEX FROM table_name\G

mysql index structure

- BTree

  • Hash index
  • full-text index
  • R-Tree index

Insert picture description here
Insert picture description here

Under what circumstances need to create an index
  1. The primary key automatically creates a unique index
  2. Fields frequently used as query conditions should be indexed
  3. Query the fields associated with other tables in the query, and create an index for the foreign key relationship
  4. Frequently updated fields are not suitable for index creation, because each update will not only update the record but also update the index
  5. Do not create indexes for fields that are not used in the Where condition
  6. The choice of single-value index and composite index, the tendency to create composite index under high concurrency
  7. The sorted field in the query, if the sorted field is accessed through the index, the sorting speed will be greatly improved
  8. Statistics or grouping fields in the query (the premise of grouping is to be sorted)
When not to create an index
  1. There are too few records in the table (the performance of Mysql above 3 million will start to decrease)
  2. Frequently add, delete and modify tables (although the query speed is improved, but at the same time it will reduce the speed of updating the table, such as Insert, Update, Delele on the table, because when updating the table, MySQl not only saves the data, but also saves the following index files )
  3. Table fields with repeated and evenly distributed data (such as nationality, gender, etc., indexing is meaningless)

If there are 10000 records in a table and the table index column has 9999 different values, then the selectivity of this index is 9999/10000. The closer the selectivity of an index is to 1, the higher the efficiency of the index.

Common bottlenecks in Mysql
  1. When the CPU is saturated, it usually occurs when data is loaded into memory or when data is read from disk.
  2. IO: Disk I/O bottleneck occurs when the loaded data is much larger than the memory capacity
  3. The performance bottleneck of server hardware: top, free, iostat and vmstat to view the performance status of the system
Query analysis tool Explain

Abbreviation: execution plan

Use the EXPLAIN keyword to simulate and optimize the execution of SQL query statements, so as to know how Mysql processes SQL statements, analyze query statements or the performance bottleneck of the table structure.

  1. What can Explain do
    Insert picture description here
  2. How to use
    Explain + SQL statementInsert picture description here

id: the sequence number of the select query, including a set of numbers, indicating the order in which the select is executed or the operation table is executed in the query

  • The id is the same, the execution order is from top to bottom
  • The id is different, if it is a self-query, the serial number of the id will increase, the greater the id value, the higher the priority, the earlier it will be executed
  • Same id, but different


Insert picture description here
The type of select_type query: mainly used to distinguish between ordinary query, joint query, self-query, nested query

  • simple: simple select query, the query does not contain self-query or UNION
  • PRIMARY: If the query contains any complex sub-parts, the outermost query is marked as PRIMARY
  • SUBQUERY: Subqueries are included in the select or where list
  • DERUIVED: derived temporary table
  • UNION: If the second select appears after union, it will be marked as union
  • UNION RESULT: SELECT to get the result from the UNION table


The row of table data is about that table

Type
access type arrangement The
Insert picture description here
common ones are:
from best to worst, in order: system>const>eq_ref>ref>range>index>ALL

Generally speaking, it is necessary to ensure that the query reaches at least the range level, preferably ref

  • system: There is only one row of records in the table, which is equivalent to a system table. This is a special case of the const type. It does not usually appear and can be ignored.
  • const: It means that it can be found once through the index. const is used to compare the primary key or the unique index. Because only one row of data is matched, it is very fast. If the primary key is placed in the where list, Mysql can convert the query to a constant
  • eq_ref: unique index scan, for each index key, there is only one record in the table that matches it, which is often common in primary key or unique index scans
  • ref: non-unique index scan, returning all rows matching a single value, is essentially an index access, it returns all matching a single row, however, it may find multiple rows of compound conditions, so He should belong to a mixture of search and scan
  • Range: Only retrieve rows in a given range, delimiting a range, and not searching one by one
    Insert picture description here
    Index: full index scan Insert picture description here
    ALL: full table scan, scan the entire table again (for data of one million or tens of millions of levels, all The query statement of the table scan must be optimized)

possible_keys
to determine whether the index is used, whether the index is invalid, which index mysql uses in the case of multiple index competition

It shows the indexes that may be applied to this table, one or more

The index actually used by keys. If it is NULL, the index is not used
. If a covering index is used in the query, the index only appears in the key list. The
Insert picture description here
above figure shows that there is no index in theory, and no index is actually used. It may be Because either the index is not created, or the index created is not used

Insert picture description here
Insert picture description here
Insert picture description here

The above figure shows that the index should not be used in theory, but the index
is actually used. This is because the covering index is used in the query, so the index only appears in the key list

key_len
Insert picture description here

ref shows which column of the index is used, if possible, is a constant, which column is constant or used to find the value on the indexed column.
Insert picture description here
rows According to table statistics and index selection, it is roughly estimated to find the required records, and the number of rows that need to be read
is as small as possible.

Extra (extra; extended)
contains other information that is not suitable for display in other columns, but is very important

Attributes:

  1. Using filesort : (nine deaths and deaths) indicates that mysql will use an external index to sort the data, instead of reading it according to the index order in the table. The sorting operation that cannot be completed by the index in Mysql becomes "file sorting" (must be optimized as soon as possible )
    Insert picture description here
    Insert picture description here
    The reason for using filesort is because the joint index is col1, col2, col3, but the first time because col2 is not used in sorting, the index is disconnected, and Using filesort appears.

2. Using temporary (ten dead without life)
uses a temporary table to save the intermediate results. Mysql uses a temporary table when sorting query results, which is common in sorting order by and grouping query group by
Insert picture description here
Insert picture description here

It can be seen that for the GROUP BY statement, either no index is used. If an index is used, it must be consistent with the order and number of indexes. Otherwise, temporary tables will be generated, which will seriously increase the burden on the database.

USING index (this is a good phenomenon)
indicates that the covering index is used in the corresponding select operation to avoid accessing the data rows of the table, and the efficiency is good.
If using where appears at the same time, it indicates that the index is used to search for index key values;
if using where does not appear

Insert picture description here
Insert picture description here
That is to say, if the range of the field you want to find is less than or equal to the index range, you can use the covering index, but because the select * is written so the range is larger than the index range, the covering index cannot be used, resulting in reduced query efficiency.

Guess you like

Origin blog.csdn.net/weixin_43941676/article/details/113880238