Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test speed a query without result in MySQL

I have queries that return thousands of results, is it posible to show only query time without actual results in MySQL console or from command line?

like image 607
jcubic Avatar asked May 29 '12 11:05

jcubic


People also ask

How can I make MySQL query run faster?

Adjust the size and properties of the memory areas that MySQL uses for caching. With efficient use of the InnoDB buffer pool, MyISAM key cache, and the MySQL query cache, repeated queries run faster because the results are retrieved from memory the second and subsequent times.

How do I stop slow queries in MySQL?

MySQL has a built-in slow query log. To use it, open the my. cnf file and set the slow_query_log variable to "On." Set long_query_time to the number of seconds that a query should take to be considered slow, say 0.2. Set slow_query_log_file to the path where you want to save the file.


2 Answers

Use SET profiling = 1; at command prompt.

Refer for more details

It's not possible to get the execution time without getting result or getting sql executed.

See why we can not get execution time without actual query execution

like image 82
Romil Kumar Jain Avatar answered Sep 19 '22 16:09

Romil Kumar Jain


If you're using Linux.

  1. Create a file query.sql
  2. Enter the query to test into it. e.g. select * from table1
  3. Run this command:

time mysql your_db_name -u'db_user_name' -p'your_password' < query.sql > /dev/null

The output will look something like this:

real    0m4.383s    
user    0m0.022s    
sys     0m0.004s

the "real" line is what you're looking at. In the above example, the query took 4.38 seconds.

Obviously, entering your DB password on the command line is not such a great idea, but will do as a quick and dirty workaround.

like image 41
codemonkey Avatar answered Sep 20 '22 16:09

codemonkey