广告
返回顶部
首页 > 资讯 > 数据库 >pg 性能分析
  • 187
分享到

pg 性能分析

pg性能分析 2015-06-27 15:06:32 187人浏览 才女
摘要

postgresql 库中出现性能问题,对于复杂的sql, 常用分析过程: 简化SQL,定位性能异常点: 简化输出。像下面语句,可以先把输出的子查询去掉。有时也可以使用count(*)代替输出。 逐个测试uNIOn

pg 性能分析

postgresql 库中出现性能问题,对于复杂的sql, 常用分析过程:

  1. 简化SQL,定位性能异常点:
  2. 简化输出。像下面语句,可以先把输出的子查询去掉。有时也可以使用count(*)代替输出。
  3. 逐个测试uNIOn(minus),with子句。基于这些语句的独立性,可以逐个测试,逐渐添加条件,找到异常点。
  4. 分析执行计划,查看表数据量,连接方式,统计信息情况,索引情况
  5. Explain  各部分的消耗,连接方式等,如果语句可以在接受时间内执行,可以使用explain(analyze, buffers, timing)
  6. Pg_stat_user_table可以查看什么时候做的vacuum和analyze,live tuple和dead tuple个数,还有增删改查的次数等。
  7. Pg_stats 可以查看值的分布情况
回到下面的SQL:
1. 先做简化,使用count(*)替换所有输出:
explain(analyze , buffers, timing) select count(*)
  from sms_task_content_info    a,
       tsk_type_tbl b,
       tsk_plan_info       c,
       sm_code_tbl      d,
       smu_info            e
where a.course_type = b.course_type
   and a.course_id = c.content_id
   and c.plan_maker = e.user_id
   and e.region_code = d.region_code
   and d.is_valid = "Y"
   and c.date_plan >= to_date("2016-12-01", "yyyy-mm-dd")
   and c.date_plan < to_date("2016-12-31", "yyyy-mm-dd") + 1
;
 
                                                                              QUERY PLAN                                                                              
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate  (cost=2947.16..2947.18 rows=1 width=0) (actual time=49.154..49.154 rows=1 loops=1)
   Buffers: shared hit=1602
   ->  Hash Join  (cost=657.32..2928.06 rows=7643 width=0) (actual time=13.259..48.521 rows=7440 loops=1)
         Hash Cond: ((c.content_id)::text = (a.course_id)::text)
         Buffers: shared hit=1602
         ->  Hash Join  (cost=459.24..2615.33 rows=7643 width=33) (actual time=10.020..42.532 rows=7440 loops=1)
               Hash Cond: ((c.plan_maker)::text = (e.user_id)::text)
               Buffers: shared hit=1491
               ->  Seq Scan on tsk_plan_info c  (cost=0.00..2022.34 rows=7643 width=45) (actual time=0.629..29.272 rows=7440 loops=1)
                     Filter: ((date_plan >= to_date("2016-12-01"::text, "yyyy-mm-dd"::text)) AND (date_plan < (to_date("2016-12-31"::text, "yyyy-mm-dd"::text) + 1)))
                     Rows Removed by Filter: 25003
                     Buffers: shared hit=1286
               ->  Hash  (cost=412.29..412.29 rows=3756 width=12) (actual time=9.377..9.377 rows=3756 loops=1)
                     Buckets: 1024  Batches: 1  Memory Usage: 164kB
                     Buffers: shared hit=205
                     ->  Hash Join  (cost=179.00..412.29 rows=3756 width=12) (actual time=3.754..7.788 rows=3756 loops=1)
                           Hash Cond: ((e.region_code)::text = (d.region_code)::text)
                           Buffers: shared hit=205
                           ->  Seq Scan on smu_info e  (cost=0.00..167.56 rows=3756 width=14) (actual time=0.006..1.228 rows=3756 loops=1)
                                 Buffers: shared hit=130
                           ->  Hash  (cost=127.00..127.00 rows=4160 width=6) (actual time=3.736..3.736 rows=4103 loops=1)
                                 Buckets: 1024  Batches: 1  Memory Usage: 156kB
                                 Buffers: shared hit=75
                                 ->  Seq Scan on sms_region_code_tbl d  (cost=0.00..127.00 rows=4160 width=6) (actual time=0.003..2.201 rows=4103 loops=1)
                                       Filter: ((is_valid)::text = "Y"::text)
                                       Rows Removed by Filter: 4
                                       Buffers: shared hit=75
         ->  Hash  (cost=171.94..171.94 rows=2092 width=33) (actual time=3.231..3.231 rows=2093 loops=1)
               Buckets: 1024  Batches: 1  Memory Usage: 133kB
               Buffers: shared hit=111
               ->  Hash Join  (cost=12.25..171.94 rows=2092 width=33) (actual time=0.021..2.231 rows=2093 loops=1)
                     Hash Cond: ((a.course_type)::text = (b.course_type)::text)
                     Buffers: shared hit=111
                     ->  Seq Scan on sms_task_content_info a  (cost=0.00..130.92 rows=2092 width=35) (actual time=0.004..0.818 rows=2093 loops=1)
                           Buffers: shared hit=110
                     ->  Hash  (cost=11.00..11.00 rows=100 width=20) (actual time=0.009..0.009 rows=6 loops=1)
                           Buckets: 1024  Batches: 1  Memory Usage: 1kB
                           Buffers: shared hit=1
                           ->  Seq Scan on tsk_type_tbl b  (cost=0.00..11.00 rows=100 width=20) (actual time=0.003..0.005 rows=6 loops=1)
                                 Buffers: shared hit=1
Planning time: 2.522 ms
Execution time: 49.270 ms
(42 rows)
 
Time: 71.990 ms
 
去掉子查询后,语句很快就输出了, 问题就在输出结果里的子查询,到最后输出7440行,就意味着那两个子查询都需要7440次。整体语句慢在这里。
 
 
select distinct d.description as "RANGE",
                b.description as "COURSE_CLASSIFICATION_DESC",
                to_char(c.date_make, "yyyy-mm-dd") as "DATE_MAKE",
                to_char(c.date_end, "yyyy-mm-dd") as "DATE_END",
                to_char(c.date_plan, "yyyy-mm-dd") as "DATE_PLAN",
                (select cast((case
                               when (select count(1)
                                       from sms_task_content_info a2,
                                            tsk_plan_info    b2,
                                            smu_info         s2
                                      where a2.course_id = b2.content_id
                                        and b2.plan_maker = s2.user_id
                                        and b2.plan_status != "2"
                                        and s2.region_code = e.region_code
                                        and a2.course_type = a.course_type
                                        and to_char(b2.date_make, "yyyy-mm-dd") =
                                            to_char(c.date_make, "yyyy-mm-dd")
                                        and to_char(b2.date_plan, "yyyy-mm-dd") =
                                            to_char(c.date_plan, "yyyy-mm-dd")
                                        and to_char(b2.date_end, "yyyy-mm-dd") =
                                            to_char(c.date_end, "yyyy-mm-dd")) != 0 then
                                (cast(100 AS numeric(5, 2)) *
                                (select count(1)
                                    from sms_task_content_info a1,
                                         tsk_plan_info    b1,
                                         smu_info         s1
                                   where a1.course_id = b1.content_id
                                     and b1.plan_maker = s1.user_id
                                     and b1.plan_status = "1"
                                     and s1.region_code = e.region_code
                                     and a1.course_type = a.course_type
                                     and to_char(b1.date_make, "yyyy-mm-dd") =
                                         to_char(c.date_make, "yyyy-mm-dd")
                                     and to_char(b1.date_plan, "yyyy-mm-dd") =
                                         to_char(c.date_plan, "yyyy-mm-dd")
                                     and to_char(b1.date_end, "yyyy-mm-dd") =
                                         to_char(c.date_end, "yyyy-mm-dd")) /
                                (select count(1)
                                    from sms_task_content_info a2,
                                         tsk_plan_info    b2,
                                         smu_info         s2
                                   where a2.course_id = b2.content_id
                                     and b2.plan_maker = s2.user_id
                                     and b2.plan_status != "2"
                                     and s2.region_code = e.region_code
                                     and a2.course_type = a.course_type
                                     and to_char(b2.date_make, "yyyy-mm-dd") =
                                         to_char(c.date_make, "yyyy-mm-dd")
                                     and to_char(b2.date_plan, "yyyy-mm-dd") =
                                         to_char(c.date_plan, "yyyy-mm-dd")
                                     and to_char(b2.date_end, "yyyy-mm-dd") =
                                         to_char(c.date_end, "yyyy-mm-dd")))
                               else
                                "0"
                             end) AS numeric(5, 2)) || "%"
                   from dual) as "FINISH_RATIO",
                d.region_code,
                b.course_type
  from sms_task_content_info    a,
       tsk_type_tbl b,
       tsk_plan_info       c,
       sm_code_tbl      d,
       smu_info            e
where a.course_type = b.course_type
   and a.course_id = c.content_id
   and c.plan_maker = e.user_id
   and e.region_code = d.region_code
   and d.is_valid = "Y"
  -- and e.region_code in()
  -- and a.course_type = "1"
   and c.date_plan >= to_date("2016-12-01", "yyyy-mm-dd")
   and c.date_plan < to_date("2016-12-31", "yyyy-mm-dd") + 1
order by d.region_code, b.course_type;

 

您可能感兴趣的文档:

--结束END--

本文标题: pg 性能分析

本文链接: https://www.lsjlt.com/news/6900.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • pg 性能分析
    postgresql 库中出现性能问题,对于复杂的sql, 常用分析过程: 简化SQL,定位性能异常点: 简化输出。像下面语句,可以先把输出的子查询去掉。有时也可以使用count(*)代替输出。 逐个测试union...
    99+
    2015-06-27
    pg 性能分析
  • PG wal 日志的物理存储分析
    原文链接:   http://www.postgres.cn/v2/news/viewone/1/385tdsourcetag=s_pcqq_aiomsg 我...
    99+
    2022-10-18
  • Mysql:SQL性能分析
    1 SQL执行频率 MySQL 客户端连接成功后,通过 show [session|global] status 命令可以提供服务器状态信息。通过如下指令,可以查看当前数据库的INSERT、UPDATE、DELETE、SELECT的访问频次...
    99+
    2023-09-25
    mysql sql 数据库
  • linux 性能分析
    查看性能顺序:[cpu] mpstat -P ALL 1 100  (sar -u,sar -p)[network] sar -n DEV[disk] sar -b,sar -d[mem] sar...
    99+
    2022-10-18
  • MySQL性能分析(Explain)
    更多知识,请移步我的小破站:http://hellofriend.top 1. 概述 使用EXPLAIN关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的。分析你的查询语句或是表结构的性能瓶颈...
    99+
    2021-11-05
    MySQL性能分析(Explain)
  • DB2 HADR性能分析
    在DB2数据库HADR监控中发现,每天有一段时间,有很多应用处于commit active状态,对应用性能有影响 猜测可能是两种原因造成 日志写的慢 网络通信慢 到底是哪个原...
    99+
    2022-10-18
  • pgbench性能测试分析
    本篇内容主要讲解“pgbench性能测试分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“pgbench性能测试分析”吧!--pgbench 测试&n...
    99+
    2022-10-18
  • vmstat 主机性能分析
    一、vmstat介绍及详解(此篇博客是从博客园博主随风迎处转载,博主的网址:http://www.cnblogs.com/beginner-boy/  ,再次感谢博主的细心积累)语法格式: vmstat [-V] [-n] [-S...
    99+
    2023-06-05
  • Python性能优化分析
    本篇内容介绍了“Python性能优化分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!python为什么性能差:当我们提到一门编程语言的效率...
    99+
    2023-06-17
  • WCF性能举例分析
    这篇文章主要介绍“WCF性能举例分析”,在日常操作中,相信很多人在WCF性能举例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”WCF性能举例分析”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!WCF(W...
    99+
    2023-06-17
  • MYSQL 性能分析器 EXPLAIN 用法实例分析
    本文实例讲述了MYSQL 性能分析器 EXPLAIN 用法。分享给大家供大家参考,具体如下: 使用方法: EXPLAIN SELECT * FROM user; 环境和数据准备 -- 查看 MySQ...
    99+
    2022-05-27
    MYSQL 性能分析器 EXPLAIN
  • Hibernate性能的示例分析
    这篇文章将为大家详细讲解有关Hibernate性能的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。Hibernate在解决性能问题方面做得非常好。有了它的缓存机制,使用第三方缓存和数据库连接池,就...
    99+
    2023-06-17
  • LevelDB性能分析和表现
    LevelDB是一种高性能的键值存储引擎,具有以下几个方面的表现和性能特点:1. 基于磁盘的持久化存储:LevelDB将数据存储在磁...
    99+
    2023-09-22
    LevelDB
  • PostgreSQL11 tpcb性能测试分析
    本篇内容介绍了“PostgreSQL11 tpcb性能测试分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成...
    99+
    2022-10-18
  • java性能分析jconsole详解
    目录jconsole简介jconsole远程前言: 本章节继续学习java性能优化的相关知识。重点学习什么是jconsole,以及如何使用?它能帮助我们做什么? jconsole简介...
    99+
    2022-11-13
  • PHP性能的宏观分析
    本篇内容主要讲解“PHP性能的宏观分析”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“PHP性能的宏观分析”吧!宏观层面,也就是对 PHP 语言本身的性能分析又分为三个方面:PHP 作为解释性语言...
    99+
    2023-06-17
  • Java日志性能实例分析
    这篇文章主要介绍“Java日志性能实例分析”,在日常操作中,相信很多人在Java日志性能实例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java日志性能实例分析”的疑惑有所帮助!接下来,请跟着小编一起来...
    99+
    2023-06-19
  • Go库性能分析工具pprof
    目录场景pprof生成 profile 文件CPU 性能分析内存性能分析分析 profile 文件 && 优化代码go tool pproftop 命令list 命令...
    99+
    2022-12-15
    Go pprof性能分析 Go pprof
  • 【Azure SQL】数据库性能分析
    前置条件 用户有查询数据统计权限 GRANT VIEW DATABASE STATE TO database_user; CPU性能问题 正在发生 查看前X个CPU消耗查询 (汇总) SELECT TOP 10 ...
    99+
    2016-02-11
    【Azure SQL】数据库性能分析
  • MySQL 如何分析查询性能
    查询优化、索引优化和表设计优化是环环相扣的。如果你有丰富的编写MySQL查询语句的经验,你就会知道如何设计表和索引来支持有效的查询。同样的,知晓表设计同样有助于了解表结构如何对查询语句产生影响。因此,即便表设计和索...
    99+
    2022-05-12
    MySQL 查询性能 MySQL 查询性能分析
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作