Postgresql 从联接表查询计划中选择最大值(id)

Postgresql 从联接表查询计划中选择最大值(id),postgresql,database-performance,query-performance,postgresql-10,Postgresql,Database Performance,Query Performance,Postgresql 10,我有一个视图req_res,它连接了两个表-请求和响应内部在requestId上连接。请求具有主键ID列 当我查询(查询1)时: 查询1计划: Aggregate (cost=2834115.70..2834115.71 rows=1 width=8) (actual time=154709.729..154709.730 rows=1 loops=1) Buffers: shared hit=467727 read=685320 dirtied=214, temp read=240773

我有一个视图req_res,它连接了两个表-请求和响应内部在requestId上连接。请求具有主键ID列

当我查询(查询1)时:

查询1计划:

Aggregate  (cost=2834115.70..2834115.71 rows=1 width=8) (actual time=154709.729..154709.730 rows=1 loops=1)
  Buffers: shared hit=467727 read=685320 dirtied=214, temp read=240773 written=239751
  ->  Hash Join  (cost=2493060.64..2831172.33 rows=1177346 width=8) (actual time=143800.101..154147.080 rows=1198706 loops=1)
        Hash Cond: (req.req_id = res.req_id)
        Buffers: shared hit=467727 read=685320 dirtied=214, temp read=240773 written=239751
        ->  Append  (cost=0.44..55619.59 rows=1177346 width=16) (actual time=0.957..2354.648 rows=1200001 loops=1)
              Buffers: shared hit=438960 read=32014
              ->  Index Scan using "5_5_req_pkey" on _hyper_2_5_chunk rs  (cost=0.44..19000.10 rows=399803 width=16) (actual time=0.956..546.231 rows=399999 loops=1)
                    Index Cond: ((id >= 49600001) AND (id <= 50800001))
                    Buffers: shared hit=178872 read=10742
              ->  Index Scan using "7_7_req_pkey" on _hyper_2_7_chunk rs_1  (cost=0.44..36619.50 rows=777543 width=16) (actual time=0.037..767.744 rows=800002 loops=1)
                    Index Cond: ((id >= 49600001) AND (id <= 50800001))
                    Buffers: shared hit=260088 read=21272
        ->  Hash  (cost=1367864.98..1367864.98 rows=68583298 width=8) (actual time=143681.850..143681.850 rows=68568554 loops=1)
              Buckets: 262144  Batches: 512  Memory Usage: 7278kB
              Buffers: shared hit=28764 read=653306 dirtied=214, temp written=233652
              ->  Append  (cost=0.00..1367864.98 rows=68583298 width=8) (actual time=0.311..99590.021 rows=68568554 loops=1)
                    Buffers: shared hit=28764 read=653306 dirtied=214
                    ->  Seq Scan on _hyper_3_2_chunk wt  (cost=0.00..493704.44 rows=24941244 width=8) (actual time=0.309..14484.420 rows=24950147 loops=1)
                          Buffers: shared hit=661 read=243631
                    ->  Seq Scan on _hyper_3_6_chunk wt_1  (cost=0.00..503935.04 rows=24978804 width=8) (actual time=0.334..14487.931 rows=24963020 loops=1)
                          Buffers: shared hit=168 read=253979
                    ->  Seq Scan on _hyper_3_8_chunk wt_2  (cost=0.00..370225.50 rows=18663250 width=8) (actual time=0.327..10837.291 rows=18655387 loops=1)
                          Buffers: shared hit=27935 read=155696 dirtied=214
Planning time: 3.986 ms
Execution time: 154709.859 ms
Aggregate(成本=2834115.70..2834115.71行=1宽度=8)(实际时间=154709.729..154709.730行=1圈=1)
缓冲区:共享命中=467727读取=685320污损=214,临时读取=240773写入=239751
->散列联接(成本=2493060.64..2831172.33行=1177346宽度=8)(实际时间=143800.101..154147.080行=1198706循环=1)
哈希条件:(req.req\u id=res.req\u id)
缓冲区:共享命中=467727读取=685320污损=214,临时读取=240773写入=239751
->追加(成本=0.44..55619.59行=1177346宽度=16)(实际时间=0.957..2354.648行=1200001循环=1)
缓冲区:共享命中=438960读取=32014
->使用“5_5_req_pkey”对超2_5_区块rs进行索引扫描(成本=0.44..19000.10行=399803宽度=16)(实际时间=0.956..546.231行=399999循环=1)
索引条件:((id>=49600001)和(使用“7_7_req_pkey”对超2_7_块rs_1进行id索引扫描)(成本=0.44..36619.50行=777543宽度=16)(实际时间=0.037..767.744行=800002循环=1)
索引条件:((id>=49600001)和(id散列(成本=1367864.98..1367864.98行=68583298宽度=8)(实际时间=143681.850..143681.850行=68568554循环=1)
存储桶:262144批:512内存使用率:7278kB
缓冲区:共享命中=28764读=653306脏=214,临时写入=233652
->追加(成本=0.00..1367864.98行=68583298宽度=8)(实际时间=0.311..99590.021行=68568554循环=1)
缓冲区:共享命中=28764读取=653306脏态=214
->在块体wt上的顺序扫描(成本=0.00..493704.44行=24941244宽度=8)(实际时间=0.309..14484.420行=24950147循环=1)
缓冲区:共享命中=661读取=243631
->在块体wt_1上的顺序扫描(成本=0.00..503935.04行=24978804宽度=8)(实际时间=0.334..14487.931行=24963020循环=1)
缓冲区:共享命中=168读取=253979
->在超3块块wt 2上的顺序扫描(成本=0.00..370225.50行=18663250宽度=8)(实际时间=0.327..10837.291行=18655387循环=1)
缓冲区:共享命中=27935读=155696脏=214
计划时间:3.986毫秒
执行时间:154709.859毫秒
查询2计划:

Finalize Aggregate  (cost=2634042.50..2634042.51 rows=1 width=8) (actual time=5525.626..5525.627 rows=1 loops=1)
  Buffers: shared hit=8764620 read=12779
  ->  Gather  (cost=2634042.29..2634042.50 rows=2 width=8) (actual time=5525.609..5525.705 rows=3 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=8764620 read=12779
        ->  Partial Aggregate  (cost=2633042.29..2633042.30 rows=1 width=8) (actual time=5515.507..5515.508 rows=1 loops=3)
              Buffers: shared hit=8764620 read=12779
              ->  Nested Loop  (cost=0.88..2632023.83 rows=407382 width=8) (actual time=5.383..5261.979 rows=332978 loops=3)
                    Buffers: shared hit=8764620 read=12779
                    ->  Append  (cost=0.44..40514.98 rows=407383 width=16) (actual time=0.035..924.498 rows=333334 loops=3)
                          Buffers: shared hit=446706
                          ->  Parallel Index Scan using "5_5_req_pkey" on _hyper_2_5_chunk rs  (cost=0.44..16667.91 rows=166585 width=16) (actual time=0.033..169.854 rows=133333 loops=3)
                                Index Cond: ((id >= 49600001) AND (id <= 50600001))
                                Buffers: shared hit=190175
                          ->  Parallel Index Scan using "7_7_req_pkey" on _hyper_2_7_chunk rs_1  (cost=0.44..23847.07 rows=240798 width=16) (actual time=0.039..336.091 rows=200001 loops=3)
                                Index Cond: ((id >= 49600001) AND (id <= 50600001))
                                Buffers: shared hit=256531
                    ->  Append  (cost=0.44..6.33 rows=3 width=8) (actual time=0.011..0.011 rows=1 loops=1000001)
                          Buffers: shared hit=8317914 read=12779
                          ->  Index Only Scan using "2_2_response_pkey" on _hyper_3_2_chunk wt  (cost=0.44..2.11 rows=1 width=8) (actual time=0.003..0.003 rows=0 loops=1000001)
                                Index Cond: (req_id = req.req_id)
                                Heap Fetches: 0
                                Buffers: shared hit=3000005
                          ->  Index Only Scan using "6_6_response_pkey" on _hyper_3_6_chunk wt_1  (cost=0.44..2.11 rows=1 width=8) (actual time=0.003..0.003 rows=1 loops=1000001)
                                Index Cond: (req_id = req.req_id)
                                Heap Fetches: 192906
                                Buffers: shared hit=3551440 read=7082
                          ->  Index Only Scan using "8_8_response_pkey" on _hyper_3_8_chunk wt_2  (cost=0.44..2.10 rows=1 width=8) (actual time=0.003..0.003 rows=1 loops=443006)
                                Index Cond: (req_id = req.req_id)
                                Heap Fetches: 162913
                                Buffers: shared hit=1766469 read=5697
Planning time: 0.839 ms
Execution time: 5525.814 ms
最终确定聚合(成本=2634042.50..2634042.51行=1宽度=8)(实际时间=5525.626..5525.627行=1循环=1)
缓冲区:共享命中=8764620读取=12779
->聚集(成本=2634042.29..2634042.50行=2宽度=8)(实际时间=5525.609..5525.705行=3圈=1)
计划人数:2人
劳工处推出:2
缓冲区:共享命中=8764620读取=12779
->部分聚合(成本=2633042.29..2633042.30行=1宽度=8)(实际时间=5515.507..5515.508行=1圈=3)
缓冲区:共享命中=8764620读取=12779
->嵌套循环(成本=0.88..2632023.83行=407382宽度=8)(实际时间=5.383..5261.979行=332978循环=3)
缓冲区:共享命中=8764620读取=12779
->追加(成本=0.44..40514.98行=407383宽度=16)(实际时间=0.035..924.498行=33333 4个循环=3)
缓冲区:共享命中=446706
->使用“5_5_req_pkey”对超2_5_区块rs进行并行索引扫描(成本=0.44..16667.91行=166585宽度=16)(实际时间=0.033..169.854行=13333循环=3)
索引条件:((id>=49600001)和(使用“7_7_req_pkey”对超2_7_块rs_1进行id并行索引扫描)(成本=0.44..23847.07行=240798宽度=16)(实际时间=0.039..336.091行=200001循环=3)
索引条件:((id>=49600001)和(id追加(成本=0.44..6.33行=3宽度=8)(实际时间=0.011..0.011行=1循环=1000001)
缓冲区:共享命中=8317914读取=12779
->仅使用“2_2_响应_pkey”对超3_2_块wt进行索引扫描(成本=0.44..2.11行=1宽度=8)(实际时间=0.003..0.003行=0循环=1000001)
索引条件:(请求id=请求请求id)
堆获取:0
缓冲区:共享命中率=300005
->仅使用“6_6_response_pkey”对超3_6_块wt_1进行索引扫描(成本=0.44..2.11行=1宽度=8)(实际时间=0.003..0.003行=1循环=1000001)
索引条件:(请求id=请求请求id)
堆取数:192906
缓冲区:共享命中=3551440读取=7082
->仅使用“8_8_response_pkey”对超3_8_块wt_2进行索引扫描(成本=0.44..2.10行=1宽度=8)(实际时间=0.003..0.003行=1循环=443006)
索引条件:(请求id=请求请求id)
堆取数:162913
缓冲区:共享命中=1766469读取=5697
计划时间:0.839毫秒
执行时间:5525.814毫秒

请回答您的问题,并添加使用
解释(分析、缓冲区、格式化文本)生成的完整内容。
(不仅仅是“简单”的解释!)作为,请确保防止计划的格式和缩进。粘贴文本,然后将
`
放在计划前一行和计划后一行。还请包括所有索引的完整
创建索引
语句。向我们显示视图的查询也会有所帮助。您的请求中有多少没有resPONSE?只有716个请求没有响应两个观察结果:将并行计划与顺序计划进行比较,此外,在第二种情况下,大多数i/o都已缓存。这些结果是可重复的吗?还是由运行查询计划2引起的
CREATE OR REPLACE VIEW public.req_res
AS SELECT req.id,
    res.req_id,
    res.body::character varying(500),
    res.time,
    res.duration,
    res.balance,
    res.header::character varying(100),
    res.created_at
   FROM response res
     JOIN request req ON req.req_id = res.req_id;
Aggregate  (cost=2834115.70..2834115.71 rows=1 width=8) (actual time=154709.729..154709.730 rows=1 loops=1)
  Buffers: shared hit=467727 read=685320 dirtied=214, temp read=240773 written=239751
  ->  Hash Join  (cost=2493060.64..2831172.33 rows=1177346 width=8) (actual time=143800.101..154147.080 rows=1198706 loops=1)
        Hash Cond: (req.req_id = res.req_id)
        Buffers: shared hit=467727 read=685320 dirtied=214, temp read=240773 written=239751
        ->  Append  (cost=0.44..55619.59 rows=1177346 width=16) (actual time=0.957..2354.648 rows=1200001 loops=1)
              Buffers: shared hit=438960 read=32014
              ->  Index Scan using "5_5_req_pkey" on _hyper_2_5_chunk rs  (cost=0.44..19000.10 rows=399803 width=16) (actual time=0.956..546.231 rows=399999 loops=1)
                    Index Cond: ((id >= 49600001) AND (id <= 50800001))
                    Buffers: shared hit=178872 read=10742
              ->  Index Scan using "7_7_req_pkey" on _hyper_2_7_chunk rs_1  (cost=0.44..36619.50 rows=777543 width=16) (actual time=0.037..767.744 rows=800002 loops=1)
                    Index Cond: ((id >= 49600001) AND (id <= 50800001))
                    Buffers: shared hit=260088 read=21272
        ->  Hash  (cost=1367864.98..1367864.98 rows=68583298 width=8) (actual time=143681.850..143681.850 rows=68568554 loops=1)
              Buckets: 262144  Batches: 512  Memory Usage: 7278kB
              Buffers: shared hit=28764 read=653306 dirtied=214, temp written=233652
              ->  Append  (cost=0.00..1367864.98 rows=68583298 width=8) (actual time=0.311..99590.021 rows=68568554 loops=1)
                    Buffers: shared hit=28764 read=653306 dirtied=214
                    ->  Seq Scan on _hyper_3_2_chunk wt  (cost=0.00..493704.44 rows=24941244 width=8) (actual time=0.309..14484.420 rows=24950147 loops=1)
                          Buffers: shared hit=661 read=243631
                    ->  Seq Scan on _hyper_3_6_chunk wt_1  (cost=0.00..503935.04 rows=24978804 width=8) (actual time=0.334..14487.931 rows=24963020 loops=1)
                          Buffers: shared hit=168 read=253979
                    ->  Seq Scan on _hyper_3_8_chunk wt_2  (cost=0.00..370225.50 rows=18663250 width=8) (actual time=0.327..10837.291 rows=18655387 loops=1)
                          Buffers: shared hit=27935 read=155696 dirtied=214
Planning time: 3.986 ms
Execution time: 154709.859 ms
Finalize Aggregate  (cost=2634042.50..2634042.51 rows=1 width=8) (actual time=5525.626..5525.627 rows=1 loops=1)
  Buffers: shared hit=8764620 read=12779
  ->  Gather  (cost=2634042.29..2634042.50 rows=2 width=8) (actual time=5525.609..5525.705 rows=3 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=8764620 read=12779
        ->  Partial Aggregate  (cost=2633042.29..2633042.30 rows=1 width=8) (actual time=5515.507..5515.508 rows=1 loops=3)
              Buffers: shared hit=8764620 read=12779
              ->  Nested Loop  (cost=0.88..2632023.83 rows=407382 width=8) (actual time=5.383..5261.979 rows=332978 loops=3)
                    Buffers: shared hit=8764620 read=12779
                    ->  Append  (cost=0.44..40514.98 rows=407383 width=16) (actual time=0.035..924.498 rows=333334 loops=3)
                          Buffers: shared hit=446706
                          ->  Parallel Index Scan using "5_5_req_pkey" on _hyper_2_5_chunk rs  (cost=0.44..16667.91 rows=166585 width=16) (actual time=0.033..169.854 rows=133333 loops=3)
                                Index Cond: ((id >= 49600001) AND (id <= 50600001))
                                Buffers: shared hit=190175
                          ->  Parallel Index Scan using "7_7_req_pkey" on _hyper_2_7_chunk rs_1  (cost=0.44..23847.07 rows=240798 width=16) (actual time=0.039..336.091 rows=200001 loops=3)
                                Index Cond: ((id >= 49600001) AND (id <= 50600001))
                                Buffers: shared hit=256531
                    ->  Append  (cost=0.44..6.33 rows=3 width=8) (actual time=0.011..0.011 rows=1 loops=1000001)
                          Buffers: shared hit=8317914 read=12779
                          ->  Index Only Scan using "2_2_response_pkey" on _hyper_3_2_chunk wt  (cost=0.44..2.11 rows=1 width=8) (actual time=0.003..0.003 rows=0 loops=1000001)
                                Index Cond: (req_id = req.req_id)
                                Heap Fetches: 0
                                Buffers: shared hit=3000005
                          ->  Index Only Scan using "6_6_response_pkey" on _hyper_3_6_chunk wt_1  (cost=0.44..2.11 rows=1 width=8) (actual time=0.003..0.003 rows=1 loops=1000001)
                                Index Cond: (req_id = req.req_id)
                                Heap Fetches: 192906
                                Buffers: shared hit=3551440 read=7082
                          ->  Index Only Scan using "8_8_response_pkey" on _hyper_3_8_chunk wt_2  (cost=0.44..2.10 rows=1 width=8) (actual time=0.003..0.003 rows=1 loops=443006)
                                Index Cond: (req_id = req.req_id)
                                Heap Fetches: 162913
                                Buffers: shared hit=1766469 read=5697
Planning time: 0.839 ms
Execution time: 5525.814 ms