Check status of the long running query - mysql

I have a database query which deletes from database with some given conditions. The query is being initiated by an endpoint which timeouts if the query takes a long time (which it does). The service which calls this endpoint needs to do some other tasks after this operation is successful. So we need to know when this query completes. The query is :
DELETE FROM foo WHERE creation_time BETWEEN ? AND ? AND bar_id = ?
How do I know when the query completes? I am using jdbctemplate for querying the database.
I thought of one option but now sure how the between query works internally.
I thought of creating a status endpoint which checks if the query is still running? The endpoint will check if there is any row in foo where creation_time = FROM and bar_id = id.
But I don't know how mysql handles the BETWEEN query internally. If it starts deleting from the FROM or the TO or anything in between.
Short question : How do I check if my query is still running or finished with jdbctemplate?

You can execute the SHOW FULL PROCESSLIST; command. See the MySQL docs for a good description of the command.
This will show all running threads, along with the query that is currently executing and the time that it has been running.
Then parse the output to see if your query is still running.
Hope this helps.

Related

mysql query with state showing "Sending data" or "Writing to net" can't be cancelled through statement.cancel() method

I make a mysql query joining two large tables without on condition filter,
such as select * from a,b.
I hit show processlist command and show the sql state is being Sending data, then i invoke my code statement.cancel().
I wonder the query has been killed ,but i'm wrong, the query is still being Sending data state. I notice that mysql implementation execute the following code
if (!this.statementExecuting.get()) {
return;
}
could someone have other avenue to cancel such queries ? appreciate a lot.
Have you tried interrupting the thread that waits for the query execution to finish?
I assume you are running multiple threads as you are able to call statement.cancel() while the statement is being executed. So you should be able to get a reference to this thread and call thread.interrupt() on it.
Not sure how the JDBC driver will handle the InterruptedException - maybe you will have to take care about it in your code.

What is meant by "prepared statement has pending or unread results"?

What is written on the PHP Manual and also one comment from the manual says:
Closes a prepared statement.
mysqli_stmt_close() also deallocates
the statement handle. If the current
statement has pending or unread
results, this function cancels them so
that the next query can be executed.
Comment:
if you are repeating an statement in
an loop using bind_param and so on
inside it for a larger operation. i
thougt id would be good to clean it
with stmt->close. but it broke always
with an error after aprox. 250
operations . As i tried it with
stmt->reset it worked for me.
Here I don't understand what is the meaning of "prepared statement
has pending or unread results"?
An RDBMS that is running a query can return data before the entire dataset has been processed. There can also be records that it has not read yet.
Both the records that are already read and the ones that are pending must be saved in some resource in the database server, usually called a 'cursor'.
You execute the application code statement that reads these records from the server's cursor and into your application's memory, with PHP's MySQi wrappers those are the called the fetch methods.
Now after executing a query, you are not obliged to fetch any or all these results. So either way, after reading the results of the query or not, executing mysqli_stmt_close() tells the server it can discard the cursor, i.e. remove the already read records from its memory and cancel the optionally still running query.
So:
Unread results: fetched from the database, but not read by the client.
Pending results: records that will be included in the result set once the query runs to completion.

How to detect if query is running in the state “Sending data”

I read some explanations about "sending data" status but I still don't get if the query is running or not. They say "sending data" means server sending some data to client but I really don't which data is sending.
What does it mean when MySQL is in the state "Sending data"?
I run some query using Mysql Workbench and while this query is executing Workbench goes timeout(after 10 min). Then I run "show processlist" command to see if query is continues to executing or not. It says my query status is "sending data".
By the way logs table has 10 million records. So this query must be finish in 10 hours. I just want to know if my query is really executing still?
update logs join user
set logs.userid=user.userid
where logs.log_detail LIKE concat("%",user.userID,"%");
When it's in the process list it is still running. Your query is just running very slow, I assume, cause you're doing a cross join (which means you connect every column of one table to every column of the other table, which can result in quite an enormous amount of data, therefore I further assume, that your query does not do, what you think it does) and no index can be used on the where clause. You're probably doing a full table scan on a very huge amount of data. You can verify this by doing an explain <your query>;.
To avoid the cross join specify the connection in an on clause, like
update logs join user ON logs.userid = user.userid
set logs.whatever = user.whatever
where logs.log_detail LIKE concat("%",user.userID,"%");

Know thread Id of procedure

Doing
SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST WHERE STATE = "executing";
It contains a column Id which is the Id number of all the threads currently running. Now in my MySQL procedure, at the beginning I wish to know the ID of the thread executing it. What query will return the ID of the thread running the procedure?
Secondly: I wish to know it because, the queries in my application are quite long running. I want to automate the process that as soon I get a fresh request, my application will cancel the already running query by Kill query someID. For this I need to know the ID of already running procedure. Does Kill query command affect the stability? As per documentation, it looks safe as it only sets a flag and connection is intact.
Use the CONNECTION_ID() function.
http://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_connection-id

Does MySQL store somehow the last querie(s)?

While working with MySQL and some really "performance greedy queries" I noticed, that if I run such a greedy query it could take 2 or 3 minutes to be computed. But if I retry the query immediately after it finished the first time, it takes only some seconds. Does MySQL store something like "the last x queries"?
The short answer is yes. there is a Query Cache.
The query cache stores the text of a SELECT statement together with the corresponding result that was sent to the client. If an identical statement is received later, the server retrieves the results from the query cache rather than parsing and executing the statement again. The query cache is shared among sessions, so a result set generated by one client can be sent in response to the same query issued by another client.
from here
The execution plan for the query will be calculated and re-used. The data can be cached, so subsequent executions will be faster.
Yes, depending on how the MySQL Server is configured, it may be using the query cache. This stores the results of identical queries until a certain limit (which you can set if you control the server) has been reached. Read http://dev.mysql.com/doc/refman/5.1/en/query-cache.html to find out more about how to tune your query cache to speed up your application if it issues many identical queries.