7

I need to find some measure of how long my queries are taking and the load on the server if possible.

I doubt its possible, but I would like to gain the cpu usage too.

Any ideas?

Jules
  • 7,190
  • 14
  • 93
  • 171

7 Answers7

21

PHP Syntax

$starttime = microtime(); 
$query = mysql_query("select * from table"); 
$endtime = microtime();

Then calculate the difference between $starttime and $endtime.

Alix Axel
  • 141,486
  • 84
  • 375
  • 483
Poonam Bhatt
  • 9,400
  • 16
  • 47
  • 69
  • I'd vote for you, but you should only used syntax highlight for code. – fncomp Nov 19 '10 at 08:25
  • As the database and the webserver are on different servers, this would not be able to distinguish if the database was going slow or the webserver. – Jules Nov 22 '10 at 12:06
  • Considering the PHP page is just waiting for the MySQL query to finish, this is probably sufficiently good to determine query execution time. It's unlikely that even a slow web server would impact the reported speed; if it were that slow, then you'd have other more obvious problems. But for reasons you've already mentioned, this won't help find CPU usage. – Matthew Nov 22 '10 at 23:29
  • @Matthew, This is wall time not CPU time. – Pacerier Nov 01 '14 at 13:24
6

Have a look at the query profiler. This may do the query time bit of what you need.

https://www.digitalocean.com/community/tutorials/how-to-use-mysql-query-profiling

https://dev.mysql.com/doc/refman/5.5/en/show-profile.html

There are various tools for seeing the load on the server

Jaydee
  • 4,090
  • 1
  • 16
  • 20
  • But I can't see how to output this within php ? I really want to have some error trapping in php which I can send an email if things get bad. – Jules Nov 15 '10 at 10:22
  • 1
    You could enable profiling at the beginning of the script and then do a `show profiles` query at the end. You can use the standard PHP functions that you are used to. – Matthew Nov 22 '10 at 23:31
  • 1
    the link does not exist anymore – Odin Oct 03 '12 at 10:27
  • Link is broken again – pun Jul 04 '16 at 04:09
2

Is almost impossible given your database and web server are located differently
plus you try to achieve it using PHP

the limitation

  • you need to have access to both servers (such as ssh)
  • if you embed CPU load checking such as a exec_shell ssh into database and return uptime upon every query execution, is overkill

workaround

embed a cronjob in your database server,
periodically sent email if the server load went high

Or

at the database,
periodically sent email on current running query using
show full processlist

Example to store SHOW FULL PROCESSLIST

$con = mysqli_connect(...) or die('unable to connect');
$sql = "show full processlist";
$res = mysqli_query($con, $sql) or die($sql);

/* optional path is /tmp */
$fp  = fopen('/tmp/'.date('YmdHis'), 'w+');
while ($row = $res->fetch_row())
{
  fputcsv($fp, $row);
}
$res->free_result();

The above should be sufficient to dump current mysql process-list into a file.
In linux box, there are lots of commands allow user to show CPU load.
But is windows, I guess you can figure out with some search on google of SO

ajreal
  • 44,929
  • 10
  • 81
  • 118
  • I'm using a shared server with my ISPs hosting package, so would I be able to distinguish which queries time / load are my fault ? – Jules Nov 18 '10 at 19:35
  • @Jules - Pretty sure your database is isolated with other. Ask your ISPs to start profiling the `show full processlist` and email you as a record or stored into server for you to access later. – ajreal Nov 18 '10 at 19:46
  • @Jules - you can use the `show full processlist` using your database connection – ajreal Nov 21 '10 at 09:39
  • I asked my ISP `Ask your ISPs to start profiling the show full processlist and email you as a record or stored into server for you to access later` and he said he wasn't what yo mean. We tried that command it pypmyadmin and it only should what was running at that moment in time. He has a windows server. – Jules Nov 22 '10 at 07:41
  • OMG, does it mean your hosting server is in window? If your hosting server is a linux box, ask him to set a cron that explicitly `show full processlist` on your database and pipe it into email or disk file. Anywhere, your ISP unable to trace where the load come from (a big question ONE), and the guy you contacted does not know how to setup a scheduler (a big question TWO). This is ironic. – ajreal Nov 22 '10 at 07:48
  • Check with the ISP again, do they have some utilities of windows task scheduler. If the server has that, you can advise your ISP to start a task that monitor `show full processlist` every minute. The output can send as an email, or output into a location on your server. So whenever your server having spike on load, you can check WHAT are the queries that running at the same time. If there nothing significant slow query, probably your application is not the culprit. – ajreal Nov 22 '10 at 12:06
  • There isn't such a task to do all that. – Jules Nov 22 '10 at 21:42
  • Pretty sure either window or linux box is capable to perform scheduler. Read this - http://stackoverflow.com/questions/132971/what-is-the-windows-version-of-cron . Are you able to access and setup scheduler on your window web server ? The alternative is for you to setup a scheduler and do `show full processlist` to your database. – ajreal Nov 23 '10 at 04:41
  • Yes scheduling a task can be done easily, but parsing `show full processlist` to a file(s) then stripping out just the items which are for me then emailing out isn't – Jules Nov 23 '10 at 07:44
  • Easier will be prepare a PHP script, execute the show query, iterate the results (fetch_row) and fputcsv into a file using file-name like '20101122080000'. Refer to updated results – ajreal Nov 23 '10 at 08:02
0

There is quite a bit of information you can get about database using

SHOW STATUS;

query.

I would assume Last_query_cost variable could be quite useful for your purposes of measurements. As well as Slow_queries which will show the amount of quires which was running for longer then certain amount of time. The full list of this variables is here http://dev.mysql.com/doc/refman/5.1/en/server-status-variables.html There is also a php function mysql_stat() php function which returns some database usage data. For example Queries per second could be somewhat useful. But to take a hold of a more certain data will require ssh access to the database.

Ivan
  • 3,449
  • 14
  • 25
  • Last_query_cost always seems to return zero. I don't have access to slow queries as it holds queries from other hosting clients. Also slow queries is zero. – Jules Nov 22 '10 at 12:04
0

use microtime to measure the time required for a query.

For server load, see http://www.php.net/manual/en/function.sys-getloadavg.php . CPU usage is mostly irrelevant, because mysql will be disk-bound most of the time.

Quamis
  • 10,181
  • 12
  • 45
  • 63
0

For more detailed stats, you should use mysqli::get_connection_stats (if you use php 5.3+)

http://www.php.net/manual/en/mysqli.get-connection-stats.php

ts.
  • 9,930
  • 7
  • 42
  • 67
0

PHP can really only time the mysql_query() execution time, which includes the whole round trip, latency and transfer times. To have a break down you will need to use the MySQL profiler as mentioned already. The following code should output the information you need to know. If you want to incorporate the profiling into your PHP process, you will have to use the profiler and select relevant fields from the information_schema.profiling table, but if it is just to check the performance the below should suffice.

<?php

$con = mysql_connect("localhost","root","my-password");
if (!$con)
{
 die('Could not connect: ' . mysql_error());
}

mysql_select_db("my-database", $con);

function microtime_float()
{
 list($usec, $sec) = explode(" ", microtime());
 return ((float)$usec + (float)$sec);
}

mysql_query("set profiling=1;");

$starttime = microtime_float();
$query = mysql_query("SELECT * FROM my-table"); 
$endtime = microtime_float();
$trans_result = mysql_query("select sum(duration) as transtime from information_schema.profiling where query_id=1");
$transtime = mysql_result($trans_result, 0, 'transtime');
$total_time = ($endtime - $starttime); 
$transtime = ($total_time - $transtime); 

echo 'Total time: '.$total_time.' secs<br />';
echo 'Transfer time: '.$transtime.' secs<br />';
echo 'Query time break-down;<br />';

$debug_result = mysql_query("show profile cpu for query 1;");
while ($row = mysql_fetch_assoc($debug_result)) {
     echo $row['Status'].' (Time: '.$row['Duration'].', CPU_User: '.$row['CPU_user'].', CPY_sys: '.$row['CPU_system'].')<br />';    
}
?>

Once you are happy with performance, remove all but the MySQL query you want.

Community
  • 1
  • 1
Ghostpsalm
  • 518
  • 3
  • 6