我刚刚创建了几个带来相同数据但又有不同的查询.第一个使用子查询,第二个使用自连接策略.检查文档,我找到了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对所有行进行了排序,它使用quicksort来实现这一目标.花费的时间是34毫秒.
它接下来在hist1.requisicion_id = hist2.requisicion_id上加入了该结果集.由于这些是关键的,它能够进行哈希查找.
哈希是主键/外键的存储方式.该字段计算为哈希
并且查找速度为1.
接下来,它使用另一个主/外键在historicestatusrequisicion上加入了结果集.
成本以计划者cost parameters确定的任意单位计量,实际时间是指花费的时间(实际花费)