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

Mysql高级2-SQL性能分析

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
一、SQL执行频率

  MySQL客户端 连接成功后,通过show [session | global] status 命令可以提供服务器状态信息,通过如下指令,可以查看当前数据库的insert,update,dalete,select的访问冰刺
  1. show [global | session] status like "Com_______";   # 七个_ 表示起个通配符
复制代码
  1. mysql> show global status like 'Com_______';
  2. +---------------+-------+
  3. | Variable_name | Value |
  4. +---------------+-------+
  5. | Com_binlog    | 0     |
  6. | Com_commit    | 0     |
  7. | Com_delete    | 0     |
  8. | Com_import    | 0     |
  9. | Com_insert    | 0     |
  10. | Com_repair    | 0     |
  11. | Com_revoke    | 0     |
  12. | Com_select    | 4     |
  13. | Com_signal    | 0     |
  14. | Com_update    | 0     |
  15. | Com_xa_end    | 0     |
  16. +---------------+-------+
  17. 11 rows in set (0.00 sec)
复制代码
  说明1:上面的数据库被执行查询4次
 
二、慢查询日志

  慢查询日志记录了所有执行时间超过指定参数(long_query_time 单位:秒,默认10秒)的所有SQL语句的日志,Mysql的慢查询日志默认没有开启,需要在Mysql的配置文件中(通常在/etc/my.cnf)中配置如下信息:
  可以使用一下语句查询慢查询是否开启
  1. mysql> show variables like 'slow_query_log';
  2. +----------------+-------+
  3. | Variable_name  | Value |
  4. +----------------+-------+
  5. | slow_query_log | OFF   |
  6. +----------------+-------+
  7. 1 row in set (0.01 sec)
复制代码
  说明:慢查询默认是关闭的
  1. # 开启慢查询
  2. slow_query_log=1
  3. # 设置慢查询的时间
  4. long_query_time=2
复制代码
  再次查询
  1. mysql> show variables like 'slow_query_log';
  2. +----------------+-------+
  3. | Variable_name  | Value |
  4. +----------------+-------+
  5. | slow_query_log | ON    |
  6. +----------------+-------+
  7. 1 row in set (0.00 sec)
复制代码
  慢日志文件通常指mysql的安装目录里面的data文件夹中。
 
三、profile

  3.1 show profiles

    可以查看每一条SQL的耗时基本情况
  1. mysql> show profiles;
  2. +----------+-------------+-----------------------------------------------------------------------+
  3. | Query_ID | Duration    | Query                                                                 |
  4. +----------+-------------+-----------------------------------------------------------------------+
  5. |       11 |  0.00020000 | SELECT DATABASE()                                                     |
  6. |       12 |  0.00029000 | SELECT DATABASE()                                                     |
  7. |       13 |  0.00040900 | SELECT DATABASE()                                                     |
  8. |       14 |  0.00145600 | show databases                                                        |
  9. |       15 |  0.00279800 | show tables                                                           |
  10. |       16 | 12.28066100 | select * from account_transaction                                     |
  11. |       17 |  0.00166700 | select * from account_transaction where id = 1                        |
  12. |       18 |  6.01525200 | select * from account_transaction where trade_no="164126925202017539" |
  13. |       19 |  6.64749300 | select * from account_transaction where trade_no="164126925202017539" |
  14. |       20 |  5.39658800 | select * from account_transaction where trade_no="164126923751014167" |
  15. |       21 |  0.00067300 | select * from account_transaction where id=100                        |
  16. |       22 |  0.00046900 | select * from account_transaction where id=1000                       |
  17. |       23 |  0.00045200 | select * from account_transaction where id=10000                      |
  18. |       24 |  0.00052900 | select * from account_transaction where id=100000                     |
  19. |       25 |  0.00038300 | select * from account_transaction where id=20000                      |
  20. +----------+-------------+-----------------------------------------------------------------------+
  21. 15 rows in set, 1 warning (0.00 sec)
复制代码
    说明1:第16条查询全部数据花费了12.28秒,第17条根据id查询只花费了0.001秒,第18条通过普通字段查询花费了6.00秒
    说明2:SQL中能不做全量查询就不要做全量查询。
    说明3:SQL中能通过id查询就不要通过其他字段查询,因为毕竟其他字段的查询还是会根据二级索引查到id,再根据id查询到具体的数据的。
  3.2 have_profiling

    参数have_profiling能够看到当前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)
复制代码
  说明1:这里的YES只是说明该版本的mysql是支持profile操作的,但是不代表profile操作是开始的,仅代表有这个功能而已!
  默认profiling是关闭的,可以通过set语句在session/global级别开启profiling;
  1. mysql> select @@profiling;
  2. +-------------+
  3. | @@profiling |
  4. +-------------+
  5. |           0 |
  6. +-------------+
  7. 1 row in set, 1 warning (0.01 sec)
复制代码
  3.3 开启profiling
  1. mysql> set profiling=1;
  2. Query OK, 0 rows affected, 1 warning (0.00 sec)
  3. mysql> select @@profiling;
  4. +-------------+
  5. | @@profiling |
  6. +-------------+
  7. |           1 |
  8. +-------------+
  9. 1 row in set, 1 warning (0.00 sec)
复制代码
  3.4 查看指定SQL耗时

    通过带query_id的SQL语句各个阶段的耗时情况
  1. show profile for query query_id;
复制代码
  1. mysql> show profile for query 20;
  2. +--------------------------------+----------+
  3. | Status                         | Duration |
  4. +--------------------------------+----------+
  5. | starting                       | 0.000083 |
  6. | Executing hook on transaction  | 0.000007 |
  7. | starting                       | 0.000007 |
  8. | checking permissions           | 0.000006 |
  9. | Opening tables                 | 0.000107 |
  10. | init                           | 0.000012 |
  11. | System lock                    | 0.000010 |
  12. | optimizing                     | 0.000012 |
  13. | statistics                     | 0.000025 |
  14. | preparing                      | 0.000041 |
  15. | executing                      | 5.393642 |
  16. | end                            | 0.000016 |
  17. | query end                      | 0.000005 |
  18. | waiting for handler commit     | 0.000009 |
  19. | closing tables                 | 0.000009 |
  20. | freeing items                  | 0.002130 |
  21. | logging slow query             | 0.000426 |
  22. | cleaning up                    | 0.000041 |
  23. +--------------------------------+----------+
  24. 18 rows in set, 1 warning (0.01 sec)
复制代码
  3.5 查看指定SQL的CPU使用情况
  1. show profile cpu for query query_id
复制代码
  1. mysql> show profile cpu for query 20;
  2. +--------------------------------+----------+----------+------------+
  3. | Status                         | Duration | CPU_user | CPU_system |
  4. +--------------------------------+----------+----------+------------+
  5. | starting                       | 0.000083 | 0.000072 |   0.000009 |
  6. | Executing hook on transaction  | 0.000007 | 0.000003 |   0.000004 |
  7. | starting                       | 0.000007 | 0.000006 |   0.000002 |
  8. | checking permissions           | 0.000006 | 0.000004 |   0.000002 |
  9. | Opening tables                 | 0.000107 | 0.000058 |   0.000017 |
  10. | init                           | 0.000012 | 0.000005 |   0.000006 |
  11. | System lock                    | 0.000010 | 0.000008 |   0.000002 |
  12. | optimizing                     | 0.000012 | 0.000010 |   0.000002 |
  13. | statistics                     | 0.000025 | 0.000023 |   0.000001 |
  14. | preparing                      | 0.000041 | 0.000027 |   0.000014 |
  15. | executing                      | 5.393642 | 2.294837 |   0.151005 |
  16. | end                            | 0.000016 | 0.000007 |   0.000009 |
  17. | query end                      | 0.000005 | 0.000003 |   0.000001 |
  18. | waiting for handler commit     | 0.000009 | 0.000009 |   0.000001 |
  19. | closing tables                 | 0.000009 | 0.000008 |   0.000002 |
  20. | freeing items                  | 0.002130 | 0.000037 |   0.000063 |
  21. | logging slow query             | 0.000426 | 0.000034 |   0.000175 |
  22. | cleaning up                    | 0.000041 | 0.000021 |   0.000018 |
  23. +--------------------------------+----------+----------+------------+
  24. 18 rows in set, 1 warning (0.00 sec)
复制代码
 
四、explain执行计划

  explain 或者 desc 命令获取Mysql如何执行select 语句的信息,包括在select 语句在执行过程中表如何连接,及连接的顺序
  4.1 语法
  1. explain/desc select 字段列表 from 表名 where 条件;
复制代码
  4.2 示例
  1. mysql> select * from account_transaction where id=100;
  2. +-----+--------------------+--------+--------+----------------------------+---------------+--------------+--------+---------+-----------------+-------------------+-----------+--------+
  3. | id  | trade_no           | type   | method | time                       | payment       | out_trade_no | amount | balance | trader_staff_id | operator_staff_id | device_id | remark |
  4. +-----+--------------------+--------+--------+----------------------------+---------------+--------------+--------+---------+-----------------+-------------------+-----------+--------+
  5. | 100 | 156384784634000449 | TOP_UP | CASH   | 2019-07-23 02:10:46.929559 | LOCAL_ACCOUNT |              |  10000 |   10000 |             449 |                11 | 7         |        |
  6. +-----+--------------------+--------+--------+----------------------------+---------------+--------------+--------+---------+-----------------+-------------------+-----------+--------+
  7. 1 row in set (0.00 sec)
  8. mysql> explain select * from account_transaction where id=100;
  9. +----+-------------+---------------------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
  10. | id | select_type | table               | partitions | type  | possible_keys | key     | key_len | ref   | rows | filtered | Extra |
  11. +----+-------------+---------------------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
  12. |  1 | SIMPLE      | account_transaction | NULL       | const | PRIMARY       | PRIMARY | 4       | const |    1 |   100.00 | NULL  |
  13. +----+-------------+---------------------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
  14. 1 row in set, 1 warning (0.00 sec)
复制代码
  4.3 explain字段含义

    参数id:select查询的序列号,表示查询语句中的执行顺序,如果id相同,执行顺序从上到下,id不同,值越大,越先执行
  1. mysql> select s.*, c.* from student s, course c,student_course sc where s.id=sc.student_id and c.id = sc.course_id;
  2. +----+--------+----+--------+
  3. | id | name   | id | name   |
  4. +----+--------+----+--------+
  5. |  1 | 张三   |  1 | java   |
  6. |  1 | 张三   |  2 | python |
  7. |  1 | 张三   |  3 | php    |
  8. |  2 | 李四   |  2 | python |
  9. |  2 | 李四   |  3 | php    |
  10. |  3 | 王五   |  4 | C      |
  11. +----+--------+----+--------+
  12. 6 rows in set (0.03 sec)
  13. mysql> explain select s.*, c.* from student s, course c,student_course sc where s.id=sc.student_id and c.id = sc.course_id;
  14. +----+-------------+-------+------------+--------+----------------------------+---------+---------+-------------------------+------+----------+--------------------------------------------+
  15. | id | select_type | table | partitions | type   | possible_keys              | key     | key_len | ref                     | rows | filtered | Extra                                      |
  16. +----+-------------+-------+------------+--------+----------------------------+---------+---------+-------------------------+------+----------+--------------------------------------------+
  17. |  1 | SIMPLE      | s     | NULL       | ALL    | PRIMARY                    | NULL    | NULL    | NULL                    |    4 |   100.00 | NULL                                       |
  18. |  1 | SIMPLE      | sc    | NULL       | ALL    | fk_course_id,fk_student_id | NULL    | NULL    | NULL                    |    6 |    33.33 | Using where; Using join buffer (hash join) |
  19. |  1 | SIMPLE      | c     | NULL       | eq_ref | PRIMARY                    | PRIMARY | 4       | mysql_test.sc.course_id |    1 |   100.00 | NULL                                       |
  20. +----+-------------+-------+------------+--------+----------------------------+---------+---------+-------------------------+------+----------+--------------------------------------------+
  21. 3 rows in set, 1 warning (0.00 sec)
复制代码
    说明1:这一个select语句中,涉及到了三个表,所以有三条执行记录。
    说明2:虽然搜索的顺序是student,course,student_course,但是执行顺序是student,student_course,course,因为两个表是没有关系的,需要依靠第三张关系表维系
    说明3:这是一个三个都是相同id的案例
  1. mysql>   select * from student where id in(select student_id from student_course where course_id = (select id from course where name = "python"));
  2. +----+--------+
  3. | id | name   |
  4. +----+--------+
  5. |  1 | 张三   |
  6. |  2 | 李四   |
  7. +----+--------+
  8. 2 rows in set (0.00 sec)
  9. mysql> explain select * from student where id in(select student_id from student_course where course_id = (select id from course where name = "python"));
  10. +----+--------------+----------------+------------+--------+----------------------------+--------------+---------+------------------------+------+----------+-------------+
  11. | id | select_type  | table          | partitions | type   | possible_keys              | key          | key_len | ref                    | rows | filtered | Extra       |
  12. +----+--------------+----------------+------------+--------+----------------------------+--------------+---------+------------------------+------+----------+-------------+
  13. |  1 | PRIMARY      | <subquery2>    | NULL       | ALL    | NULL                       | NULL         | NULL    | NULL                   | NULL |   100.00 | NULL        |
  14. |  1 | PRIMARY      | student        | NULL       | eq_ref | PRIMARY                    | PRIMARY      | 4       | <subquery2>.student_id |    1 |   100.00 | NULL        |
  15. |  2 | MATERIALIZED | student_course | NULL       | ref    | fk_course_id,fk_student_id | fk_course_id | 4       | const                  |    2 |   100.00 | Using where |
  16. |  3 | SUBQUERY     | course         | NULL       | ALL    | NULL                       | NULL         | NULL    | NULL                   |    4 |    25.00 | Using where |
  17. +----+--------------+----------------+------------+--------+----------------------------+--------------+---------+------------------------+------+----------+-------------+
  18. 4 rows in set, 1 warning (0.00 sec)
复制代码
    说明1:id值越大,越先被执行,所以这个查询,先执行course表的查询,在执行student_course表,最后执行student表
    参数select_type:表示select的类型,常见的取值有,SIMPLE、PRIMARY、UNION、SUBQUERY
    参数type:表示连接的类型,性能由好到差的链接类型为NULL、system、const、eq_ref、ref、range、index、all;



    • 当查询语句中不使用任何表,则查询类型为最优的,但是却在实际工作中,很难做到,不查询表,不然查询的意义是什么呢。
    • 当查询系统表的时候,type会为system,所以一般系统表查询比较快
    • 当查询id主键或者唯一索引的时候,会出现const类型
    • 当查询使用非唯一索引的时候,会出现ref
    • 当全表查询的时候会出现all

    参数possible_key:可能的索引,一个或者多个
    参数key:是实际用到的索引,如果为NULL,则表示没有使用索引
    参数key_len:表示索引中使用的字节数,该值为索引字段最大可能长度,并非实际使用长度,在不损失精确性的前提下,长度越短越好。
    参数rows:MySQL认为必须要执行的查询的行数,在InnoDB引擎中,是一个估计值,可能并不总是准确的
    参数filtered:表示返回结果的行数占需要读取行数的百分比,filtered的值越大越好
 

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

举报 回复 使用道具