PHP gets query time for MySQL Execute SQL statement

Source: Internet
Author: User
Tags explode

Timing starts runtime (); Execute Query mysql_query ($sql); The timing is over. Echo runtime (1); Timer function   Runtime ($mode =0)   {    static   $t;       if (! $mode)   {           $t   =   microtime ();        return;    }       $t 1   =   microtime ();       List ($m 0, $s 0)   =   explode ("   ", $t);       List ($m 1, $s 1)   =   explode ("   ", $t 1);       Return   

 

The execution time of SQL can be analyzed by:

1, determine whether the SQL writing is reasonable, efficient

2, check whether the design of the field, table is reasonable

Method 1: Rewrite the SQL Operation class at the bottom of the system, usually the class structure is

Business model---"DB Class---" Execute SQL

Can be rewritten according to the circumstances, such as DB classes;

1 Public Function execute ($sql)  {2//code ... 3  4/* Detects SQL execution time, exceeds execution time to log */5 $start _time = array_sum (Explode (", microtime ())); 6  7 $this->lastresult = m Ysql_query ($sql, $this->link) or $this->displayerror ($sql); 8  9 $end _time = array_sum (Explode (", microtime ())), $differ = $end _time-$start _time;11 if ($differ >0.001) {
   //modify 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 ");}16-//code ... 19}

Reference:

phpMyAdmin in the code, get query execution time 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 method can also use the MySQL profile, you can refer to the @php SQL execution time monitoring?
This is a better way to count the execution of multiple SQL.
I have seen it as if a blog, after visiting the page will have a hint about how many times the database has been queried, and how long it takes to query the data, then the profile of MySQL can be easily implemented.

Annotation 1:micortime function

The function Micortime (), which calculates the microsecond, can return the current UNIX timestamp and the number of microseconds. Returns the number of floating-point units in seconds. However, the function is only available under the operating system that supports Gettimeofday () system calls. You can find out more about the handbook below. May cause some unknown error, note.

Comment 2:profile Save up to 100 records, how can this be solved?

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.
This is the maximum of 100, can not be changed.

Reference 2:php to get the millisecond timestamp method

Java can be obtained by gettime (). If you are making high-precision, millisecond-level docking communications with some programs written by Java, you will need to use PHP to output milliseconds of time. To get a more precise millisecond timestamp, use the following code:

<?phpfunction Getmillisecond () {list ($t 1, $t 2) = Explode ("", Microtime ()), return (float) sprintf ('%.0f ', (Floatval ($ T1) +floatval ($t 2)) *1000);} Echo Getmillisecond ();

Run Result: 1.46647658229E+12

PHP gets query time for MySQL Execute SQL statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.