2009-10-27 16 views

risposta

29

mysql ha un builtin profiler. È possibile abilitare la creazione del profilo emettendo set profiling=1; e utilizzare show profiles; per ottenere i tempi di esecuzione.

+0

e SHOW PROFILE; anche –

+0

Questo è quello che cercavo grazie – mck89

+0

[DEPRECATED !!] (http://dev.mysql.com/doc/refman/5.6/en/performance-schema.html) – bobobobo

6

se si utilizza PHP .. è possibile utilizzare microtime() prima della query e dopo la query per capire quanto tempo è necessario per la query da eseguire.

$sql_query='SELECT * FROM table'; 
$msc=microtime(true); 
$mysql_query($sql_query); 
$msc=microtime(true)-$msc; 
echo $msc.' seconds'; // in seconds 
echo ($msc*1000).' milliseconds'; // in millseconds 
+0

Questo non ti dice quanto tempo ha preso MySQL per eseguire la query. Ti dice quanto tempo è occorso a PHP per inviare la richiesta, attendere che fosse ricevuta e ricevere il risultato. Sarà rallentato da qualsiasi cosa possa essere in esecuzione sul client PHP e se c'è una latenza tra il client PHP e il server MySQL, i risultati saranno ulteriormente imprecisi. –

+0

Ottengo risultati strani da questo, a volte riporta '2.15 s' per 8000 righe, altre volte '1.503.023.491.52 s' per 6000 righe. Se applico 'number_format ($ msc, 2)' allora ottengo risultati di eventi estranei come numeri negativi. – Slam

5

Prova questo ...

mysql_query("SET profiling = 1;"); 
if (mysql_errno()) { die("ERROR ".mysql_errno($link) . ": " . mysql_error($link)); } 

/* It goes without saying that this is your actual query that you want to measure the execution time of */ 
$query="SELECT some_field_name FROM some_table_name"; 
$result = mysql_query($query); 
if (mysql_errno()) { die("ERROR ".mysql_errno($link) . ": " . mysql_error($link)); } 

$exec_time_result=mysql_query("SELECT query_id, SUM(duration) FROM information_schema.profiling GROUP BY query_id ORDER BY query_id DESC LIMIT 1;"); 
if (mysql_errno()) { die("ERROR ".mysql_errno($link) . ": " . mysql_error($link)); } 
$exec_time_row = mysql_fetch_array($exec_time_result); 

echo "<p>Query executed in ".$exec_time_row[1].' seconds'; 
+0

Questo viene etichettato come una domanda MySQL. Non tutti quelli che usano MySQL usano PHP. –

Problemi correlati