• 记录sql执行时间毫秒级


    dui  对sql的执行时间进行分析可以:

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

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

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

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

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

     1 public function execute($sql)  {
     2   //code...
     3 
     4 /*检测sql执行时间,超过执行时间记录到日志中*/
     5 $start_time = array_sum(explode(' ', microtime()));
     6 
     7 $this->lastresult = mysql_query($sql,$this->link) or $this->displayerror($sql);
     8 
     9 $end_time = array_sum(explode(' ', microtime()));
    10 $differ = $end_time - $start_time;
    11 if($differ >0.001){                //修改时间范围,单位:秒
    12    putContent('sqlLOG', date('Y-m-d H:i:s', $start_time)."   "
    13       . date('Y-m-d H:i:s', $end_time)."   "
    14       .$differ. "   ".$sql."
    ");
    15 }
    16 
    17 
    18   //code...
    19 }

    引用:

    phpmyadmin中的代码,获得query执行时间如下:

    // 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,可以参考@php里sql执行时间的监控?
    这个更适合统计多条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

    打赏支持我写出更多好文章,谢谢!
    打赏作者
    +

    (^_^)打个赏喝个咖啡(^_^)

    微信支付
    支付宝支付
  • 相关阅读:
    解决软件升级过程中GAC发生的问题.
    Appupdater 组件的一个问题.
    == 和 != 有点靠不主,建议在进行比较的时候尽可能的使用Equals
    从资源文件中提取Icon对象到Image对象的小技巧。
    arcgis flex开发备忘
    IIS服务器的系统事件日志常见问题汇总(更新中)
    WebSphere6.1配置应用程序
    Android Preference使用
    Oracle无法删除当前已连接的用户
    SSH环境搭建
  • 原文地址:https://www.cnblogs.com/yangf2016/p/5602859.html
Copyright © 2020-2023  润新知