PHP gets the query time method of MySQL executing sql statement
- 2021-10-27 06:42:28
- OfStack
As shown below:
// Timing start
runtime();
// Execute a query
mysql_query($sql);
// End of timing .
echo runtime(1);
// Timing function
function runtime($mode=0) {
static $t;
if(!$mode) {
$t = microtime();
return;
}
$t1 = microtime();
list($m0,$s0) = explode(" ",$t);
list($m1,$s1) = explode(" ",$t1);
return sprintf("%.3f ms",($s1+$m1-$s0-$m0)*1000);
}
Analyzing the execution time of sql can:
1. Determine whether the writing of sql is reasonable and efficient
2. Check whether the design of fields and tables is reasonable
Method 1: Overwrite the sql operation class at the bottom of the system. Generally, the structure of the class is
Service model--"db Class--" Executes sql
It can be rewritten at a certain stage according to the situation, such as db class; It is usually modified
public function execute($sql) {
//code...
/* Detection sql Execution time, which is recorded in the log if it exceeds the execution time */
$start_time = array_sum(explode(' ', microtime()));
$this->lastresult = mysql_query($sql,$this->link) or $this->displayerror($sql);
$end_time = array_sum(explode(' ', microtime()));
$differ = $end_time - $start_time;
if($differ >0.001){ // Modification time range in seconds
putContent('sqlLOG', date('Y-m-d H:i:s', $start_time)." "
. date('Y-m-d H:i:s', $end_time)." "
.$differ. " ".$sql."\r\n");
}
//code...
}
Quote:
For the code in phpmyadmin, get the execution time of query as follows:
// garvin: Measure query time.
// TODO-Item http://sourceforge.net/tracker/index.php?func=detail&aid=571934&group_id=23067&atid=377411
$querytime_before = array_sum(explode(' ', microtime()));
$result = @PMA_DBI_try_query($full_sql_query, null, PMA_DBI_QUERY_STORE);
$querytime_after = array_sum(explode(' ', microtime()));
$GLOBALS['querytime'] = $querytime_after - $querytime_before;
In addition to this way, you can also use profile of mysql.
This is more suitable for counting the implementation of multiple sql.
I have seen what seems to be a blog. After visiting the page, there will be a prompt saying that the database was queried several times and how long it took to query the data, so it can be easily realized to open profile of mysql.
Annotation 1: micortime function
The microsecond function micortime () returns the current UNIX timestamp and the number of microseconds. Returns floating-point numbers in seconds. However, the function is only available on operating systems that support gettimeofday () system calls. You can check the manual for a detailed understanding. Some unknown errors may be caused, so pay attention.
Annotation 2: profile can save up to 100 records. How to solve this problem?
profiling_history_size
The number of statements for which to maintain profiling information if profiling is enabled. The default value is 15. The maximum value is 100. Setting the value to 0 effectively disables profiling.
There are 100 articles at most, so we can't change them.
Reference 2: PHP method for obtaining millisecond timestamp
java can be passed through gettime (); Get. If you want to communicate with some programs written by java in milliseconds with high precision, you need to use PHP to output milliseconds. To get a more accurate millisecond timestamp, you can use the following code:
<?php
function getMillisecond() {
list($t1, $t2) = explode(' ', microtime());
return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
}
echo getMillisecond();
Running result: 1.46647658229E +12