Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To get MYSQL query execution time in Query

I am developing a query browser, working with some large database and I need to know the time taken for executing the query.

I can see the time once the query is executed succesfully in PHPMYADMIN.

Profiling or Showing rows 0 - 29 (2,000 total, Query took 0.0145 sec)

Ex: Profiling

SELECT * FROM `larger_table`;


Status                        Time
starting                     0.000026
checking permissions         0.000006
Opening tables               0.000014
System lock                  0.000010
init                         0.000022
optimizing                   0.000004
statistics                   0.000007
preparing                    0.000005
executing                    0.000001
Sending data                 0.014138
end                          0.000004
query end                    0.000002
closing tables               0.000005
freeing items                0.000091
logging slow query           0.000003
cleaning up                  0.000002

In Query Browser I can see the time it takes in the bottom of the browser window.

So how can I get the execution time of the query when submit the query for execution.

i.e When I give the following query:

SELECT * FROM `larger_table`;

The query should return the time for execution it.

This I should catch in PHP and show to the Users.When the user gives some query to executed in browser.

Is there any way to find the execution time when the query is submitted.?

Kindly check the Images where I marked the estimated time. enter image description here

enter image description here

I found some thing Check this

like image 819
DonOfDen Avatar asked May 08 '13 07:05

DonOfDen


1 Answers

Please use the code below

$sql_query = 'SELECT * FROM larger_table';
$msc = microtime(true);
mysql_query($sql_query);
$msc = microtime(true) - $msc;
echo $msc . ' seconds'; // in seconds
echo ($msc * 1000) . ' milliseconds'; // in millseconds
like image 150
kapil Avatar answered Oct 06 '22 21:10

kapil