What is the purpose of the `KILL QUERY` command? - mysql

I am beginner to MySQL and SQL and I am learning these technologies from MySQL and MyISAM websites.
I am learning the KILL command now and have learned how to use KILL CONNECTION. But when I come to learn KILL QUERY from MySQL and MyISAM websites where they simply provided information about KILL QUERY that they terminate query that the specified connection id is executing.
Other user (not me) executes INSERT and DELETE queries but when i have used KILL QUERY by getting the connection id of other user via SHOW PROCESSLIST command, it says
Query OK, 0 rows affected (0.01 sec)
But nothing happen to other user.
I just want to know that what is the exact use of this command. What is the purpose of this KILL QUERY command. Can anyone tell me?

Some useful pointers:
How can I stop a running MySQL query?
MySQL Kill query
How to kill all processes for a Specific user
MySQL Manaul - Kill
You need to have some clarity here KILL will kill the connection, but KILL QUERY will only kill that query and will leave that connection intact.
Really, if you are using KILL QUERY on another users command, this can only really be noticable if their query is running for longer than your Kill Query command, so if it's a slow query, because [most] queries will execute and close within a fraction of a second, so by the time you've KILLed the other users query, their query may well have completed and closed anyway.
I suspect this is what you are seeing, that you're trying to kill a query that closes and is finished before the kill command can execute.
Killing queries seems (from literature) only useful for slow and long running queries, and why this is so should be obvious from the above.
Edit:
(I had this ready to post on my original answer and then took it out as being irrelevant, but seems to be more relevant now due to comments!)
From the MySQL Manual:
Note
You cannot use KILL with the Embedded MySQL Server library because the embedded server merely runs inside the threads of the host application. It does not create any connection threads of its own.
So to answer your question in the comment, I think that you may be running MySQL embedded so your kill calls will not function.

Related

Does my sql query continue executing after i been disconnected due to long operation?

If I run a SQL query in MySQL workbench and the connections time out after 30 seconds because it is taking a long time. Does my Query continue executing on the MySQL server even though I am disconnected?
For example, if I am doing an update and the update loops over a billion records. Does the MySQL server disconnect me first then it finishes the query after? Or does it disconnect me and terminate the query?
It does. As Mustafa mentioned, you can see the query still running if you look at "Administration tab" --> Management --> Client Connections.
Also good to remember that you can change the 30sec cap to longer, shorter or none.
Yes, MySQL Workbench can disconnect and the query keeps running. This has been reported as a bug, but it's in the "Verified" state, which means it is not fixed: https://bugs.mysql.com/bug.php?id=78809
See also this related SO thread: MySQL Query running even after losing connection
If you have a long-running query that needs to do a bulk update, you may need to change the MySQL Session timeout options in the MySQL Workbench preferences. Alternatively, don't use MySQL Workbench for long-running jobs, use the mysql command line tool.

mySQL loses connection on one table, doesn't on any others

I have a MySQL database with multiple tables. When I run a simple SELECT command on one of the tables, it gives the "lost connection" error, but when I run the same command on any of the other tables, it works just fine. Does anyone know what could be the cause of this issue? I'm running the commands from the MySQL Workbench.
You must set the 'Interactive_timeout' and 'wait_timeout' properties in the mysql configuration file to the values ​​you need.
Quite likely there is some sort of a transaction running. Whether you started the transaction via a sql command (i.e. START TRANSACTION) or whether some other query (possibly from a GUI) started a transaction (Like an ALTER TABLE command when adding a column) and the transaction never finished.
Run the command
SHOW FULL PROCESSLIST;
to find the IDs of all processes that are running for your user
You can then kill the processes by running the command
KILL <PROCESS ID> (i.e. KILL 40196;)
When you find and stop the process that contained the transaction that never finished you will likely be able to query the table again. Your table will be queryable again, and you can look further into solving the problem of why the transaction was started but never finished.

Enable alerts for MySQL long running queries

My application is loading very slow. After doing some research I got to know that MySQL is causing this slowness. I have around 15-20 users that access this server. After preliminary investigation (googling and stackoverflow), I found out that they were some queries running at the time and those were the culprit. It’s annoying to run query every now and then to look out for the queries running for a long time. Is there a workaround for this and also get email/SMS alerts for it. How can I enable email/SMS alerts to look over those queries
execute below commands on mysql from admin/root user-
slow_query_log = ON;
long_query_time = 2;
Now get slow query log file path from belowcommand-
SHOW VARIABLES LIKE 'slow_query_log_file';
Now go to your mysql db machine and check logged slow queries and optimize them.
Note: By this you can get slow queries even without restart mysql service but for better do these entries in your conf file so that you can get slow logs even after service restart.

Why are queries executed from mysql workbench taking much longer than executing them directly from the mysql cli?

I have a query that does multiple joins on several tables and uses a covering index. It seems to run a lot faster when executed directly from the mysql cli instead of from mysql workbench.
mysql > some query
...
250 rows in set (0.05 sec)
from mysql workbench it takes about ~0.200 sec duration / 0.100 sec fetch
Is there a reason beyond latency, authentication and transfer of data why it would be orders of magnitude faster on the console? Does a three-way tcp handshake occur each time a query is executed from mysql workbench or does that tcp connection stay open until you close the workbench session?
There's a persistent connection in Workbench, so there's no overhead in that regard. Executing a query should be equally fast in both CLI as well as Workbench, however data transfer is a bit slower sometimes because Workbench locally caches results first to be able to sort over it when the user clicks a header field in the resultset view (including multi column sort). Try repeating the query in both tools and see if the times still differ. The first run of a query is usually slower than following runs due to the execution cache.

Will mysql somehow rerun killed query behind my back?

I ran a very unique experimental long query about 1 week ago from Sequel Pro against a MySQL 5.5 DB. The query is not used in any codes; just a manual one. I remember I killed it after only a few seconds. Then in the last few days, the DBA keeps finding the exact same query was started again after one being killed. The DBA has verified the query was killed at the time he tried. My workstation has been rebooted at least once and moved out of network connection many times since I first ran that query manually. Sequel Pro had no connection to any DB when one of this rerun occurred. And there seems nothing else in my workstation that would trigger that.
My question: is there some way that the query can get stuck in some server-side job/run list and not being killed properly but get rerun?
Found the cause. A DBA has cron script running in the background looking for slow queries in the slow query log and try to run explain on the query! Apparently the slow queries get rerun again and again.