本文實(shí)例講述了mysql慢查詢操作。分享給大家供大家參考,具體如下:
mysql有些sql會(huì)執(zhí)行很慢,有可能造成服務(wù)器負(fù)載飆升
首先查詢 確定影響負(fù)載的是mysql ,使用top命令,ps命令等
其次,進(jìn)入MySQL,使用show full processlist查詢執(zhí)行中的sql語(yǔ)句,看看問題,使用explain 命令 查看狀態(tài)
最后找出sql語(yǔ)句殺死或者優(yōu)化
centos7上面安裝mariadb服務(wù)
yum -y install mariadb-server mariadb-devel
開啟慢查詢
more /etc/my.cnf.d/server.cnf
[mariadb]
slow_query_log=ON
slow_query_log_file=/usr/local/mysql/data/slow.log
long_query_time=1
啟動(dòng)mariadb服務(wù)
查詢mysql的慢查詢是否開啟,以及多久的時(shí)間以上是慢查詢
MariaDB [(none)]> show variables like '%slow_query%';
+---------------------+--------------------------------+
| Variable_name | Value |
+---------------------+--------------------------------+
| slow_query_log | ON |
| slow_query_log_file | /usr/local/mysql/data/slow.log |
+---------------------+--------------------------------+
2 rows in set (0.00 sec)
MariaDB [(none)]> show variables like 'long_query_time';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
1 row in set (0.00 sec)
#如果沒用開啟慢查詢,可以在命令行開啟
mysql> set global slow_query_log=1;
Query OK, 0 rows affected (0.00 sec)
測(cè)試慢查詢,以及查看日志
MariaDB [(none)]> select sleep(2);
+----------+
| sleep(2) |
+----------+
| 0 |
+----------+
1 row in set (2.00 sec)
[root@localhost ~]# more /usr/local/mysql/data/slow.log
/usr/libexec/mysqld, Version: 5.5.60-MariaDB (MariaDB Server). started with:
Tcp port: 0 Unix socket: /var/lib/mysql/mysql.sock
Time Id Command Argument
# Time: 180930 23:51:07
# User@Host: root[root] @ localhost []
# Thread_id: 2 Schema: QC_hit: No
# Query_time: 2.001017 Lock_time: 0.000000 Rows_sent: 1 Rows_examined: 0
SET timestamp=1538322667;
select sleep(2);
確認(rèn)慢查詢
MariaDB [(none)]> show full processlist; #查看state慢查詢?cè)谶M(jìn)行
+----+------+-----------+------+---------+------+------------+-----------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+----+------+-----------+------+---------+------+------------+-----------------------+----------+
| 3 | root | localhost | NULL | Query | 9 | User sleep | select sleep(10) | 0.000 |
| 4 | root | localhost | NULL | Query | 0 | NULL | show full processlist | 0.000 |
+----+------+-----------+------+---------+------+------------+-----------------------+----------+
2 rows in set (0.00 sec)
MariaDB [(none)]> show full processlist; #查看state慢查詢已經(jīng)結(jié)束,但是用戶登陸了
+----+------+-----------+------+---------+------+-------+-----------------------+----------+
| Id | User | Host | db | Command | Time | State | Info | Progress |
+----+------+-----------+------+---------+------+-------+-----------------------+----------+
| 3 | root | localhost | NULL | Sleep | 1 | | NULL | 0.000 |
| 4 | root | localhost | NULL | Query | 0 | NULL | show full processlist | 0.000 |
+----+------+-----------+------+---------+------+-------+-----------------------+----------+
2 rows in set (0.00 sec)
更多關(guān)于MySQL相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《MySQL查詢技巧大全》、《MySQL常用函數(shù)大匯總》、《MySQL日志操作技巧大全》、《MySQL事務(wù)操作技巧匯總》、《MySQL存儲(chǔ)過程技巧大全》及《MySQL數(shù)據(jù)庫(kù)鎖相關(guān)技巧匯總》
希望本文所述對(duì)大家MySQL數(shù)據(jù)庫(kù)計(jì)有所幫助。
您可能感興趣的文章:- MySQL 之壓力測(cè)試工具的使用方法
- PHP+MySQL實(shí)現(xiàn)在線測(cè)試答題實(shí)例
- 詳解Mysql5.7自帶的壓力測(cè)試命令mysqlslap及使用語(yǔ)法
- MySQL如何快速的創(chuàng)建千萬(wàn)級(jí)測(cè)試數(shù)據(jù)
- MySQL占用內(nèi)存較大與CPU過高測(cè)試與解決辦法
- 使用sysbench來(lái)測(cè)試MySQL性能的詳細(xì)教程
- MySQL循環(huán)語(yǔ)句之while循環(huán)測(cè)試
- MySQL數(shù)據(jù)庫(kù)基于sysbench實(shí)現(xiàn)OLTP基準(zhǔn)測(cè)試