如何查看mysql执行过的语句

如何查看mysql执行过的语句,第1张

开启记录MySQL执行过SQL语句的方法很简单:编辑/etc/my.cnf文件,在[mysqld]节下面添加:log=/var/lib/mysql/sql_row.log行(日志的路径自己根据需要定义)。

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Default to using old password format for compatibility with mysql 3.x

# clients (those using the mysqlclient10 compatibility package).

old_passwords=1

log=/var/lib/mysql/sql_row.log

# Disabling symbolic-links is recommended to prevent assorted security risks

# to do so, uncomment this line:

# symbolic-links=0

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

修改完毕后,记得重启 MySQL:

service mysql restart

# 或者

/etc/init.d/mysqld stop

/etc/init.d/mysqld start

现在你去 /var/lib/mysql/ 路径下的 sql_row.log 文件应该是能够看到 MySQL 什么时候执行了哪些程序了。

1先通过status命令查看Mysql运行状态mysql>status--------------mysqlVer14.14Distrib5.1.73,forredhat-linux-gnu(x86_64)usingreadline5.1Connectionid:113752Currentdatabase:information_schemaCurrentuser:push_user@localhostSSL:NotinuseCurrentpager:stdoutUsingoutfile:''Usingdelimiter:Serverversion:5.1.73SourcedistributionProtocolversion:10Connection:LocalhostviaUNIXsocketServercharacterset:latin1Dbcharacterset:utf8Clientcharacterset:latin1Conn.characterset:latin1UNIXsocket:/tmp/mysql.sockUptime:22days8hours31min23secThreads:38Questions:1037751897Slowqueries:2356Opens:79836Flushtables:1Opentables:64Queriespersecondavg:537.282--------------在上面显示列表的最后一条,我们来查看Slowqueries这一项的值,如果多次查看的值大于0的话,说明有些查询sql命令执行时间过长。2)这时再通过showprocesslist命令来查看当前正在运行的SQL,从中找出运行慢的SQL语句,找到执行慢的语句后,再用explain命令查看这些语句的执行计划。mysql>showprocesslist+--------+-----------+---------------------+--------------------+---------+-------+-------+------------------+|Id|User|Host|db|Command|Time|State|Info|+--------+-----------+---------------------+--------------------+---------+-------+-------+------------------+|50270|ambari|DataBase-01:41512|ambari|Sleep|23||NULL||50271|ambari|DataBase-01:41511|ambari|Sleep|6||NULL||50272|ambari|DataBase-01:41514|ambari|Sleep|23||NULL||62452|oozie|DataBase-02:42987|oozie|Sleep|25||NULL||63660|ambari|DataBase-01:56052|ambari|Sleep|0||NULL||110404|push_user|localhost:33817|quartz|Sleep|12||NULL||112835|push_user|localhost:46571|hibernate|Sleep|1||NULL||113163|push_user|localhost:56585|hibernate|Sleep|1||NULL||113289|push_user|14.118.132.20:47333|DW|Sleep|628||NULL||113320|push_user|localhost:47428|hibernate|Sleep|3||NULL||113321|push_user|localhost:47429|hibernate|Sleep|3||NULL||113322|push_user|localhost:47430|hibernate|Sleep|3||NULL||113357|push_user|localhost:52337|hibernate|Sleep|3||NULL||113364|push_user|localhost:57206|hibernate|Sleep|3||NULL||113366|push_user|localhost:34813|hibernate|Sleep|1||NULL||113398|push_user|localhost:37382|hibernate|Sleep|1||NULL||113498|push_user|localhost:47626|quartz|Sleep|12717||NULL||113709|push_user|localhost:59382|hibernate|Sleep|1||NULL||113710|push_user|localhost:33627|hibernate|Sleep|1||NULL||113715|hive|DataBase-02:54968|hive|Sleep|2390||NULL||113716|hive|DataBase-02:54969|hive|Sleep|2390||NULL||113717|hive|DataBase-02:54974|hive|Sleep|2336||NULL||113718|hive|DataBase-02:54975|hive|Sleep|2336||NULL||113719|push_user|localhost:48243|hibernate|Sleep|1||NULL||113720|push_user|localhost:48245|hibernate|Sleep|1||NULL||113721|push_user|localhost:48244|hibernate|Sleep|1||NULL||113722|push_user|localhost:48247|hibernate|Sleep|1||NULL||113723|push_user|localhost:48249|hibernate|Sleep|1||NULL||113724|push_user|localhost:48248|hibernate|Sleep|1||NULL||113745|push_user|localhost:50684|hibernate|Sleep|1||NULL||113746|push_user|localhost:50685|hibernate|Sleep|1||NULL||113747|push_user|localhost:50695|hibernate|Sleep|1||NULL||113748|push_user|localhost:50696|hibernate|Sleep|1||NULL||113749|push_user|localhost:50697|hibernate|Sleep|1||NULL||113750|push_user|localhost:50699|hibernate|Sleep|1||NULL||113751|push_user|localhost:50700|hibernate|Sleep|1||NULL||113752|push_user|localhost|information_schema|Query|0|NULL|showprocesslist||113753|push_user|14.118.132.20:28688|DW|Sleep|396||NULL|+--------+-----------+---------------------+--------------------+---------+-------+-------+------------------+38rowsinset(0.00sec)或者通过如下命令查询:mysql>useinformation_schemamysql>select*fromPROCESSLISTwhereinfoisnotnull+--------+-----------+-----------+--------------------+---------+------+-----------+--------------------------------------------------+|ID|USER|HOST|DB|COMMAND|TIME|STATE|INFO|+--------+-----------+-----------+--------------------+---------+------+-----------+--------------------------------------------------+|113752|push_user|localhost|information_schema|Query|0|executing|select*fromPROCESSLISTwhereinfoisnotnull|+--------+-----------+-----------+--------------------+---------+------+-----------+--------------------------------------------------+1rowinset(0.00sec)

问题

我们有一个 SQL,用于找到没有主键 / 唯一键的表,但是在 MySQL 5.7 上运行特别慢,怎么办?

实验

我们搭建一个 MySQL 5.7 的环境,此处省略搭建步骤。

写个简单的脚本,制造一批带主键和不带主键的表:

执行一下脚本:

现在执行以下 SQL 看看效果:

...

执行了 16.80s,感觉是非常慢了。

现在用一下 DBA 三板斧,看看执行计划:

感觉有点惨,由于 information_schema.columns 是元数据表,没有必要的统计信息。

那我们来 show warnings 看看 MySQL 改写后的 SQL:

我们格式化一下 SQL:

可以看到 MySQL 将

select from A where A.x not in (select x from B) //非关联子查询

转换成了

select from A where not exists (select 1 from B where B.x = a.x) //关联子查询

如果我们自己是 MySQL,在执行非关联子查询时,可以使用很简单的策略:

select from A where A.x not in (select x from B where ...) //非关联子查询:1. 扫描 B 表中的所有记录,找到满足条件的记录,存放在临时表 C 中,建好索引2. 扫描 A 表中的记录,与临时表 C 中的记录进行比对,直接在索引里比对,

而关联子查询就需要循环迭代:

select from A where not exists (select 1 from B where B.x = a.x and ...) //关联子查询扫描 A 表的每一条记录 rA:     扫描 B 表,找到其中的第一条满足 rA 条件的记录。

显然,关联子查询的扫描成本会高于非关联子查询。

我们希望 MySQL 能先"缓存"子查询的结果(缓存这一步叫物化,MATERIALIZATION),但MySQL 认为不缓存更快,我们就需要给予 MySQL 一定指导。

...

可以看到执行时间变成了 0.67s。

整理

我们诊断的关键点如下:

\1. 对于 information_schema 中的元数据表,执行计划不能提供有效信息。

\2. 通过查看 MySQL 改写后的 SQL,我们猜测了优化器发生了误判。

\3. 我们增加了 hint,指导 MySQL 正确进行优化判断。

但目前我们的实验仅限于猜测,猜中了万事大吉,猜不中就无法做出好的诊断。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/7559859.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-07
下一篇2023-04-07

发表评论

登录后才能评论

评论列表(0条)

    保存