pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Number of rows returned by Sort node
3+ messages / 2 participants
[nested] [flat]

* Number of rows returned by Sort node
@ 2018-01-09 15:20  Vitaliy Garnashevich <vgarnashevich@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Vitaliy Garnashevich @ 2018-01-09 15:20 UTC (permalink / raw)
  To: pgsql-general

Hi,

How, according to EXPLAIN ANALYZE, the Sort node could return more rows 
as output (rows=767662), than it had for input from its child node 
(rows=135627)?

->  Merge Join  .... (actual time=1977.388..333626.072 rows=725757 loops=1)
       ->  Index Scan using .... (actual time=0.013..312144.441 
rows=49176765 loops=1)
       ->  Sort  .... (actual time=1977.363..2274.092 rows=767662 loops=1)
             ->  Hash Left Join  .... (actual time=97.123..1887.956 
rows=135627 loops=1)

(full plan attached, PostgreSQL 9.3)

Regards,
Vitaliy

Attachments:

plan.txt (text/plain, 5.4K) download QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- GroupAggregate (cost=6051876.28..6062663.17 rows=359563 width=65) (actual time=604213.422..614449.378 rows=86325 loops=1) -> Sort (cost=6051876.28..6052775.19 rows=359563 width=65) (actual time=604213.378..609416.622 rows=5563369 loops=1) Sort Key: cmdb_program_daily_usage.used_from, cmdb_program_daily_usage."user", cmdb_software_product_version.friendly_name, cmdb_software_product_version.id Sort Method: external merge Disk: 445928kB -> Hash Join (cost=197462.19..6018696.01 rows=359563 width=65) (actual time=4136.668..533171.915 rows=5563369 loops=1) Hash Cond: (cmdb_ci.id = cmdb_ci_computer.id) -> Nested Loop (cost=194838.34..6011562.23 rows=359563 width=73) (actual time=3909.509..527042.088 rows=5563369 loops=1) -> Nested Loop (cost=194837.91..5794445.19 rows=459672 width=65) (actual time=3909.321..467581.211 rows=5828546 loops=1) -> Hash Left Join (cost=194837.21..3206507.24 rows=343355 width=37) (actual time=3909.179..337915.666 rows=725757 loops=1) Hash Cond: (cmdb_program_instance.program = cmdb_program.id) Filter: ((NOT cmdb_program.ssi_exclude_from_usage) OR (cmdb_program.ssi_exclude_from_usage IS NULL)) -> Merge Join (cost=120277.29..3123792.65 rows=343355 width=45) (actual time=1977.388..333626.072 rows=725757 loops=1) Merge Cond: (cmdb_program_instance.spkg = cmdb_ci_spkg.id) -> Index Scan using idx_fk_2005379175958 on cmdb_program_instance (cost=0.57..5178525.28 rows=88519832 width=24) (actual time=0.013..312144.441 rows=49176765 loops=1) -> Sort (cost=120276.66..120493.07 rows=86563 width=37) (actual time=1977.363..2274.092 rows=767662 loops=1) Sort Key: cmdb_ci_spkg.id Sort Method: quicksort Memory: 16740kB -> Hash Left Join (cost=2024.59..113177.86 rows=86563 width=37) (actual time=97.123..1887.956 rows=135627 loops=1) Hash Cond: (cmdb_ci_spkg.software = cmdb_software_product_version.id) -> Bitmap Heap Scan on cmdb_ci_spkg (cost=2021.27..111984.31 rows=86563 width=16) (actual time=96.993..1780.596 rows=135627 loops=1) Recheck Cond: ((software IS NOT NULL) AND (software = 2)) Filter: ((NOT os) OR (os IS NULL)) -> Bitmap Index Scan on idx_fk_1998473864224 (cost=0.00..1999.63 rows=86957 width=0) (actual time=65.665..65.665 rows=136536 loops=1) Index Cond: ((software IS NOT NULL) AND (software = 2)) -> Hash (cost=3.30..3.30 rows=1 width=29) (actual time=0.088..0.088 rows=1 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 1kB -> Index Scan using cmdb_software_product_version_pkey on cmdb_software_product_version (cost=0.29..3.30 rows=1 width=29) (actual time=0.068..0.083 rows=1 loops=1) Index Cond: (id = 2) -> Hash (cost=59002.74..59002.74 rows=1244574 width=9) (actual time=1931.213..1931.213 rows=1230216 loops=1) Buckets: 131072 Batches: 1 Memory Usage: 49257kB -> Seq Scan on cmdb_program (cost=0.00..59002.74 rows=1244574 width=9) (actual time=0.026..1496.853 rows=1230216 loops=1) -> Index Scan using idx_2011674031584 on cmdb_program_daily_usage (cost=0.70..6.67 rows=87 width=44) (actual time=0.047..0.172 rows=8 loops=725757) Index Cond: ((program_instance = cmdb_program_instance.id) AND (usage_date >= '2017-07-13'::date) AND (usage_date <= '2018-01-09'::date)) -> Index Scan using cmdb_ci_pkey on cmdb_ci (cost=0.43..0.46 rows=1 width=8) (actual time=0.008..0.009 rows=1 loops=5828546) Index Cond: (id = cmdb_program_daily_usage.used_from) Filter: operational -> Hash (cost=1815.19..1815.19 rows=64693 width=8) (actual time=227.122..227.122 rows=64705 loops=1) Buckets: 8192 Batches: 1 Memory Usage: 2528kB -> Index Only Scan using cmdb_ci_computer_pkey on cmdb_ci_computer (cost=0.41..1815.19 rows=64693 width=8) (actual time=0.037..205.223 rows=64705 loops=1) Heap Fetches: 22752 Total runtime: 614596.450 ms (41 rows)
^ permalink raw reply [nested|flat] 3+ messages in thread

* Re: Number of rows returned by Sort node
@ 2018-01-09 15:23  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Vitaliy Garnashevich <vgarnashevich@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2018-01-09 15:23 UTC (permalink / raw)
  To: Vitaliy Garnashevich <vgarnashevich@gmail.com>; +Cc: pgsql-general

Vitaliy Garnashevich <vgarnashevich@gmail.com> writes:
> How, according to EXPLAIN ANALYZE, the Sort node could return more rows 
> as output (rows=767662), than it had for input from its child node 
> (rows=135627)?

Unsurprising when it's the inner input of a merge join --- that reflects
the merge join rescanning parts of the inner input.  It has to do that
whenever the outer input has duplicate keys.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Number of rows returned by Sort node
@ 2018-01-09 16:43  Vitaliy Garnashevich <vgarnashevich@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Vitaliy Garnashevich @ 2018-01-09 16:43 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-general

Thanks, Tom!

I've found some more information in PostgreSQL source code:

For example, consider the following relations:

         outer: (0 ^1 1 2 5 5 5 6 6 7)    current tuple: 1
         inner: (1 ^3 5 5 5 5 6)          current tuple: 3

...suppose that the executor has
just joined the first outer "5" with the last inner "5". The
next step is of course to join the second outer "5" with all
the inner "5's". This requires repositioning the inner "cursor"
to point at the first inner "5". This is done by "marking" the
first inner 5 so we can restore the "cursor" to it before joining
with the second outer 5. The access method interface provides
routines to mark and restore to a tuple.


Essential operation of the merge join algorithm is as follows:

Join {
     get initial outer and inner tuples INITIALIZE
     do forever {
         while (outer != inner) { SKIP_TEST
             if (outer < inner)
                 advance outer SKIPOUTER_ADVANCE
             else
                 advance inner SKIPINNER_ADVANCE
         }
         mark inner position SKIP_TEST
         do forever {
             while (outer == inner) {
                 join tuples JOINTUPLES
                 advance inner position NEXTINNER
             }
             advance outer position NEXTOUTER
             if (outer == mark) TESTOUTER
                 restore inner position to mark TESTOUTER
             else
                 break    // return to top of outer loop
         }
     }
}


So, even that the Sort node was returning unique values, the join 
algorithm still had to do a lot of mark/restore, which were reflected in 
EXPLAIN's row count. Anyway, that's clear now.

Regards,
Vitaliy

On 2018-01-09 17:23, Tom Lane wrote:
> Vitaliy Garnashevich <vgarnashevich@gmail.com> writes:
>> How, according to EXPLAIN ANALYZE, the Sort node could return more rows
>> as output (rows=767662), than it had for input from its child node
>> (rows=135627)?
> Unsurprising when it's the inner input of a merge join --- that reflects
> the merge join rescanning parts of the inner input.  It has to do that
> whenever the outer input has duplicate keys.
>
> 			regards, tom lane
>





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2018-01-09 16:43 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-01-09 15:20 Number of rows returned by Sort node Vitaliy Garnashevich <vgarnashevich@gmail.com>
2018-01-09 15:23 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-01-09 16:43   ` Vitaliy Garnashevich <vgarnashevich@gmail.com>

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