亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PHP獲取MySQL執(zhí)行sql語句的查詢時間方法

 更新時間:2018年08月21日 10:05:26   作者:Microtiger  
今天小編就為大家分享一篇PHP獲取MySQL執(zhí)行sql語句的查詢時間方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

如下所示:

//計時開始
runtime();
 
//執(zhí)行查詢
mysql_query($sql);
 
//計時結束.
echo runtime(1);
 
//計時函數(shù) 
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);
}

對sql的執(zhí)行時間進行分析可以:

1,確定sql的書寫是否合理,高效

2,檢查字段、表的設計是否合理

方法1:在系統(tǒng)底層對sql操作類進行改寫,通常類的結構是

業(yè)務model ---》 db類 ---》 執(zhí)行sql

可以根據(jù)情況在某階段進行改寫,比如db類;通常會修改

public function execute($sql) {
  //code...

/*檢測sql執(zhí)行時間,超過執(zhí)行時間記錄到日志中*/
$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){    //修改時間范圍,單位:秒
 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...
}

引用:

phpmyadmin中的代碼,獲得query執(zhí)行時間如下:

 
// 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;

除了這種方式還可以使用mysql的profile。

這個更適合統(tǒng)計多條sql的執(zhí)行情況。

我見過好像是一個博客,訪問頁面之后會有一個提示大概說共查詢了幾次數(shù)據(jù)庫,用了多長時間查詢數(shù)據(jù),那么開啟mysql的profile就可以輕松實現(xiàn)了。

批注1:micortime函數(shù)

計算微秒的函數(shù)micortime(),可以返回當前UNIX時間戳和微秒數(shù)。返回浮點數(shù)單位為秒。不過函數(shù)僅在支持gettimeofday()系統(tǒng)調(diào)用的操作系統(tǒng)下可用??梢圆橄率謨栽敿毩私庀???赡芤l(fā)有些不明的錯誤,注意。

批注2:profile最多保存100條記錄,這個要怎么解決呢?

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.

這個最大就100條了,改不了。

引用2:PHP獲取毫秒級時間戳的方法

java里面可以通過gettime();獲取。如果是要與java寫的某些程序進行高精度的毫秒級的對接通信,則需要使用PHP輸出毫秒級的時間。為獲取更為精準的毫秒級時間戳可以使用下面的代碼:

<?php
function getMillisecond() {
list($t1, $t2) = explode(' ', microtime());
return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
}
echo getMillisecond();

運行結果:1.46647658229E+12

以上這篇PHP獲取MySQL執(zhí)行sql語句的查詢時間方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論