MySQL Database Optimization (1)

Foreword

For no matter what kind of service, for their optimization, is that from two aspects, the first is for hardware optimization, the second is the optimization of the system as well as the service itself.

1, the number of queries to connect to MySQL server

mysql> show status like 'connections';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Connections   | 3     |
+---------------+-------+
1 row in set (0.01 sec)

2, MySQL query runtime server

mysql> show status like 'uptime';      //单位为“秒”
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Uptime        | 127   |
+---------------+-------+
1 row in set (0.00 sec)

3, the number of query operations

mysql> show status like 'com_select';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_select    | 12    |
+---------------+-------+
1 row in set (0.00 sec)

4, the number of insertion operations

mysql> show status like 'com_insert';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_insert    | 1     |
+---------------+-------+
1 row in set (0.00 sec)

5, the number of update operations

mysql> show status like 'com_update';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_update    | 1     |
+---------------+-------+
1 row in set (0.00 sec)

6, the number of delete operations

mysql> show status like 'com_delete';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_delete    | 0     |
+---------------+-------+
1 row in set (0.00 sec)

7, the number of queries MySQL slow query servers

mysql> show status like 'slow_queries';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries  | 21    |
+---------------+-------+
1 row in set (0.00 sec)

Second, SQL statement analysis

1, explain keywords analysis

mysql> explain select * from stu_info\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE 
        table: stu_info         #表名
   partitions: NULL
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL     #使用哪个列或常数与索引一起使用来查询记录
         rows: 3
     filtered: 100.00
        Extra: NULL
1 row in set, 1 warning (0.00 sec)

Select_type above is explained as follows:

  • Select_type: indicates the type wherein the select statement is simple simple queries (excluding join queries and subqueries) Primary main query Union join query;

2, use the index to improve query efficiency

mysql> explain select * from stu_info where s_id=3\G     #没有索引时的查询结果分析如下
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: stu_info
   partitions: NULL
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 3        #需要查询三行才能查到(这个表数据总共也就三行)
     filtered: 33.33
        Extra: Using where
1 row in set, 1 warning (0.00 sec)

mysql> create index index_01 on stu_info(s_id);     #创建索引
mysql> explain select * from stu_info where s_id=3\G   #再次进行查询
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: stu_info
   partitions: NULL
         type: ref
possible_keys: index_01   #使用的是哪个索引名称
          key: index_01
      key_len: 5
          ref: const
         rows: 1     #创建索引后,查询1行就查到可。
     filtered: 100.00
        Extra: NULL
1 row in set, 1 warning (0.00 sec)

Use the index note the following:

  • After indexing with like 'xx%'% is not the first query highest efficiency;
  • If multiple index fields, in addition to the first query field the fastest, and the rest will not be according to the index, the index does not take effect;
  • To create an index field set, a query about the value of an index or a combination of both sides belong to the index value set field.

Other notes on using the index, you can refer Bowen: MySQL index type explain ;

Three, profiling analysis inquiry

By slow query log can know which SQL statement is executed inefficient, we can see that through explain the specific implementation, the index using SQL statements, etc., can also be combined show command to view the execution status. If you feel that explain information is not detailed enough, you can do with the information system resource consumption through more accurate profiling SQL command. profiling is disabled by default. You can view the following statement:

1, to see whether to open profiling

mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| have_profiling         | YES   |
| profiling              | OFF   |            #OFF表示未开启
| profiling_history_size | 15    |
+------------------------+-------+
3 rows in set (0.00 sec)

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 |                   # 0表示未开启
+-------------+
1 row in set, 1 warning (0.00 sec)

2, open profiling

mysql> set profiling=1;          #开启
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select @@profiling;    #qu
+-------------+
| @@profiling |
+-------------+
|           1 |
+-------------+
1 row in set, 1 warning (0.00 sec)

3, execute SQL statements to be tested

mysql> select * from bank;
+-------+-------+
| name  | money |
+-------+-------+
| lu    |  1000 |
| qi    |  1000 |
| zhang |  2000 |
+-------+-------+
3 rows in set (0.00 sec)

4, see the SQL statement corresponding to the ID, analyze

mysql> show profiles;
+----------+------------+--------------------+
| Query_ID | Duration   | Query              |
+----------+------------+--------------------+
|        1 | 0.00012925 | select @@profiling |
|        2 | 0.00401325 | SELECT DATABASE()  |
|        3 | 0.01405400 | show databases     |
|        4 | 0.00034675 | show tables        |
|        5 | 0.00011475 | show tabels        |
|        6 | 0.00029225 | show tables        |
|        7 | 0.00041200 | select * from bank |
|        8 | 0.00020225 | select * from bank |
+----------+------------+--------------------+
8 rows in set, 1 warning (0.00 sec)
mysql> show profile for query 7;     #查询sql语句的详细分析
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000161 |
| checking permissions | 0.000010 |
| Opening tables       | 0.000016 |
| init                 | 0.000047 |
| System lock          | 0.000013 |
| optimizing           | 0.000004 |
| statistics           | 0.000013 |
| preparing            | 0.000009 |
| executing            | 0.000004 |
| Sending data         | 0.000050 |
| end                  | 0.000004 |
| query end            | 0.000008 |
| closing tables       | 0.000007 |
| freeing items        | 0.000012 |
| logging slow query   | 0.000041 |
| cleaning up          | 0.000013 |
+----------------------+----------+
16 rows in set, 1 warning (0.00 sec)

In the above command returns the result, status is a profile in the state, duration is a time-consuming status under the state, so we are concerned about is the state of which the most time-consuming, which can optimize these states, of course, you can also view more information such as: CPU and so on. The syntax is as follows:

mysql> show profile block io for query 7\G
mysql> show profile all for query 7\G

In addition to the above block io and all, may be replaced by cpu (cpu time display the user, the system cpu time), IPC (display overhead information transmission and reception), page faults (errors associated overhead information display page), swaps (display exchange of information related to the number of overhead).

Note: After the test is complete, remember to turn off debugging features, so as not to affect the normal use of the database.

Fourth, to optimize the structure of the database table

Probably we can proceed to optimize the structure of the database table from the following aspects:

  • Many of the fields into a plurality of tables table, the table to avoid excessive field;
  • Add intermediate table, a reasonable increase in redundant field;
  • Optimization speed recording is inserted;
    • Disabled before inserting data index, index creation will not take effect, the command: ALTER TABLE table_name DISABLE KEYS;
    • Determined according to the actual situation, the only check is disabled before the insertion recording, the command: set unique_checks = 0;
    • Inserting a plurality of command data is preferably integrated as one;
    • Use bulk load data infle insert data.
  • For tables innodb engine, the following points can be optimized:
    • Disabling uniqueness check: set unique_checks = 0;
    • Disable foreign key checks: set foreign_key_checks = 0;
    • Disable automatic submission: set autocommit = 0;

Analysis tables, checklists and optimization table

The so-called analysis table is to analyze the distribution of keywords checklist is to check for errors, optimize the table is deleted or updated space caused by waste.

1, the analysis table

Analysis time table can analyze one or more tables, only time during the analysis, inserts and updates can not be performed. Parsing table is as follows:

mysql> analyze table bank;
+-------------+---------+----------+----------+
| Table       | Op      | Msg_type | Msg_text |
+-------------+---------+----------+----------+
| test01.bank | analyze | status   | OK       |
+-------------+---------+----------+----------+
1 row in set (0.00 sec)

For the result of the return of explanation: Table is the table name, what operations are performed op, msg_type level information (status is the normal state, info is information, note note, warning warning, error error), msg_text display information.

2, checklist

Check for errors, whether keyword statistics, check for errors Check table view table names option = {quick | fast | medium | extended | changed} Quick does not scan line, do not check for errors only connection Fast checking not been closed properly tables Medium scan line verification deleted connection is valid, each row may be calculated checksum keywords. Extended all the keywords for each line a comprehensive keyword search, Changed only after the last inspection checklist changed and has not been closed properly tables, Option is only valid for innodb table is not valid for myisam, in the implementation of the table will add read-only lock.

mysql> check table bank;
+-------------+-------+----------+----------+
| Table       | Op    | Msg_type | Msg_text |
+-------------+-------+----------+----------+
| test01.bank | check | status   | OK       |
+-------------+-------+----------+----------+
1 row in set (0.00 sec)

3, optimization table

Eliminate wasted space resulting from deleting or updating, the command syntax is:. Optimize [local | no_write_to_binlog] table tb1_name ..., optimization innodb tables and tables myisam are valid, but only to optimize the table varchar \ text \ blob digital type , on the read-only locks during execution.

mysql> optimize table bank\G
*************************** 1. row ***************************
   Table: test01.bank
      Op: optimize
Msg_type: note
Msg_text: Table does not support optimize, doing recreate + analyze instead
*************************** 2. row ***************************
   Table: test01.bank
      Op: optimize
Msg_type: status
Msg_text: OK
2 rows in set (0.04 sec)

-------- end of this article so far, thanks for reading --------

Guess you like

Origin blog.51cto.com/14154700/2464201