ITPub博客

首页 > 数据库 > 数据库开发技术 > openGauss学习笔记-274 openGauss性能调优-实际调优案例03-建立合适的索引

openGauss学习笔记-274 openGauss性能调优-实际调优案例03-建立合适的索引

原创 数据库开发技术 作者:shuchaoyang 时间:2024-01-21 23:23:15 0 删除 编辑

openGauss学习笔记-274 openGauss性能调优-实际调优案例03-建立合适的索引274.1 现象描述274.2 优化分析

openGauss学习笔记-274 openGauss性能调优-实际调优案例03-建立合适的索引

274.1 现象描述

查询与销售部所有员工的信息:

SELECT staff_id,first_name,last_name,employment_id,state_name,city 
FROM staffs,sections,states,places
WHERE sections.section_name='Sales'
AND staffs.section_id = sections.section_id
AND sections.place_id = places.place_id
AND places.state_id = states.state_id
ORDER BY staff_id;

274.2 优化分析

在优化前,没有创建places.place_id和states.state_id索引,执行计划如下:

                                            QUERY PLAN
---------------------------------------------------------------------------------------------------
Sort  (cost=129.74..131.18 rows=576 width=136)
  Sort Key: staffs.staff_id
  ->  Hash Join  (cost=70.54..103.33 rows=576 width=136)
        Hash Cond: (states.state_id = places.state_id)
        -> Seq Scan on states  (cost=0.00..22.38 rows=1238 width=36)
        ->  Hash  (cost=69.38..69.38 rows=93 width=108)
              ->  Hash Join  (cost=42.41..69.38 rows=93 width=108)
                    Hash Cond: (places.place_id = sections.place_id)
                    -> Seq Scan on places  (cost=0.00..21.67 rows=1167 width=40)
                    ->  Hash  (cost=42.21..42.21 rows=16 width=76)
                          ->  Hash Join  (cost=24.66..42.21 rows=16 width=76)
                                Hash Cond: (staffs.section_id = sections.section_id)
                                -> Seq Scan on staffs  (cost=0.00..15.37 rows=537 width=76)
                                ->  Hash  (cost=24.59..24.59 rows=6 width=8)
                                      -> Seq Scan on sections  (cost=0.00..24.59 rows=6 width=8)
                                            Filter: (section_name = 'Sales'::text)
(16 rows)

建议在places.place_id和states.state_id列上建立2个索引,执行计划如下:

                                                QUERY PLAN
-----------------------------------------------------------------------------------------------------------
Sort  (cost=119.76..121.20 rows=576 width=136)
  Sort Key: staffs.staff_id
  ->  Hash Join  (cost=70.14..93.35 rows=576 width=136)
        Hash Cond: (staffs.section_id = sections.section_id)
        -> Seq Scan on staffs  (cost=0.00..15.37 rows=537 width=76)
        ->  Hash  (cost=67.43..67.43 rows=217 width=68)
              -> Nested Loop  (cost=24.66..67.43 rows=217 width=68)
                    ->  Hash Join  (cost=24.66..51.06 rows=35 width=40)
                          Hash Cond: (places.place_id = sections.place_id)
                          -> Seq Scan on places  (cost=0.00..21.67 rows=1167 width=40)
                          ->  Hash  (cost=24.59..24.59 rows=6 width=8)
                                -> Seq Scan on sections  (cost=0.00..24.59 rows=6 width=8)
                                      Filter: (section_name = 'Sales'::text)
                    ->  Index Scan using states_state_id_idx on states  (cost=0.00..0.41 rows=6 width=36)
                          Index Cond: (state_id = places.state_id)
(15 rows)


👍 点赞,你的认可是我创作的动力!

⭐️ 收藏,你的青睐是我努力的方向!

✏️ 评论,你的意见是我进步的财富!


PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“在看”,加个“星标”,这样每次新文章推送才会第一时间出现在你的订阅列表里。点“在看”支持我吧!


原文作者:超哥的IT私房菜;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论
专注于IT系统集成、运维、架构二十年的IT职场老兵,拥有CCNA、OCA、OCP、PGCA、PGCE认证证书,公众号:超哥的IT私房菜,知识星球:超哥的IT私房菜,目前是多家IT厂商的咨询顾问。

注册时间:2021-08-27

  • 博文量
    552
  • 访问量
    303618