zoukankan      html  css  js  c++  java
  • 转载: PostgreSQL SQL的性能调试方法2--数据库log分析

    原文:http://blog.csdn.net/hantiannan/article/details/4513028

    1.log_min_duration_statement

      从log找出执行超过一定时间的 SQL。postgresql.conf配置文件 设置 log_min_duration_statement参数的 值。

    这个参数是设置执行最小多长时间的 SQL 输出 到log。

      例如输出执行超过 3秒的SQL:log_min_duration_statement = 3s
      这个参数设置为 -1是无效。 设置为 0是 输出所有的 SQL,但 这样会增加服务器负担,一般不要设置太低的 值。
       这样设置后输出的SQL例子如下:
       LOG:duration: 3016.724 ms statement: SELECT count(*)
          FROMpg_class
     
    2.contrib/auto_explain功能。Postgres8.4后增加的功能。
       默认这个功能不能使用的,需要在postgresql.conf配置文件中设置以下参数。
           shared_preload_libraries= 'auto_explain'
           custom_variable_classes= 'auto_explain'
           auto_explain.log_min_duration= 4s
        这样系统在执行的时候如果遇到超过4秒的SQL的话,会自动把执行计划输出到log。这样就直接看log就更加容易找到问题点。

       执行计划例子:

      LOG:  duration: 4016.724ms  plan:

        Aggregate  (cost=14.90..14.91rows=1 width=0)
         ->Hash Join  (cost=3.91..14.70rows=81 width=0)
             HashCond: (pg_class.oid = pg_index.indrelid)
              ->Seq Scan onpg_class  (cost=0.00..8.27rows=227 width=4)
              ->Hash  (cost=2.90..2.90 rows=81width=4)
                    ->Seq Scan onpg_index  (cost=0.00..2.90rows=81 width=4)
                         Filter:indisunique
        STATEMENT:  SELECTcount(*)
                  FROMpg_class, pg_index
                 WHEREoid = indrelid AND indisunique;
     
    3.log统计分析工具(PostgreSQL log analyzer)

        比较有名是pgFouine 。这个工具是自动分析指定的log,然后生成HTML报表。把SQLlog图像化后更加直观。

    可以统计分析最慢的SQL,调用最多的SQL,花费时间最多的SQL等等分类。这样我们就很容易找到速度慢的SQL。再加以改善。

        报表例子如下:

        

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    剑指 Offer 22. 链表中倒数第k个节点
    剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
    Leetcode1450. 在既定时间做作业的学生人数
    Leetcode1572. 矩阵对角线元素的和
    Leetcode 1480. 一维数组的动态和
    Idea连接数据库报错
    Java实现二叉树层次遍历并存入List的方法:从上往下,从左往右
    SpringCloud资源网站
    Java循环对list进行remove
    Java中字符串判空的正确打开方式
  • 原文地址:https://www.cnblogs.com/leeeee/p/7276624.html
Copyright © 2011-2022 走看看