pg 性能分析_pg4实战怎么样

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

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;

代码100分

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/7665.html

(0)
上一篇 2023-03-16
下一篇 2023-03-16

相关推荐

  • Python编程技巧:如何有效结束循环

    Python编程技巧:如何有效结束循环codebreak/code语句用于跳出循环,可以在需要结束循环时使用。在循环体内使用codebreak/code语句后,循环将立即结束。下面是codebreak/code语句的一个简单示例:

    2023-12-08
    122
  • mysqldump 备份恢复_sql2003数据库备份恢复

    mysqldump 备份恢复_sql2003数据库备份恢复场景一 昨天凌晨2点做过一次完全备份,白天正常使用,该下班的时候,好巧啊!硬盘坏了。不过幸运的是做过备份并且二进制日志和数据库分开存放 1、建立数据库并开启二进制日志 建立用于存放二进制日志的文件夹…

    2023-03-03
    136
  • MySQL8.0-练习制作大学生手游情况调查系统1「终于解决」

    MySQL8.0-练习制作大学生手游情况调查系统1「终于解决」步骤: 1、以管理员身份运行dos窗口,开启MySQL服务,指令:net start MySQL80 2、以管理员身份运行MySQL 3、创建一个数据库,指令:create database data

    2023-04-23
    139
  • 解决动态库的符号冲突的方法_动态库加载失败

    解决动态库的符号冲突的方法_动态库加载失败一次debug遇到的疑惑 某天发现一个程序有点问题。祭上print大法,在关键的 lib_func() 函数里添加 print 调试信息,重新编译运行。 期望 print 出的信息一点都没有,但是程序

    2023-04-16
    151
  • 03 SQL2005数据库备份和还原

    03 SQL2005数据库备份和还原1、数据库备份的意义硬件故障或误操作2、数据库的恢复模式(数据库属性——选项——恢复模式)完整:可恢复到数据库故障时间点或指定时间点大容量日志:数据库日志不记录对数据修改的时间,效率高,通过日志只能…

    2023-04-05
    178
  • mysql 数据长度最大限制问题

    mysql 数据长度最大限制问题my.ini文件中没有设置通信缓冲区的最大长度,默认为1M,故而出现以上错误,在mysql安装文件夹下找到my.ini文件(linux是my.cnf文件)。打开后, 在[mysql]下面新增: de…

    2022-12-24
    160
  • 技术分享ppt_prevent

    技术分享ppt_prevent作者:Erik Frøseth 翻译:管长龙 原文:https://mysqlserverteam.com/hash-join-in-mysql-8/ 长期以来,在 MySQL 中执行 join 查…

    2022-12-20
    139
  • 基于pyspider的Python爬虫实现

    基于pyspider的Python爬虫实现近年来,互联网的爆发式增长使得大量的数据涌入到我们的视野中,这些数据包含丰富的信息,提供了巨大的商业和研究价值。然而,如何获取这些数据成为了我们面临的一个主要问题。Python作为一种高级编程语言,拥有强大的数据处理和网络爬取能力。pyspider是一款基于Python开发的强大网页爬虫框架,能够以简洁的方式实现高效的数据爬取,为我们提供了一种高效的解决方案。

    2024-05-16
    79

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注