In layman's language Mysql - SQL Optimization

A. The general steps to optimize SQL statements

1.1 show [session | global] status display session (the session, the default level) or global value (since the database since the last start) all statistical parameters:

  Execution show status like 'Com_%'

         Com_xxx xxx represents the number of times each statement was executed, usually has the following parameters:

    Com_select number of select, accumulate a query 1

    Com_insert number of insert, bulk insert accumulated only once

    Com_update number of operations to perform update

    Com_delete execute delete number

 

  For InnoDB engine, accumulating a slightly different algorithm

    Innodb_rows_read select query number of rows returned, others were the case (number of rows affected).

 

1.2 Positioning low efficiency in the implementation of SQL statements

  (1) slow query log by positioning the lower the efficiency of SQL. Slow query log is off by default, when using --log-slow-queries option to start, mysqld write a log file of all the execution time than long_query_time seconds SQL statement contains. Currently recommended --slow-query-log displays the status of the specified slow query, use slow_query_log_file to specify the path to the slow query log.

  set long_query_time; (default 10s, can be accurate to 0.01s) more localhost-slow.log; (see the slow query log)

   If the slow query log records a lot of content, you can use the tool to slow query log mysqldumpslow subtotal,

  例:mysqldumpslow bj37-slow.log;

       (2) show processlist can use the command to view the current thread MYSQL underway, including the state of the thread, such as whether the lock table. Real-time view of the implementation of SQL.

 

1.3 Analysis of inefficient SQL execution plans Explain

  Implementation plan has the following columns illustrate:

  select_type: select the type indicates, there are simple, primary, union, subquery several other

  table: Table output set

  type: MYSQL find a way to express the desired line in the table, or call the access type. Common types are: ALL | index | range | ref | eq_ref | const | null from left to right, by the poor performance to good

            ALL: full table scan

       index: Index full scan

     range: index range scan, common <, <=,>,> =, between the operator and the like    

     ref: scanning prefix or a non-unique index scan using a unique index such as under conditions where uid = 20;

     eq_ref: unique indexes, such as using a.aid = b.bid under conditions where;

     const / system: single table has at most one matching row, the query very quickly, so the value of the other columns of this line of matching optimizer can be used as constants in the current query to deal with. The example query a primary key or a unique primary key index, such explain select * from (select * from customer where email = '[email protected]' \ g);

     NULL: MYSQL without accessing the table or index, a result can be obtained directly, for example: explain select 1 from dual where 1 \ G

  

  In addition, mysql introducing explain extended commands, plus show warnings, see SQL optimization can be performed before actually doing what SQL is rewritten.

 

1.4 Analysis of SQL by show profile

  By have_profiling parameters, be able to see the current MYSQL support profile: select @@ have_profiling;

  The default profiling is off, you can open profiling in Session level set by the statement: select @@ profiling

  By profile, we can more clearly understand the process of execution of SQL

    First execute the statement: select count (*) from payment;

    Then the statement analysis: show profiles;

    4, according to the statement analysis select query ID of the statement is: show profile for query 4; see the statement during execution time consumed by each state, and

 

  Secondly, can also view the SQL query parsing during execution of each step corresponding to the source code file, a function name and a specified number of lines of source files show profile source for.

 

1.5 How to choose the implementation plan by trace analysis optimizer

  First, open the trace, set the format to JSON, set the maximum memory size can be used

     set OPTIMIZER_TRACE=“enabled=ON”,END_MARKERS_IN_JSON=on;

     set OPTIMIZER_TRACE_MAX_MEM_SIZE=1000000;

  Enter to execute the statement: select count (*) from payment;

  Check the INFORMATION_SCHEMA.OPTIMIZER_TRACE can know how the implementation of SQL MYSQL

    select * from INFORMATION_SCHEMA.OPTIMIZER_TRACE/G;

    

Second, indexing issues ( see in particular   https://blog.csdn.net/liutong123987/article/details/79384395 )

   

2.1 Mysql present, there are several index types: FULLTEXT, HASH, BTREE, RTREE.

  1. FULLTEXT
    is the full-text index, only MyISAM engine support. It can be CREATE TABLE, ALTER TABLE, CREATE INDEX use, but can now create full-text index is only CHAR, VARCHAR, TEXT columns. MyISAM and full-text indexing is not born together, it appears to solve the "% word%" low efficiency of this type of fuzzy search for the text of the problem WHERE name LIKE.

  HASH 2.
    As the sole (the sole of almost 100%) and the like HASH key-value pairs, it is suitable as an index. HASH index can locate one, do not need to look like a tree indexes as layer by layer, and therefore has a very high efficiency. However, this efficiency is conditional, that is, only the "=" and "in" under conditions effective for the scope of inquiry, ordering and combination index is still inefficient.

  BTREE 3.
    BTREE index is a kind of index value by a certain algorithm, into a tree data structure (binary tree), each query begins at the entrance root of the tree, in order to traverse node, obtain leaf. This is the default in MySQL and the most common type of index.

  RTREE 4.
    RTREE rarely used in MySQL, supports only geometry data type, the type of storage engine supports only MyISAM, BDb, InnoDb, NDb, Archive few. Relative to BTREE, RTREE strength lies in the range of search.

 

2.2 Index types of
  ordinary index: only speed up queries

  The only index: + speed up query column value unique (there may be null)

  Primary key index: a unique value to speed up queries + column (not have null) only one table +

  Composite index: a multi-column index value of the composition, specifically for combinatorial search, the efficiency is greater than the combined index

  Full-text indexing: the content of the text word segmentation, search

  The combined index, a separate index using multiple combinations of search

  Covering index, select the columns of data can be acquired only from the index, without reading the data line, in other words a query column to be built index covering

   Operating Index:

1. Create an index
- Create a regular index CREATE INDEX index_name ON table_name (col_name) ;

- create a unique index CREATE UNIQUE INDEX index_name ON table_name (col_name);

- Create a regular composite index CREATE INDEX index_name ON table_name (col_name_1, col_name_2);

- Create a unique composite index CREATE UNIQUE INDEX index_name ON table_name (col_name_1, col_name_2);


2. Create a table structure indexed by modifying
ALTER TABLE table_name ADD INDEX index_name (col_name );


3. Direct table index specified when creating
the CREATE TABLE table_name (
    ID the INT the NOT NULL, col_name VARCHAR (16) the NOT NULL, the INDEX index_name (col_name)
);


4. Delete Index
- delete the index DROP INDEX index_name ON table_name; - modified to delete the index table structure ALTER TABLE table_name DROP INDEX index_name;

Mysql can use scene indexes:

  1. matching full value: the index concrete columns specify a specific value, the analysis found that the statement type = const

  2. The range of values ​​matching the query: the index value can range searches. type = range

  3. The most left-prefix match: Use only the leftmost column index to find. type = ref; e.g. index col1 + col2 + col3 joint index, col1, col1 + col2, col1 + col3 found equivalent index can be used, but col2, col2 + col3 can not be found by using the index

  4. only the index for the query when the query field column in the index, the more efficient query. Analysis found that the statement type = ref, Extra = Using index;

  The prefix match column: only the first column of the index, and contains only a portion of the head of the first column of the index to find. type = range

  6. Part precise index matching can be achieved while other parts range matching, type = ref

  7. If the column name is an index, use column_name is null will use index (different from the oracle)

 

Index exists but can not use an index of typical scenes

  1. LIKE% at the beginning of the query can not use B-Tree indexes, key implementation of the plan is NULL means no use of the index

    E.g. explain selecy * from actor where last_name like '% NI%' \ G; not use the index, using the full-text index generally similar to solve the problem, one embodiment of the optimization method is scanned first two conditions satisfied index idx_last_name actor_id primary key list, then to retrieve records by primary key back to the table, to avoid a large number of the IO request full table scan table generated actor.

  2. When there is an implicit data type conversion will not use the index, especially when the column type is a string, must remember that in the condition where a character constant values ​​in quotation marks.

  3. The composite index of the case, the most left-prefix does not meet the principle, not the use of a composite index

  4. How is estimated using the index Mysql slower than a full table scan, the index is not used

  The conditions of use or separated, if only the first condition listed index, is not followed, then the index will not be involved used

 

View usage index

  show status like 'Handler_read%';

  Handler_read_rnd_next value represents the number of the data file read request is the next line, if the table index value is generally higher Description incorrect or not they use the write index.

 

 

Simple and practical optimization 2.3

  Table and regular analysis checklist: analyse table tbl_name, keyword analysis and storage for distribution list, results of the analysis will be able to make the system to get accurate statistics that can generate the correct SQL execution plan. If you feel the actual implementation of the plan is not expected to perform planning, execution time analysis table may solve the problem.

    Checklist: check table tbl_name; format checklist action check one or more tables for errors, check whether the view may be wrong, such as in the view definition referenced table does not exist.

  Periodic Table Optimization: optimize table tbl_name; this command to space debris of the table to merge, and can be deleted or updated eliminate waste caused by space.

 

Third, the commonly used SQL optimization

3.1 bulk insert  

  For InnoDB tables, the following ways:

    (1) Because InnoDB table is stored in the order of the primary key, the data arrangement order of the introduced primary key, can effectively improve the efficiency of import data

    (2) into the data before executing SET UNIQUE CHECKS = 0, turning off the uniqueness check, performed after completion of the introduction SET UNIQUE CHECKS = 1, the recovery check uniqueness

    (3) If the application uses an automatic manner submitted, before the implementation of the recommendations introduced SET AUTOCOMMIT = 0, turn off the automatic submission, after the end of execution introduced SET AUTOCOMMIT = 1, auto-commit

  For MyISAM tables, through the following means:

    ALTER table tbl_name DISABLE KEYS; (closed update non-unique index)

    loading the data

    ALTER table tbl_name ENABLE KEYS;

 

3.2 INSERT statement

  (1) make use of multiple insert statement value table, insert into test values ​​(1,2), (1,3), (1,4) ... greatly seen client connected to the database, and the like closing consumption

  (2) If the customer is a different insert many rows, you can get higher speed by INSERT DELAYED statement, meaning DELAYED is to immediately execute an INSERT statement, in fact, the data are stored in the memory queue, and not really written to disk, which each statement than were inserted much faster.

  (3) when loading a table from a text file, using the LOAD DATA INFILE INSERT generally much faster than 20 times

 

3.3 ORDER BY statement

  Minimize additional sorting, orderly return data directly through the index.

  (1) and avoid mixing DESC ASC sekect * from tbl_name ordery by key1 DESC, key2 ASC;

  (2) select a keyword query for different rows in order by using the * from tbl_name where key2 = 'constant' order by key1;

  (3) using the order by select * from tbl_name order by key1 for different keywords, key2;

  Filesort optimization: a scanning algorithm? Disposable taken to meet the conditions of all the columns of the row, then a direct current output after reordering to sort the sort buffer zone.

  (1) appropriate to increase the value of system variables max_length_for_sort_data that allows MYSQL choose a more optimal sorting algorithm Filesort

  (2) appropriate to increase sort_buffer_size sort area, try to make the sorting is done in memory, rather than by creating a temporary table in the file.

  (3) try to use only the necessary fields, SELECT specific field names rather than solve *

 

3.4 GROUP BY statement

  By default, MySQL carried out for all group by the sort field, which is similar to the order specified by the query, therefore, if the display including the order that contains the same column by clause, there is no effect on the actual execution performance of MySQL. If the query includes GROUP BY but you want to avoid consuming to sort the results, you can specify the sort order by null ban in order to avoid the emergence of Filesort.

 

3.4 nested query

  Use join, connect the left and right, inside and outside connections

 

3.5 OR condition

  If you want to use the index, or the need to ensure the conditions for each column must use the index, if not then consider increasing the index.

 

3.6 paging query

  General paging query by creating a covering index can better improve performance.

 

The use of SQL prompt

4.1 USE INDEX

  Price increases use index to provide the desired area MYSQL reference index list, you can no longer make MYSQL consider other available indexes.

 

4.2 IGNORE INDEX

  Allowing users to ignore one or more indexes.

 

4.3 FORCE INDEX

  To force MySQL to use an index, even with a full table scan faster must also use the index.

  

Guess you like

Origin www.cnblogs.com/Edword-ty/p/11027984.html