mysql 获取执行计划方法:
1.通过explain进行查看sql的执行计划;
2.通线程正在执行的sql查看该sql的执行计划;
explain进行查看sql的执行计划相对简单,其实通线程正在执行的sql查看执行计划也很简单,那么为什么还需要第二种方式呢?
因为日常工作会发现很多sql一直在执行,执行发生异常,如果把该sql拿出来执行又很快,于是我们想获取该sql执行异常时候的执行计划,同时因为数据在时刻的变化,统计信息也有可能变化,有可能导致执行计划发生改变,好在mysql5.7开始已经可以通过正在执行的线程查看该sql的执行计划,不需要把sql拿出来执行获取执行计划,这样就可以很好的还原真相:
show processlist;或者select * from information_schema.PROCESSLIST
where info is not null order by time desc ;查看。
mysql> select from information_schema.PROCESSLIST
where info is not null;
+-----+------+-----------------+------+---------+------+--------------+-----------------------------------------------------------------------+
| ID | USER | HOST | DB | COMMAND | TIME | STATE | INFO |
+-----+------+-----------------+------+---------+------+--------------+-----------------------------------------------------------------------+
| 17 | root | localhost | NULL | Query | 0 | executing | select from information_schema.PROCESSLIST
where info is not null |
| 178 | root | localhost:60124 | app1 | Query | 560 | Sending data | select count(*) from t ,t_adress where t.adress = t_adress.adress |
+-----+------+-----------------+------+---------+------+--------------+-----------------------------------------------------------------------+
2 rows in set (0.00 sec)
mysql> EXPLAIN FOR CONNECTION 178;
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+----------------------------------------------------+
| 1 | SIMPLE | t_adress | NULL | ALL | NULL | NULL | NULL | NULL | 200096 | 100.00 | NULL |
| 1 | SIMPLE | t | NULL | ALL | NULL | NULL | NULL | NULL | 309664 | 100.00 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+----------------------------------------------------+
2 rows in set (0.01 sec)
使用起来是不是很方便?
最后说一句:
今天心里很憋屈,睡觉了。。。。。。。。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。