From: William Leite Araújo <william.bh@gmail.com>
To: José Carlos <jc4520@gmail.com>
Cc: pgsql-sql@postgresql.org
Subject: Postgresql quey planner
Date: Thu, 5 Oct 2006 16:02:37 -0300
Message-ID: <bc63ad820610051202n45dd8adcua57d7228e53218da@mail.gmail.com> (raw)
Estou confuso com o funcionamento do "query planner" do postgresql.
Tenho 2 queries conceitualmente idênticas:
I'm confused about the planner functionality. I'd 2 queries
contextualy indentical:
SELECT p.pos_id, count(aut_id) as pesados
FROM posto p LEFT OUTER JOIN pesagem e USING(pos_id)
WHERE e.pos_id IS NULL OR (pes_dat_tstam BETWEEN '2006-03-01' AND
'2006-03-31')
GROUP BY p.pos_id ORDER BY pos_id
SELECT p.pos_id, count(pes_id) as autuados
FROM posto p LEFT OUTER JOIN autuacao a USING(pos_id)
WHERE a.pos_id IS NULL OR (aut_dat_tstam BETWEEN '2006-03-01' AND
'2006-03-31')
GROUP BY p.pos_id ORDER BY pos_id
A segunda faz realmente o que eu desejo. Mostra todos os postos,
inclusive os que não apresentam resultado. Já a primeira, alguns postos
simplesmente não aparecem. A diferença entre as tabelas é o número de
registros. A tabela de "pesagem" possui muito mais registros que a de
"autuacao". Avaliando o planner, ví que realmente trada de forma muito
diferente as consultas, mas não entendi porque o resultado e afetado. Não
deveria. A saida do planner é :
The second does really what I desire. Show all records in table
"posto", including that's without count in table "autuados". But the first,
doesn't. Some elements on table "Posto" are ommited. Seeing the planner
output, I couldn't understand why he uses "GroupAggregate" on the first but
not on the second query. The relevant difference on tables is the number of
records. "pesagem" has about 22000 but "autuacao" only 100. Is my queries
wrongs?
'GroupAggregate (cost=0.00..1027.66 rows=10 width=8) (actual time=
0.318..30.741 rows=6 loops=1)'
' -> Merge Left Join (cost=0.00..1027.43 rows=21 width=8) (actual time=
0.289..30.651 rows=54 loops=1)'
' Merge Cond: ("outer".pos_id = "inner".pos_id)'
' Filter: (("inner".pos_id IS NULL) OR (("inner".pes_dat_tstam >=
'2006-03-01'::date) AND ("inner".pes_dat_tstam <= '2006-03-31'::date)))'
' -> Index Scan using prk_posto on posto p (cost=0.00..5.35 rows=10
width=4) (actual time=0.064..0.081 rows=10 loops=1)'
' -> Index Scan using fki_frk_pos_id on pesagem a (cost=
0.00..801.48 rows=12604 width=12) (actual time=0.059..16.331 rows=12604
loops=1)'
'Total runtime: 31.035 ms'
'Sort (cost=8.38..8.41 rows=10 width=8) (actual time=0.557..0.564 rows=10
loops=1)'
' Sort Key: p.pos_id'
' -> HashAggregate (cost=8.09..8.22 rows=10 width=8) (actual time=
0.520..0.532 rows=10 loops=1)'
' -> Merge Right Join (cost=1.27..7.85 rows=49 width=8) (actual
time=0.062..0.443 rows=58 loops=1)'
' Merge Cond: ("outer".pos_id = "inner".pos_id)'
' Filter: (("outer".pos_id IS NULL) OR (("outer".aut_dat_tstam
>= '2006-03-01'::date) AND ("outer".aut_dat_tstam <= '2006-03-31'::date)))'
' -> Index Scan using idx_autuacao_pos_id on autuacao a
(cost=0.00..4.85 rows=77 width=12) (actual time=0.007..0.103 rows=77
loops=1)'
' -> Sort (cost=1.27..1.29 rows=10 width=4) (actual time=
0.049..0.107 rows=86 loops=1)'
' Sort Key: p.pos_id'
' -> Seq Scan on posto p (cost=0.00..1.10 rows=10
width=4) (actual time=0.005..0.016 rows=10 loops=1)'
'Total runtime: 0.659 ms'
Não consigo entender porquê o primeiro usa no final "GroupAggregate"
mas o segundo não. É erro?
Atenciosamente,
--
William Leite Araújo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: william.bh@gmail.com, jc4520@gmail.com
Subject: Re: Postgresql quey planner
In-Reply-To: <bc63ad820610051202n45dd8adcua57d7228e53218da@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox