Mysqlsla is a very helpful tool to analyze and filter query information from accessing query logs. It's used to optimize queries in your local system or server level. Can easily find out slow and general log queries. Mysqlsla report contains execute time, lock time, Rows sent, Rows examined for each unique one,count, minimum, maximum and etc.
Download the mysqlsa package latest version using wget command,
# wget http://hackmysql.com/scripts/mysqlsla-2.03.tar.gz
# tar -xf mysqlsla-2.03.tar.gz
# cd mysqlsla-2.03
# tar -xf mysqlsla-2.03.tar.gz
# cd mysqlsla-2.03
We can easily install that package in three steps ,
# perl Makefile.PL
# make
# make install
# make
# make install
Once have done the installation steps, check the mysqlsla installed path using whereis command,
# whereis mysqlsla
mysqlsla: /usr/local/bin/mysqlsla
mysqlsla: /usr/local/bin/mysqlsla
We will going to check mysqlsla report, If you do not have mysql access logs open my.cnf file add log below line under [mysqld]
# vi /etc/my.cnf
[mysqld]
Log = /var/log/mysql/mysqlqueries.log
[mysqld]
Log = /var/log/mysql/mysqlqueries.log
Create a log file mysqlqueries.log under /var/log/mysql/ and change file permission to mysql
# touch /var/log/mysqlqueries.log
# Chown mysql:mysql /var/log/mysqlqueries.log
#/etc/init.d/mysqld restart
# Chown mysql:mysql /var/log/mysqlqueries.log
#/etc/init.d/mysqld restart
Be ensure that the mysql queries log generating in mysqlqueries.log file. To get mysql queries report top 100 queries,
root@host # mysqlsla --top 100 /var/log/mysql/mysqlqueries.log > /var/log/mysql/mysqlsla-report.log
For sample mysqlsla general query
,
_____________________________________ 001 ______________________
Count : 3.69k (2.76%)
Connection ID : 4
Database : auviitbo_joomla
Users :
linux_db@localhost : 100.00% (3686) of query, 36.39% (48568) of all users
Query abstract:
SELECT folder AS type, element AS name, params FROM uic_extensions
Query sample:
SELECT folder AS type, element AS name, params
FROM uic_extensions
WHERE enabled >= 1 AND type ='plugin' AND state >= 0 AND access IN (1,1)
ORDER BY ordering
Count : 3.69k (2.76%)
Connection ID : 4
Database : auviitbo_joomla
Users :
linux_db@localhost : 100.00% (3686) of query, 36.39% (48568) of all users
Query abstract:
SELECT folder AS type, element AS name, params FROM uic_extensions
Query sample:
SELECT folder AS type, element AS name, params
FROM uic_extensions
WHERE enabled >= 1 AND type ='plugin' AND state >= 0 AND access IN (1,1)
ORDER BY ordering
How to check mysql slow queries for consuming cpu resources?
Collect information about more than long query times seconds to execute. Likewise, we want to configure same as previous# vi /etc/my.cnf
[mysqld]
set-variable=long_query_time=1
log-slow-queries=/var/log/mysql/log-slow-queries.log
# touch /var/log/mysql/log-slow-queries.log
# chown mysql:mysql /var/log/mysql/log-slow-queries.log
# /etc/init.d/mysql restart
[mysqld]
set-variable=long_query_time=1
log-slow-queries=/var/log/mysql/log-slow-queries.log
# touch /var/log/mysql/log-slow-queries.log
# chown mysql:mysql /var/log/mysql/log-slow-queries.log
# /etc/init.d/mysql restart
Do check whether mysql slow queries has enabled by below command,
mysql> SHOW VARIABLES like "%slow%";
+------------------+-----------+
| Variable_name | Value |
+------------------+-----------+
| log_slow_queries | ON |
| slow_launch_time | 2 |
+------------------+-----------+
2 rows in set (0.00 sec)
+------------------+-----------+
| Variable_name | Value |
+------------------+-----------+
| log_slow_queries | ON |
| slow_launch_time | 2 |
+------------------+-----------+
2 rows in set (0.00 sec)
To get top 50 mysql slow queries,
# mysqlsla -lt slow --sort t_sum --top 10 log-slow-queries.log > mysql_slow.log
Example of MySQLsla slow query log,
________________________________________________________ 001 ___
Count : 1 (10.00%)
Time : 4 s total, 4 s avg, 4 s to 4 s max (12.90%)
Lock Time (s) : 0 total, 0 avg, 0 to 0 max (0.00%)
Rows sent : 0 avg, 0 to 0 max (0.00%)
Rows examined : 380.24k avg, 380.24k to 380.24k max (2.63%)
Database : _dataquery
Users :
mysql_eschol@localhost : 100.00% (1) of query, 10.00% (1) of all users
Query abstract:
SELECT date FROM nameofrow WHERE school_id = 'S' AND nameofrow = 'S';
Query sample:
select nameofrow from nameofrow where school_id = '62' and nameofrow = '2014-04-19';
Count : 1 (10.00%)
Time : 4 s total, 4 s avg, 4 s to 4 s max (12.90%)
Lock Time (s) : 0 total, 0 avg, 0 to 0 max (0.00%)
Rows sent : 0 avg, 0 to 0 max (0.00%)
Rows examined : 380.24k avg, 380.24k to 380.24k max (2.63%)
Database : _dataquery
Users :
mysql_eschol@localhost : 100.00% (1) of query, 10.00% (1) of all users
Query abstract:
SELECT date FROM nameofrow WHERE school_id = 'S' AND nameofrow = 'S';
Query sample:
select nameofrow from nameofrow where school_id = '62' and nameofrow = '2014-04-19';
Comments (0)