select query in user sleep state - mysql

mysql server block many query, when i use show processlist, i found a simple query started 30 days ago
Command: Query
Time: 2262201
State: User sleep
Info: SELECT `abc` FROM `xxx` WHERE `a`='a' AND `b`='b';
below is mysql document explain about User sleep
https://dev.mysql.com/doc/refman/5.7/en/general-thread-states.html
The thread has invoked a SLEEP() call.
but this query string not include SLEEP function, why cause this state?
xxx is InnoDB table; mysql version is 5.7.32-log

Related

Get MySQL processlist log every 5 seconds

How can I write a cron job that runs the MySQL "show processlist" command and stores in log file every 5 seconds between 5 am to 7 am?
I know the lowest possible timing I can have in cron is a minute not second. If I need a script, I am looking for a solution in Bash.
I think this cron job works for every 5 minutes between 5 am to 7 am.
*/5 5-7 * * * mysql -ufoo --password='' -te "show full processlist" > /home/foo/log/show_processlist.log.`date +\%Y\%m\%d-\%H\%M` 2>&1
You can use mysqladmin which is MySQL CLI command to administrate database,
mysqladmin -u root -p -i 5 processlis
Press CTRL+C to stop the running command anytime and use "--verbose" to see the full query.
Set a chron task to run at 05:00 which executes a script that loops over a time value and sleeps for 5 seconds between those time values. May not be exactly 5 seconds as sleep is usually the min time to sleep, but should be close enough.
You can write shell/Python/php/something else script which should be run by cron job minutely.
This script should have the following logic (pseudo code):
i = 0
while i < 20:
i++
show full processlist
delay 4 sec
Want a bash command on every second to see FULL queries?
while [ true ]; do mysql -u MYSQLUSERNAME -p'MYSQLUSERPASSWORD' --execute='SHOW FULL processlist;'; sleep 1; done;
Assuming you're on a safe environment to enter raw MYSQL password ;).

Automatic kill/timeout slow queries in MySQL

Is there some configuration that can be done at MySQL side to automatically kill or timeout queries that are extremely slow, say 100 seconds.
You can list all your MySQL queries by the following command:
$ mysqladmin processlist
so you can run some script which will parse that list and it'll kill the specific query.
In example, you can run some script in any language via cron to periodically check for the long queries, e.g.:
$result = mysql_query("SHOW FULL PROCESSLIST");
while ($row=mysql_fetch_array($result)) {
$process_id=$row["Id"];
if ($row["Time"] > 200 ) {
$sql="KILL $process_id";
mysql_query($sql);
}
}
Another example:
mysql> select concat('KILL ',id,';') from information_schema.processlist
where user='root' and time > 200 into outfile '/tmp/a.txt';
mysql> source /tmp/a.txt;
Related:
How do I kill all the processes in Mysql "show processlist"?
Read more:
http://www.mysqlperformanceblog.com/
Starting with MySQL 5.1 you can create a stored procedure to query the information_schmea.PROCESSLIST table for all queries that match your criteria ("long running time" in your case). Upon querying processlist table, you can simply iterate over a cursor to kill the running query processes that exceeds your timeout criteria.
Take a look at the following example: Procedure to find and terminate all non-SUPER and "system account" queries running longer than N seconds
http://forge.mysql.com/tools/tool.php?id=106
You should checkout the command pt-kill of percona toolkit

Estimating MySQL Query Runtime

I ran a MySQL query from command line using the following:
nohup mysql --user=root --password=XXXXXXXXX database < report.sql > results.tab
This query is extensive (5000 lines) and running a complex search over 90,000,000 rows from 150 tables.
This query has been running for over 24 hours and has not yet finished.
Is there a way I can run status check on this running query?
try show processlist in mysql if that works.

How to close existing connection in MySQL

I found that some connection got unclosed after the execution of command from Mysql server.
How can I configure my Mysql server so that I can close them all after executing a command?
if you can get the process_id inside mysql you can kill the process. Killing any process should work (though it will create a new connection next time you send a command).
mysql> SHOW PROCESSLIST; -- or SHOW FULL PROCESSLIST
mysql> KILL process_number;
Configure the wait_timeout variable to something soon enough, for example 30 seconds

Find the query from query_id in mysql

Can I find the exact query from mysql query id?
This is a part of "SHOW ENGINE INNODB STATUS" in MySQL:
MySQL thread id 1106, query id 1360 localhost 127.0.0.1 test2
---TRANSACTION 0 19491, not started, OS thread id 2960035840
Is there a way by which I can find what was the query with id 1360?
Some folks say turn on 'general log' and you'll find your query by id.
http://forums.mysql.com/read.php?22,419784,419896#msg-419896
Just added this line to my.cnf
log=/tmp/mysql_query.log
Then restarted mysql service (/etc/init.d/mysql stop /etc/init.d/mysql start)
Then tailed the log file. Seemingly there is a query id in it!
110825 15:07:49 36 Connect ***#localhost on ***
...
36 Query SELECT * FROM genre g LIMIT 0,1000
36 Quit
See also http://www.jeff-barr.com/?p=112 and http://dev.mysql.com/doc/refman/5.1/en/query-log.html
You can use the following command:
SHOW PROCESSLIST;
It will give you all currently running process with their query ID and query that is being executed.