Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check how long MySQL query is taking?

When I run a query, I don't want it to print to the console. I just want to see the time.

9166 rows in set (0.90 sec)

Thats all I want to see, instead of it printing everything.

like image 887
TIMEX Avatar asked Jul 31 '12 08:07

TIMEX


People also ask

How can I tell how long a MySQL query is?

Now, go to the Scalyr dashboard menu and select MySQL. You will be able to see the log details of your MySQL, which includes the query time. This is a very simple and easy way to measure query time for a large number of MySQL queries.

How do I know which query takes a long time?

You can view this by Right Clicking on Instance Name in SQL Server Management Studio and selecting “Activity Monitor”. Activity monitor tells you what the current and recent activities are in your SQL Server Instance. The above screenshot displays an overview window for the Activity Monitor.

Why is MySQL query taking so long?

There are a number of things that may cause a query to take longer time to execute: Inefficient query – Use non-indexed columns while lookup or joining, thus MySQL takes longer time to match the condition. Table lock – The table is locked, by global lock or explicit table lock when the query is trying to access it.

How do I find slow query logs?

By default, the slow query log file is located at /var/lib/mysql/hostname-slow. log. We can also set up another location as shown in listing 03 using the slow_query_log_file parameter. We can also indicate to log queries not using indexes, as shown in the listing 04.


2 Answers

Try this simple example

mysql> set profiling=1;
mysql> select count(*) from comment;
mysql> select count(*) from message;
mysql> show profiles;

+----------+------------+------------------------------+
| Query_ID | Duration   | Query                        |
+----------+------------+------------------------------+
|        1 | 0.00012700 | select count(*) from comment |
|        2 | 0.00014200 | select count(*) from message |
+----------+------------+------------------------------+
2 rows in set (0.00 sec)
like image 89
Antaresm Avatar answered Sep 25 '22 02:09

Antaresm


You can write your query inside sub-query with COUNT to do the trick as:

SELECT COUNT(1)
FROM ( SELECT * FROM your_table WHERE ...) a

It may slow down your query slightly, as it's doing COUNT also but I think its negligible.

For measuring performance of query you can turn on PROFILES in MySQL as:

SET profiling = 1;

For more details about PROFILES see here.

like image 37
Omesh Avatar answered Sep 24 '22 02:09

Omesh