gpt4 book ai didi

Postgresql 比较 2 个优化查询

转载 作者:行者123 更新时间:2023-11-29 11:37:15 25 4
gpt4 key购买 nike

我刚刚创建了几个查询,它们带来相同的数据,但格式不同。第一个使用子查询,第二个使用自连接策略。检查文档,我发现了 ANALYZE 和 EXPLAIN 命令,现在我试图了解哪个查询更好。这是每个查询的 EXPLAIN ANALYZE 的结果。希望有人能给我一些关于结果的解释,如果可能的话给我一些引用点,在哪里可以找到更多信息,谢谢你。

 EXPLAIN ANALYZE
SELECT historicoestatusrequisicion_id, requisicion_id, estatusrequisicion_id,
comentario, fecha_estatus, usuario_id
FROM historicoestatusrequisicion
WHERE requisicion_id IN
(
SELECT requisicion_id FROM historicoestatusrequisicion
WHERE usuario_id = 27 AND estatusrequisicion_id = 1
)
ORDER BY requisicion_id, estatusrequisicion_id;

这是结果

"Sort  (cost=240.15..242.42 rows=906 width=58) (actual time=72.470..80.575 rows=3066 loops=1)"
" Sort Key: public.historicoestatusrequisicion.requisicion_id, public.historicoestatusrequisicion.estatusrequisicion_id"
" Sort Method: quicksort Memory: 436kB"
" -> Hash Join (cost=96.44..195.65 rows=906 width=58) (actual time=16.198..46.765 rows=3066 loops=1)"
" Hash Cond: (public.historicoestatusrequisicion.requisicion_id = public.historicoestatusrequisicion.requisicion_id)"
" -> Seq Scan on historicoestatusrequisicion (cost=0.00..78.66 rows=3066 width=58) (actual time=0.018..8.616 rows=3066 loops=1)"
" -> Hash (cost=95.45..95.45 rows=79 width=7) (actual time=16.132..16.132 rows=904 loops=1)"
" -> HashAggregate (cost=94.66..95.45 rows=79 width=7) (actual time=10.475..13.109 rows=904 loops=1)"
" -> Seq Scan on historicoestatusrequisicion (cost=0.00..93.99 rows=267 width=7) (actual time=1.309..5.329 rows=904 loops=1)"
" Filter: ((usuario_id = 27) AND (estatusrequisicion_id = 1))"
"Total runtime: 88.682 ms"

第二次查询

  EXPLAIN ANALYZE
SELECT hist1.historicoestatusrequisicion_id, hist1.requisicion_id, hist1.estatusrequisicion_id, hist1.comentario, hist1.fecha_estatus, hist1.usuario_id
FROM historicoestatusrequisicion hist1
JOIN historicoestatusrequisicion hist2 ON hist2.requisicion_id = hist1.requisicion_id
WHERE hist2.usuario_id = 27 AND hist2.estatusrequisicion_id = 1
ORDER BY hist1.requisicion_id, hist1.estatusrequisicion_id;

这是结果

"Sort  (cost=248.71..250.97 rows=906 width=58) (actual time=34.833..40.601 rows=3066 loops=1)"
" Sort Key: hist1.requisicion_id, hist1.estatusrequisicion_id"
" Sort Method: quicksort Memory: 436kB"
" -> Hash Join (cost=97.33..204.21 rows=906 width=58) (actual time=4.320..23.515 rows=3066 loops=1)"
" Hash Cond: (hist1.requisicion_id = hist2.requisicion_id)"
" -> Seq Scan on historicoestatusrequisicion hist1 (cost=0.00..78.66 rows=3066 width=58) (actual time=0.010..5.886 rows=3066 loops=1)"
" -> Hash (cost=93.99..93.99 rows=267 width=7) (actual time=4.289..4.289 rows=904 loops=1)"
" -> Seq Scan on historicoestatusrequisicion hist2 (cost=0.00..93.99 rows=267 width=7) (actual time=0.425..2.316 rows=904 loops=1)"
" Filter: ((usuario_id = 27) AND (estatusrequisicion_id = 1))"
"Total runtime: 46.387 ms"

最佳答案

分析器会告诉您实际的执行路径是什么。对于第二个查询

它根据 estatusrequisicion ID 对所有行进行排序,它使用快速排序来实现这一点。花费的时间是 34 毫秒。

接下来它在 hist1.requisicion_id = hist2.requisicion_id 上加入该结果集。由于这些是关键,因此能够进行哈希查找。
哈希是主键/外键的存储方式。该字段被计算成一个散列
查找速度为 1。

接下来,它使用另一个主键/外键在 historiestatusrequisicion 上加入该结果集。

成本以计划者的 cost parameters 确定的任意单位衡量。 ,actual time就是(actual taken)花了多长时间

decent entry level explanation of execution plans

关于Postgresql 比较 2 个优化查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3865684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com