• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    PHP获取MySQL执行sql语句的查询时间方法

    如下所示:

    //计时开始
    runtime();
     
    //执行查询
    mysql_query($sql);
     
    //计时结束.
    echo runtime(1);
     
    //计时函数 
    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的执行时间进行分析可以:

    1,确定sql的书写是否合理,高效

    2,检查字段、表的设计是否合理

    方法1:在系统底层对sql操作类进行改写,通常类的结构是

    业务model ---》 db类 ---》 执行sql

    可以根据情况在某阶段进行改写,比如db类;通常会修改

    public function execute($sql) {
      //code...
    
    /*检测sql执行时间,超过执行时间记录到日志中*/
    $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执行时间如下:

     
    // garvin: Measure query time.
    // TODO-Item http://sourceforge.net/tracker/index.php?func=detailaid=571934group_id=23067atid=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。

    这个更适合统计多条sql的执行情况。

    我见过好像是一个博客,访问页面之后会有一个提示大概说共查询了几次数据库,用了多长时间查询数据,那么开启mysql的profile就可以轻松实现了。

    批注1:micortime函数

    计算微秒的函数micortime(),可以返回当前UNIX时间戳和微秒数。返回浮点数单位为秒。不过函数仅在支持gettimeofday()系统调用的操作系统下可用。可以查下手册详细了解下。可能引发有些不明的错误,注意。

    批注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执行sql语句的查询时间方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

    您可能感兴趣的文章:
    • MySQL执行update语句和原数据相同会再次执行吗
    • 一条SQL语句在MySQL中是如何执行的
    上一篇:php ajax数据传输和响应方法
    下一篇:php实现将数据做成json的格式给前端使用
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    PHP获取MySQL执行sql语句的查询时间方法 PHP,获取,MySQL,执行,sql,语句,