11 MySQL's performance optimization

01- Introduction to Optimization

MySQL database optimization is multifaceted, the principle is to reduce system bottlenecks and reduce the footprint, increasing the response speed of the system.
1 , by optimizing the file system, improving disk I \ O sketches speed;
 2 , by scheduling strategy to optimize the operating system, to improve MySQL load capacity under high load conditions;
 3 , optimized table structure, indexes, queries etc. so that the query more responsive.

In MySQL, you can use SHOW STATUS statement to query some of the performance parameters of the MySQL database.
The grammatical structure is as follows:
SHOW STATUS LIKE 'value';
Common performance parameters are as follows:
· Connections: number of MySQL server connection;
· Uptime: on-line time MySQL server;
· Slow_queries: the number of slow queries;
· Com_select: the number of operations of the inquiry;
· Com_insert: insert the number of operations;
· Com_update: the number of operations update;
· Com_delete: the number of delete operations.

02- optimize queries

By analyzing the query, you can understand the implementation of the query, the query performed to identify bottlenecks to optimize queries.
MySQL provides EXPLAIN statement and DESCRIBE statement to analyze the query.

The basic syntax EXPLAIN statement is as follows:
EXPLAIN [EXTENDED] SELECT select_options
Additional information is generated using the keyword EXPLAIN, EXPLAIN statement.
select_options a query query options, including FROM WHERE clause and so on.

Executing the statement, the select statement can analyze the implementation of EXPLAIN later, and some features can be analyzed through the queried.
Example:
mysql> select * from student;
+------+--------+
| s_id | name   |
+------+--------+
| 1 | liming |
| 2 | wangwu |
| 3 | lysis |
+------+--------+
3 rows in set (0.04 sec)

mysql> explain select * from student;
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
|  1 | SIMPLE      | student | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    3 |   100.00 | NULL  |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

# 1, affect the speed of the index query 
performance improvements in MySQL in a most effective way is to design a reasonable data table index.
Using an index to quickly locate a record in the table, thereby increasing the speed of the database query.
Create an index:
create index index_name on student(name);

# 2, use the index query 
the index can improve query speed. But not the field with a query index, the index will work.
The following special circumstances indexes did not work:
( 1 ) using the LIKE keyword query
If the first character string is matching %, the index will not work. Only% not in the first position, the index will work.
Example:
mysql> create index index_name on student(name);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc select * from student where name like '%l';
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | student | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    3 |    33.33 | Using where |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> desc select * from student where name like 'l%';
+----+-------------+---------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
| id | select_type | table   | partitions | type  | possible_keys | key        | key_len | ref  | rows | filtered | Extra                 |
+----+-------------+---------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
|  1 | SIMPLE      | student | NULL       | range | index_name    | index_name | 768     | NULL |    2 |   100.00 | Using index condition |
+----+-------------+---------+------------+-------+---------------+------------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)

( 2 ) multi-column index query
MySQL can create indexes on multiple fields. 16 may include an index field. For the time index, only the query used in these fields in the first field, the index will be used.
Create multi-column index:
create index index_id_price on fruits(f_id, f_price);
( 3 ) using the OR keyword query
The query only query or keyword, and when two conditions before and after the columns are indexed, the query can use the index. Otherwise, the query will not use the index.
# 1, sub-queries to optimize 
MySQL 4.1 version from the start to support sub-queries, using sub-queries can be nested query select statement.
Although the sub-queries can be flexible query, but the efficiency is not high. The implementation of sub-queries, MySQL needs to create a temporary table as the result of the inner query.
Then the outer query query records from the temporary table.
After the query is complete, and then withdraw these temporary tables.

In MySQL, you can use JOIN query alternative sub-queries.
Join query does not need to create a temporary table, which is faster than the speed of the sub-query, if the query using the index, then the performance will be better.

03- optimize the database structure

Design of the database structure, you need to consider the data redundancy, update a query speed, the data type of the field is reasonable and many other content.

# 1, the table number field into a plurality of tables 
for more fields table, frequency of use is low some fields may be separated to form a new table these fields.
Because when the data table is large, due to the presence of low frequency of use field slowed.

# 2, increasing the middle of the table 
to table the need for frequent union query, you can create an intermediate table to improve query performance.
By establishing middle of the table, inserting data requires frequent joint inquiry into the middle of the table, and then the original query to a query on the joint middle of the table, in order to improve query performance.

# 3, increasing the redundancy field 
reasonably adding redundancy field can improve query speed.

# 4, the optimization of the insertion recording speed 
when recording is inserted, the insertion speed mainly affect the index, the only check, the number of records disposable insert. To optimize the method of inserting the recording speed:
    For MyISAM table engine:
    ( 1 ) Disable Indexing
    For non-empty table, insert records, MySQL will be indexed according to the index table to insert the record. If you insert a lot of data, indexing slows inserted record.
To address this situation, you can disable the index before inserting the record. After the data is completely inserted in the open index.
Disable Indexing statement as follows:
ALTER TABLE table_name DISABLE KEYS;
Where table_name is to disable indexing table name of the table.
Re-open the index statement is as follows:
ALTER TABLE table_name ENABLE KEYS;
For empty table bulk import data, you do not need to do this, because MyISAM engine indexing table is only after you import the data.

    ( 2 ) the only disabled check
    When inserting data, MySQL will insert a recording Jining uniqueness checking. This uniqueness check will slow inserted record.
    In order to reduce its impact on query speed, you can disable the uniqueness check before inserting the record until the recording is completed and then inserted into the open.
Disable uniqueness check statement is as follows:
SET UNIQUE_CHECKS = 0;
Open the uniqueness check statement is as follows:
SET UNIQUE_CHECKS = 1;
    ( 3 ) using bulk insert
    When inserting multiple records, may be used an INSERT statement to insert a record or records; simultaneous insertion of multiple statements faster.
    ( 4 ) using the LOAD DATA INFILE bulk import
   When bulk import data, if they can use LOAD DATA INFILE statement, as far as possible to use. Because the speed of LOAD DATA INFILE statement to import data faster than the insert statement.

    For InnoDB table engine:
    ( 1 ) disable the uniqueness check
    Before inserting data execution UNIQUE_CHECKS SET = 0 to disable the checking of the unique index, and then run the data import is completed. 1 = UNIQUE_CHECKS SET .
    ( 2 ) checking the foreign key is disabled
    Prohibit the implementation of the foreign key check before inserting data, then restore the data to check the foreign key after insertion is complete.
    Disabling foreign key checking statement as follows:
    SET foreign_key_checks=0;
    Restore external examination of key statement is as follows:
    SET foreign_key_checks=1;
    ( 3 ) prohibit automatic submission
    Automatic submission prohibited transaction before inserting data, after the data import is complete, perform a restore operation automatically submitted.
    Disable automatic submission of the statement is as follows:
    set autocommit=0;
    Automatic restoration statements submitted as follows:
    set autocommit=1;

 

# 5, table analysis, and optimization checklist table 
    analysis table is to analyze the distribution of keywords;
    Checklist mainly checklist for errors;
    Optimization of the main table is updated or deleted to eliminate wasted space caused.
    ( 1 ) analysis table
    MySQL provides analysis tables ANALYZE TABLE statement, the basic syntax rules are as follows:
ANALYZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tb1_name[, tb1_name]....
LOCAL keyword is an alias NO_WRITE_TO_BINLOG keywords, both of which are not written to the binary log execution, tb1_name analysis table is the table name, you can have one or more.

Process analysis using ANALYZE TABLE table, the database system will automatically add a read-only table index.
During analysis, only read records in a table, and inserted records can not be updated.
ANALYZE TABLE statement can analyze InnoDB, BDB and MyISAM table type.
Example:
mysql> ANALYZE TABLE student;
+-----------------+---------+----------+----------+
| Table           | Op      | Msg_type | Msg_text |
+-----------------+---------+----------+----------+
| test_db.student | analyze | status   | OK       |
+-----------------+---------+----------+----------+
1 row in set (0.01 sec)

    ( 2 ) checklist
    MySQL can be used to check the CHECK TABLE statement. CHECK TABLE statement checks InnoDB and MyISAM table types for errors.
    For MyISAM table types, CHECK TABLE statement will update the keyword statistics.
    CHECK TABLE can also check if there is an error view.
    The basic grammatical structure as follows:
    CHECK TABLE  tb1_name [,tb1_name] ... [option] ...
    option = {QUICK | FAST | MEDIUM | EXTENDED | CHANGED}
Which, tb1_name is the table name; option parameters have five values, namely QUICK | the FAST | MEDIUM, | EXTENDED | CHANGED is.
The meaning of each option are:
· QUICK: Do not scan lines
· FAST: Only check tables have not been closed properly
· CHANGED: just check the last inspection after the changes to the table and the table was not closed properly
· MEDIUM: scanning lines, in order to verify the deleted connection is valid.
· EXTENDED: for all keywords in each row will be a comprehensive keyword search.
option only for MyISAM table types valid for InnoDB table types invalid. CHECK TABLE statement in the implementation process will add to the table read-only lock.

    ( 3 ) optimization table
    MySQL use OPTIMIZE TABLE statement to optimize the table. The statement of InnoDB and MyISAM table types are there. However, OPTIMIZE TABLE statements can only be optimized table VARCHAR, BLOB or TEXT type of field.
  The basic syntax OPTIMIZE TABLE statement is as follows:
 OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tb1_name[, tb1_name]....

Eliminate file fragmentation can delete and update caused by OPTIMIZE TABLE statement. OPTIMIZE TABLE statement in the implementation process will add to the table read-only lock.

04- optimized MySQL server

# 1, optimizing server hardware 
server hardware performance directly determines the performance of the MySQL database.
Hardware performance bottlenecks MySQL database directly determine the operating speed and efficiency.
Here's how to optimize the server hardware:
( 1 ) large memory configuration.
( 2 ) high speed disk system configured to reduce the waiting time of the disk, increase the response speed.
( 3 ) reasonable distribution disk I \ O, the plurality of devices dispersed on the disk, in order to reduce competition for resources, improve the ability to operate in parallel.
( 4 ) a multiprocessor configuration, MySQL database is multi-threaded, multi-processor can execute multiple threads simultaneously.

# 2, parameter optimization of MySQL 
by MySQL optimization of the parameters can improve resource utilization, so as to improve the performance of the MySQL server.
MySQL server configuration parameters are in my.cnf or my.ini file [MySQLd] group.

 

Guess you like

Origin www.cnblogs.com/pgxpython/p/11736553.html