翼度科技»论坛 编程开发 mysql 查看内容

MySQL查询优化

4

主题

4

帖子

12

积分

新手上路

Rank: 1

积分
12
1、获取服务器使用信息

SHOW STATUS
语法规则
  1. show [session | global] status like 'Connections'
复制代码
这里的 [session | global]可以省略不写,默认为session,这个参数表示获取性能参数的级别,session表示当前会话级别,global表示获取全局级别。
参数值参数说明Connections连接MySQL服务器的次数UptimeMySQL服务器启动后连续工作的时间Slow_queries慢查询的次数Com_insert插入数据的次数(只统计insert语句的次数,一次插入多条算一次)Com_delete删除数据的次数Com_update修改数据的次数Com_select查询数据的次数Innodb_rows_read查询数据时返回的数据行数Innodb_rows_inserted插入数据时返回的记录数Innodb_rows_updated更新数据时返回的记录数Innodb_rows_delete删除数据时返回的记录数例如查看MySLQ服务器启动后连续工作的时间
  1. mysql> show session status like 'connections';
  2. +---------------+-------+
  3. | Variable_name | Value |
  4. +---------------+-------+
  5. | Connections   | 10    |
  6. +---------------+-------+
  7. 1 row in set (0.00 sec)
  8. mysql> show global status like 'connections';
  9. +---------------+-------+
  10. | Variable_name | Value |
  11. +---------------+-------+
  12. | Connections   | 10    |
  13. +---------------+-------+
  14. 1 row in set (0.01 sec)
  15. mysql> show status like 'connections';
  16. +---------------+-------+
  17. | Variable_name | Value |
  18. +---------------+-------+
  19. | Connections   | 10    |
  20. +---------------+-------+
  21. 1 row in set (0.00 sec)
复制代码
2、分析查询语句

EXPLAIN
语法格式
  1. explain select id,name,age from Student \G;
复制代码
只需要在查询语句之前添加explain语句即可,这时这个语句不会真的查询数据,而是根据explain模拟优化器执行SLQ语句,并输出相关信息。例如
  1. mysql> explain select name from Student \G;
  2. *************************** 1. row ***************************
  3.            id: 1
  4.   select_type: SIMPLE
  5.         table: Student
  6.    partitions: NULL
  7.          type: ALL
  8. possible_keys: NULL
  9.           key: NULL
  10.       key_len: NULL
  11.           ref: NULL
  12.          rows: 1000
  13.      filtered: 100.00
  14.         Extra: NULL
  15. 1 row in set, 1 warning (0.00 sec)
复制代码
(1)id:表示select语句的序列号,有多少个select语句就有多少个序列号。
(2)select_type:SQL语句的查询类型,简单查询语句/复杂查询语句。
类型说明SIMPLE简单查询PRIMARY主键查询或者包含子查询时最外层的查询语句UNION连接查询,表示连接查询的第二个语句或者更后面DEPENDENT UNION与UNION差不多,但是这里取决于最外层的查询语句UNION RESULT连接查询的结果信息SUBQUERY子查询中的第一个查询语句DEPENDENT SUBQUERY取决于外层的查询语句DERIVEDFROM子句中的子查询MATERIALIZED表示实例化子查询UNCACHEABLE SUBQUERY不缓存子查询的结果数据,重新计算外部查询的每一行数据UNCACHEABLE UNION不缓存连接查询的结果数据,每次执行连接查询时都会重新计算数据结果(3)table:当前查询的数据表
(4)partitions:如果这个是分区表、表示查询结果匹配的分区。负责返回null。
(5)type:当前SQL语句使用的关联类型或者访问类型。最优到差依次为
system>const>eq_ref>ref>fulltext>ref_or_null>index_merge>unique_subquery>index_subquery>range>index>All
(6)possible_keys:执行查询语句时可能使用到的索引。但在实际查询中未必会使用到。为null时表示没有可使用的索引。
(7)key:执行查询语句时实际会使用到的索引,没有使用则为null。
(8)key_len:实际使用到索引按照字节计算的长度值。
(9)ref:数据表中的哪个列或者常量用来与key列中的索引做比较来检索数据。
(10)rows:查询数据的行数。
(11)filtered:查询结果符合查询条件的百分比。
(12)ectra:执行查询语句时额外的详细信息。
3、深入分析

SHOW PROFILE语句解析
查看MySQL是否支持PROFILE
  1. mysql> select @@have_profiling;
  2. +------------------+
  3. | @@have_profiling |
  4. +------------------+
  5. | YES              |
  6. +------------------+
  7. 1 row in set, 1 warning (0.00 sec)
复制代码
默认profiling时关闭的
  1. mysql> select @@profiling;
  2. +-------------+
  3. | @@profiling |
  4. +-------------+
  5. |           0 |
  6. +-------------+
  7. 1 row in set, 1 warning (0.00 sec)
复制代码
通过set语句开启
  1. mysql> set session profiling=1;
  2. Query OK, 0 rows affected, 1 warning (0.01 sec)
复制代码
  1. mysql> select @@profiling;
  2. +-------------+
  3. | @@profiling |
  4. +-------------+
  5. |           1 |
  6. +-------------+
  7. 1 row in set, 1 warning (0.00 sec)
复制代码
3.1、分析InnoDB数据表

  • 查看表结构
  1. mysql> show create table Student \G;
  2. *************************** 1. row ***************************
  3.        Table: Student
  4. Create Table: CREATE TABLE `Student` (
  5.   `name` varchar(255) DEFAULT NULL,
  6.   `age` int DEFAULT NULL,
  7.   `gender` varchar(255) DEFAULT NULL,
  8.   `school` varchar(255) DEFAULT NULL,
  9.   `id` int NOT NULL AUTO_INCREMENT,
  10.   `phone` varchar(255) DEFAULT NULL,
  11.   PRIMARY KEY (`id`)
  12. ) ENGINE=InnoDB AUTO_INCREMENT=1001 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
  13. 1 row in set (0.00 sec)
复制代码

  • 查看数据条数
  1. mysql> select count(*) from Student;
  2. +----------+
  3. | count(*) |
  4. +----------+
  5. |     1000 |
  6. +----------+
  7. 1 row in set (0.00 sec)
复制代码

  • 查看SQL语句信息
  1. mysql> show profiles;
  2. +----------+------------+--------------------------------+
  3. | Query_ID | Duration   | Query                          |
  4. +----------+------------+--------------------------------+
  5. |        1 | 0.00342200 | select @@profiling             |
  6. |        2 | 0.00027400 | select countct(*) from Student |
  7. |        3 | 0.00028900 | select countct(*) from Student |
  8. |        4 | 0.00436800 | select count(*) from Student   |
  9. |        5 | 0.00045100 | show create tables Student     |
  10. |        6 | 0.00571900 | show create table Student      |
  11. |        7 | 0.00136000 | show create table Student      |
  12. |        8 | 0.00401200 | select count(*) from Student   |
  13. |        9 | 0.00037000 | show profiles
  14. ;               |
  15. +----------+------------+--------------------------------+
  16. 9 rows in set, 1 warning (0.00 sec)
复制代码

  • 查询执行SQL语句执行过程中的所在线程的具体信息
  1. mysql> show profile for query 8;
  2. +--------------------------------+----------+
  3. | Status                         | Duration |
  4. +--------------------------------+----------+
  5. | starting                       | 0.001623 |
  6. | Executing hook on transaction  | 0.000015 |
  7. | starting                       | 0.000029 |
  8. | checking permissions           | 0.000017 |
  9. | Opening tables                 | 0.000119 |
  10. | init                           | 0.000013 |
  11. | System lock                    | 0.000025 |
  12. | optimizing                     | 0.000014 |
  13. | statistics                     | 0.000060 |
  14. | preparing                      | 0.000051 |
  15. | executing                      | 0.001900 |
  16. | end                            | 0.000008 |
  17. | query end                      | 0.000007 |
  18. | waiting for handler commit     | 0.000030 |
  19. | closing tables                 | 0.000019 |
  20. | freeing items                  | 0.000062 |
  21. | cleaning up                    | 0.000020 |
  22. +--------------------------------+----------+
  23. 17 rows in set, 1 warning (0.00 sec)
复制代码
可以看到这条SQL语句的主要时间花在了executing和starting

  • 查看指定SQL语句消耗的BLOCK IO资源
  1. mysql> show profile block io for query 8;
  2. +--------------------------------+----------+--------------+---------------+
  3. | Status                         | Duration | Block_ops_in | Block_ops_out |
  4. +--------------------------------+----------+--------------+---------------+
  5. | starting                       | 0.001623 |            0 |             0 |
  6. | Executing hook on transaction  | 0.000015 |            0 |             0 |
  7. | starting                       | 0.000029 |            0 |             0 |
  8. | checking permissions           | 0.000017 |            0 |             0 |
  9. | Opening tables                 | 0.000119 |            0 |             0 |
  10. | init                           | 0.000013 |            0 |             0 |
  11. | System lock                    | 0.000025 |            0 |             0 |
  12. | optimizing                     | 0.000014 |            0 |             0 |
  13. | statistics                     | 0.000060 |            0 |             0 |
  14. | preparing                      | 0.000051 |            0 |             0 |
  15. | executing                      | 0.001900 |            0 |             0 |
  16. | end                            | 0.000008 |            0 |             0 |
  17. | query end                      | 0.000007 |            0 |             0 |
  18. | waiting for handler commit     | 0.000030 |            0 |             0 |
  19. | closing tables                 | 0.000019 |            0 |             0 |
  20. | freeing items                  | 0.000062 |            0 |             0 |
  21. | cleaning up                    | 0.000020 |            0 |             0 |
  22. +--------------------------------+----------+--------------+---------------+
  23. 17 rows in set, 1 warning (0.00 sec)
复制代码
这条SQL语句没有消耗资源。也就是没有IO操作。

来源:https://www.cnblogs.com/changwan/p/18198053
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具