Mysql性能优化-11.慢查询日志

1.概要

用于记录 执行时间超过某个临界值的SQL的日志,用于快速定位慢查询。
我们优化的参考。
slow query log
开启日志,设置临界时间,读取日志内容,优化对应SQL

2.开启日志,利用配置完成

mysql> show variables like 'slow_query_log';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| slow_query_log | OFF   |
+----------------+-------+
1 row in set, 1 warning (0.00 sec)

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

mysql> show variables like 'slow_query_log';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| slow_query_log | ON    |
+----------------+-------+
1 row in set, 1 warning (0.00 sec)

mysql> show variables like 'long_query_time';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+
1 row in set, 1 warning (0.00 sec)

mysql> set long_query_time=0.5;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'long_query_time';
+-----------------+----------+
| Variable_name   | Value    |
+-----------------+----------+
| long_query_time | 0.500000 |
+-----------------+----------+
1 row in set, 1 warning (0.00 sec)

3.查看日志

# Time: 2019-06-02T13:29:09.150989Z
# User@Host: root[root] @ localhost [::1]  Id:    29
# Query_time: 1.910755  Lock_time: 0.000073 Rows_sent: 5  Rows_examined: 1400005
use mysql_test;
SET timestamp=1559482149;
select * from t_student order by rand() limit 5;

4.profile信息

详细记录SQL的执行时间的工具。
开启profile,执行SQL自动记录,读取SQL形成的profile信息。

4.1 开启profile

mysql> show variables like 'profiling';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling     | OFF   |
+---------------+-------+
1 row in set, 1 warning (0.01 sec)

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

mysql> show variables like 'profiling';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| profiling     | ON    |
+---------------+-------+
1 row in set, 1 warning (0.00 sec)

4.2 查询profile

mysql> show profiles\G
*************************** 1. row ***************************
Query_ID: 1
Duration: 0.00217450
   Query: show variables like 'profiling'
*************************** 2. row ***************************
Query_ID: 2
Duration: 0.00078675
   Query: SELECT DATABASE()
*************************** 3. row ***************************
Query_ID: 3
Duration: 2.55289900
   Query: select * from t_student order by rand() limit 5
3 rows in set, 1 warning (0.00 sec)

4.3 具体某条SQL的详细步骤时间

mysql> show profile for query 3;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000017 |
| Waiting for query cache lock   | 0.000003 |
| starting                       | 0.000003 |
| checking query cache for query | 0.000060 |
| checking permissions           | 0.000004 |
| Opening tables                 | 0.001803 |
| init                           | 0.000022 |
| System lock                    | 0.000007 |
| optimizing                     | 0.000003 |
| statistics                     | 0.000054 |
| preparing                      | 0.000011 |
| Creating tmp table             | 0.000045 |
| Sorting result                 | 0.000005 |
| executing                      | 0.000002 |
| Sending data                   | 0.020438 |
| converting HEAP to ondisk      | 0.021765 |
| Sending data                   | 1.665973 |
| Creating sort index            | 0.840376 |
| end                            | 0.000014 |
| query end                      | 0.000019 |
| removing tmp table             | 0.002172 |
| query end                      | 0.000009 |
| closing tables                 | 0.000009 |
| freeing items                  | 0.000049 |
| cleaning up                    | 0.000038 |
+--------------------------------+----------+
25 rows in set, 1 warning (0.00 sec)

5.典型的服务器配置

明显是数据库管理员的工作。

5.1 max_connections最大连接数

mysql> show variables like '%max_connections%';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| max_connections | 151   |
+-----------------+-------+
1 row in set, 1 warning (0.00 sec)

5.2 table_open_chahe 表句柄缓存

mysql> show variables like '%table_open_cache%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| table_open_cache           | 2000  |
| table_open_cache_instances | 16    |
+----------------------------+-------+
2 rows in set, 1 warning (0.00 sec)

5.3 key_buffer_size 索引缓冲大小

mysql> show variables like 'key_buffer_size';
+-----------------+---------+
| Variable_name   | Value   |
+-----------------+---------+
| key_buffer_size | 8388608 |
+-----------------+---------+
1 row in set, 1 warning (0.00 sec)

5.4 innodb_buffer_pool_size innodb存储引擎缓存池大小(很重要)

mysql> show variables like 'innodb_buffer_pool_size';
+-------------------------+---------+
| Variable_name           | Value   |
+-------------------------+---------+
| innodb_buffer_pool_size | 8388608 |
+-------------------------+---------+
1 row in set, 1 warning (0.00 sec)

5.5 innodb_file_per_table

innodb 独立表文件,ibd文件,就是innodb表的表文件。

mysql> show variables like 'innodb_file_per_table';
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_file_per_table | ON    |
+-----------------------+-------+
1 row in set, 1 warning (0.00 sec)

5.6 以上的配置全都取决于运行环境

基于测试负载情况,选择以上配置的大小。

猜你喜欢

转载自blog.csdn.net/weixin_34363171/article/details/90888502