MySQL performance - SQL execution analysis (transfer)

Back-end development will inevitably touch the database, and the quality of the data layer will affect the response time of the entire service. Therefore, the optimization skills of the database must be mastered. The following is what I have organized in the learning process, the memo.

The following is the content of the previous article, divided into the following parts:

  • 1. SQL execution time analysis
  • Second, SQL execution analysis

1. SQL execution time analysis

By finding the SQL statements with long execution time, the efficiency problem of the data layer can be found intuitively.

1. View the execution of the system by show processlist

mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
|  2 | root | localhost | NULL | Query   |    0 | init  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.01 sec)

2. View through profiling

This command is to check the execution time of SQL, and it can be very intuitive to see the speed.

2.1 Check if profiling is enabled

0 means the analysis function is still turned off

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 |
+-------------+

2.2 Open the tool

mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           1 |
+-------------+

2.3 View the execution time of SQL

mysql> show profiles;
+----------+------------+----------------------------+
| Query_ID | Duration   | Query                      |
+----------+------------+----------------------------+
|        1 | 0.00173700 | select * from ip           |
|        2 | 0.00057500 | select porxy, port from ip |
+----------+------------+----------------------------+

2.4 View the detailed information of SQL execution time

grammar:show profile for query Query_ID

mysql> show profile for query 1;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000073 |
| checking permissions | 0.000031 |   ---检查是否在缓存中  
| Opening tables       | 0.000207 |   ---打开表
| init                 | 0.000067 |   ---初始化
| System lock          | 0.000040 |   ---锁系统
| optimizing           | 0.000005 |   ---优化查询
| statistics           | 0.000021 |
| preparing            | 0.000015 |   ---准备
| executing            | 0.000003 |   ---执行
| Sending data         | 0.000993 |
| end                  | 0.000006 |
| query end            | 0.000007 |
| closing tables       | 0.000011 |
| freeing items        | 0.000169 |
| cleaning up          | 0.000089 |
+----------------------+----------+

The above specific information is obtained from the INFORMATION_SCHEMA.PROFILING table. This table records the execution time and related information of all the various steps. grammar:
select * from INFORMATION_SCHEMA.PROFILING where query_id = Query_ID;

3. Slow query log

MySQL's slow query log, as its name implies, records SQL whose execution time exceeds the set value (10s by default) into the log. This function needs to be turned on manually, but it will cause a certain performance loss after turning it on.

3.1 Check whether the slow log is enabled

By default, the value of slow_query_log is OFF, which means that the slow query log is disabled, which can be enabled by setting the value of slow_query_log. grammar:set global slow_query_log=1

mysql> show variables  like '%slow_query_log%';
+---------------------+------------------------------------------------------+
| Variable_name       | Value                                                |
+---------------------+------------------------------------------------------+
| slow_query_log      | OFF                                                  |
| slow_query_log_file | /usr/local/var/mysql/xueweihandeMacBook-Air-slow.log |
+---------------------+------------------------------------------------------+
2 rows in set (0.11 sec)

mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.03 sec)

mysql> show variables  like '%slow_query_log%';
+---------------------+------------------------------------------------------+
| Variable_name       | Value                                                |
+---------------------+------------------------------------------------------+
| slow_query_log      | ON                                                   |
| slow_query_log_file | /usr/local/var/mysql/xueweihandeMacBook-Air-slow.log |
+---------------------+------------------------------------------------------+

3.2 Set the timeout time

  • Set syntax:set global long_query_time=4
  • Check out the syntax:show variables like 'long_query_time'

Note: After modification, you need to reconnect or open a new session to see the modified value.

permanent, modified my.cnf

slow_query_log=1
long_query_time=10
slow_query_log_file=/path/mysql_slow.log

3.3 Other parameters

3.3.1 log_output

参数是指定日志的存储方式。log_output='FILE'表示将日志存入文件,默认值是'FILE'。log_output='TABLE'表示将日志存入数据库,这样日志信息就会被写入到mysql.slow_log表中。MySQL数据库支持同时两种日志存储方式,配置的时候以逗号隔开即可,如:log_output='FILE,TABLE'。日志记录到系统的专用日志表中,要比记录到文件耗费更多的系统资源,因此对于需要启用慢查询日志,又需要能够获得更高的系统性能,那么建议优先记录到文件。

3.3.2 log-queries-not-using-indexes

未使用索引的查询也被记录到慢查询日志中(可选项)。如果调优的话,建议开启这个选项。另外,开启了这个参数,其实使用full index scan的sql也会被记录到慢查询日志。

3.3.3 log_slow_admin_statements

表示是否将慢管理语句例如ANALYZE TABLE和ALTER TABLE等记入慢查询日志

3.4 分析工具 mysqldumpslow

MySQL 提供了慢日志分析工具 mysqldumpslow。

  • -s 表示按照何种方式排序;
    • c: 访问计数
    • l: 锁定时间
    • r: 返回记录
    • t: 查询时间
    • al:平均锁定时间
    • ar:平均返回记录数
    • at:平均查询时间
  • -t 是top n的意思,即为返回前面多少条的数据;
  • -g 后边可以写一个正则匹配模式,大小写不敏感的;

3.4.1 命令示例

  • 得到返回记录集最多的 10 个 SQL:mysqldumpslow -s r -t 10 /database/mysql/mysql06_slow.log

  • 得到访问次数最多的 10 个 SQL:mysqldumpslow -s c -t 10 /database/mysql/mysql06_slow.log

  • 得到按照时间排序的前10条里面含有左连接的查询语句:mysqldumpslow -s t -t 10 -g “left join” /database/mysql/mysql06_slow.log

  • 另外建议在使用这些命令时结合 | 和 more 使用 ,否则有可能出现刷屏的情况:mysqldumpslow -s r -t 20 /mysqldata/mysql/mysql06-slow.log | more

二、SQL 执行情况分析

使用 explain 分析 SQL 执行情况。

explain select * from ip;

+----+-------------+-------+------+---------------+------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | ip    | ALL  | NULL          | NULL | NULL    | NULL |  400 | NULL  |
+----+-------------+-------+------+---------------+------+---------+------+------+-------+
select_type table type possible_keys key key_len rows Extra
Indicates the type of query The table that outputs the result set Indicates the join type of the table Indicates the index that may be used when querying Indicates the actual index used the length of the index field Number of lines scanned (estimated number of lines) Description and description of implementation

 

 

The following is the next content, divided into the following parts:

  • 1. Performance optimization when creating tables
  • 2. Performance optimization when designing tables
  • 3. Optimizing SQL Statements
  • 4. Other

1. Performance optimization when creating tables

1. Always set an ID for each table

Each table should set an ID field as the primary key, the primary key should be  INT or  UNSIGNED type, and set the auto-increment  AUTO_INCREMENT flag. Because of the use  VARCHAR of typed primary keys, performance will be degraded.

Here, there is only one exception, that is the "foreign key" of the "associated table", that is, the primary key of this table is formed by the primary keys of several individual tables. We call this situation a "foreign key". For example, if there is a "student table" with student IDs, and a "course table" with course IDs, then the "grade table" is an "association table", which is associated with the student table and the curriculum table. In the grade table, Student ID and Course ID are called "foreign keys" and together they form the primary key .

2. Index the search field

See my previous article for details: MySQL Indexes

3. Use ENUM instead of VARCHAR

The ENUM type is very fast and compact. In reality, it holds a TINYINT, but appears to be a string. In this way, using this field to do some list of options becomes quite perfect.

If you have a field, such as "country", that you know is limited and fixed, then you should use ENUM instead of VARCHAR.

ENUM is a unique field type of MySQL database, which will affect migration to other databases after use. Therefore, if you change the database situation in the future, you must use it with caution .

4. Use NOT NULL whenever possible

应该总是让你的字段保持 NOT NULL,因为这样节省空间(NULL 也是需要空间的)。

5. 把IP地址存成 UNSIGNED INT

如果使用整形来存放 IP 而不是 VARCHAR(15) 字段,节省了很多的空间(需要写一个 IP 转换的函数)。

二、设计表时的性能优化

1. 选择正确的存储引擎

MyISAM 适合于一些需要大量查询的应用,但其对于有大量写操作并不是很好。甚至你只是需要 update 一个字段,整个表都会被锁起来,而别的进程,就算是读进程都无法操作直到读操作完成。另外,MyISAM 对于 SELECT COUNT(*) 这类的计算是超快无比的。

InnoDB 的趋势会是一个非常复杂的存储引擎,对于一些小的应用,它会比 MyISAM 还慢。他是它支持“行锁” ,于是在写操作比较多的时候,会更优秀。并且,他还支持更多的高级应用,比如:事务。

2. 固定长度的表会更快

表中没有如下类型的字段: VARCHAR,TEXT,BLOB。只要你包括了其中一个这些字段,那么这个表就不是“固定长度静态表”了,这样,MySQL 引擎会用另一种方法来处理。

固定长度的表会提高性能,因为 MySQL 搜寻得会更快一些,因为这些固定的长度是很容易计算下一个数据的偏移量的,所以读取的自然也会很快。而如果字段不是定长的,那么,每一次要找下一条的话,需要程序找到主键。

并且,固定长度的表也更容易被缓存和重建。不过,唯一的副作用是,固定长度的字段会浪费一些空间,因为定长的字段无论你用不用,他都是要分配那么多的空间。

使用“垂直分割”技术(见下一条),你可以分割你的表成为两个一个是定长的,一个则是不定长的。

3. 垂直分割

“垂直分割”是一种把数据库中的表按列变成几张表的方法,这样可以降低表的复杂度和字段的数目,从而达到优化的目的。(以前,在银行做过项目,见过一张表有100多个字段,很恐怖)

示例一:在Users表中有一个字段是家庭地址,这个字段是可选字段,相比起,而且你在数据库操作的时候除了个人信息外,你并不需要经常读取或是改写这个字段。那么,为什么不把他放到另外一张表中呢? 这样会让你的表有更好的性能,大家想想是不是,大量的时候,我对于用户表来说,只有用户ID,用户名,口令,用户角色等会被经常使用。小一点的表总是会有好的性能。

示例二: 你有一个叫 “last_login” 的字段,它会在每次用户登录时被更新。但是,每次更新时会导致该表的查询缓存被清空。所以,你可以把这个字段放到另一个表中,这样就不会影响你对用户ID,用户名,用户角色的不停地读取了,因为查询缓存会帮你增加很多性能。

另外,你需要注意的是,这些被分出去的字段所形成的表,你不会经常性地去Join他们,不然的话,这样的性能会比不分割时还要差,而且,会是极数级的下降。

三、优化 SQL 语句

1. 使用查询缓存

1.1 查看是否开启缓存:

mysql> select @@query_cache_type;
+--------------------+
| @@query_cache_type |
+--------------------+
| ON                 |
+--------------------+

开启缓存,修改 my.cnf,在末尾加入,重启MySQL生效

query_cache_type = 1
query_cache_size = 600000

1.2 为查询缓存优化你的查询

// 查询缓存不开启
r = mysql_query("SELECT username FROM user WHERE signup_date >= CURDATE()");

// 开启查询缓存
today = date("Y-m-d");
r = mysql_query("SELECT username FROM user WHERE signup_date >= '%s'" % today);

上面两条 SQL 语句的差别就是 CURDATE(),MySQL 的查询缓存对这个函数不起作用。所以,像 NOW() 和 RAND() 或是其它的诸如此类的 SQL 函数都不会开启查询缓存,因为这些函数的返回是会不定的易变的。所以,你所需要的就是用一个变量来代替 MySQL 的函数,从而开启缓存

2. 当只要一行数据时使用 LIMIT 1

在这种情况下,加上 LIMIT 1 可以增加性能。这样一样,MySQL 数据库引擎会在找到一条数据后停止搜索,而不是继续往后查少下一条符合记录的数据。

3. 在 JOIN 表的时候使用相当类型的例,并将其索引

如果有很多 JOIN 的操作,JOIN 的字段应该加索引,同时保证这些字段的类型一致

4. 避免 SELECT *

从数据库里读出越多的数据,那么查询就会变得越慢。所以,应该养成需要什么就取什么的好的习惯

5. 拆分大的 DELETE 或 INSERT 语句

如果你需要在一个在线的网站上去执行一个大量的 DELETE 或 INSERT 查询,你需要非常小心,要避免你的操作让你的整个网站停止相应。因为这两个操作是会锁表的,表一锁住了,别的操作都进不来了。

执行这种大量的 DELETE 和 INSERT,可以分成几部分执行,没执行一部分就暂停一下再执行。

四、其它

1. EXPLAIN 你的 SELECT 查询

使用 EXPLAIN 关键字可以让你知道 MySQL 是如何处理你的 SQL 语句的。这可以帮你分析你的查询语句或是表结构的性能瓶颈。

查看 rows 列可以让我们找到潜在的性能问题。

2. 从 PROCEDURE ANALYSE() 取得建议

PROCEDURE ANALYSE() 会让 MySQL 帮你去分析你的字段和其实际的数据,并会给你一些有用的建议(只是建议)。只有表中有实际的数据,这些建议才会变得有用,因为要做一些大的决定是需要有数据作为基础的。

mysql> select * from charac procedure analyse()
*************************** 1. row ***************************
Field_name: world.charac.charac
Min_value: A
Max_value: E
Min_length: 1
Max_length: 1
Empties_or_zeros: 0
Nulls: 0
Avg_value_or_avg_length: 1.0000
Std: NULL
Optimal_fieldtype: ENUM('A','B','C','D','E') NOT NULL
1 row in set (0.00 sec)

3. 使用连接池和 ORM

参考

from:http://www.cnblogs.com/xueweihan/ 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326214504&siteId=291194637