
EXPLAIN ANALYZE
SELECT
  t_s_secyear.customer_id, t_s_secyear.customer_first_name,
  t_s_secyear.customer_last_name, t_s_secyear.customer_email_address
FROM year_total t_s_firstyear
     ,year_total t_s_secyear
     ,year_total t_c_firstyear
     ,year_total t_c_secyear
     ,year_total t_w_firstyear
     ,year_total t_w_secyear
WHERE t_s_secyear.customer_id = t_s_firstyear.customer_id
   and t_s_firstyear.customer_id = t_c_secyear.customer_id
   and t_s_firstyear.customer_id = t_c_firstyear.customer_id
   and t_s_firstyear.customer_id = t_w_firstyear.customer_id
   and t_s_firstyear.customer_id = t_w_secyear.customer_id
   and t_s_firstyear.sale_type = 's'
   and t_c_firstyear.sale_type = 'c'
   and t_w_firstyear.sale_type = 'w'
   and t_s_secyear.sale_type = 's'
   and t_c_secyear.sale_type = 'c'
   and t_w_secyear.sale_type = 'w'
   and t_s_firstyear.dyear =  2001
   and t_s_secyear.dyear = 2001+1
   and t_c_firstyear.dyear =  2001
   and t_c_secyear.dyear =  2001+1
   and t_w_firstyear.dyear = 2001
   and t_w_secyear.dyear = 2001+1
   and t_s_firstyear.year_total > 0
   and t_c_firstyear.year_total > 0
   and t_w_firstyear.year_total > 0
   and case when t_c_firstyear.year_total > 0 then t_c_secyear.year_total / t_c_firstyear.year_total else null end
           > case when t_s_firstyear.year_total > 0 then t_s_secyear.year_total / t_s_firstyear.year_total else null end
   and case when t_c_firstyear.year_total > 0 then t_c_secyear.year_total / t_c_firstyear.year_total else null end
           > case when t_w_firstyear.year_total > 0 then t_w_secyear.year_total / t_w_firstyear.year_total else null end
ORDER BY t_s_secyear.customer_id
         ,t_s_secyear.customer_first_name
         ,t_s_secyear.customer_last_name
         ,t_s_secyear.customer_email_address
LIMIT 100;

/*
 Limit  (cost=69317.01..69317.24 rows=2 width=120) (actual time=213.612..216.029 rows=8 loops=1)
   ->  Gather Merge  (cost=69317.01..69317.24 rows=2 width=120) (actual time=213.610..216.026 rows=8 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         ->  Sort  (cost=68316.98..68316.99 rows=1 width=120) (actual time=196.233..196.239 rows=3 loops=3)
               Sort Key: t_s_secyear.customer_id, t_s_secyear.customer_first_name, t_s_secyear.customer_last_name, t_s_secyear.customer_email_address
               Sort Method: quicksort  Memory: 25kB
               Worker 0:  Sort Method: quicksort  Memory: 25kB
               Worker 1:  Sort Method: quicksort  Memory: 25kB
               ->  Parallel Hash Join  (cost=57122.32..68316.97 rows=1 width=120) (actual time=173.516..196.151 rows=3 loops=3)
                     Hash Cond: (t_s_secyear.customer_id = t_s_firstyear.customer_id)
                     Join Filter: (CASE WHEN (t_c_firstyear.year_total > '0'::numeric) THEN (t_c_secyear.year_total / t_c_firstyear.year_total) ELSE NULL::numeric END > CASE WHEN (t_s_firstyear.year_total > '0'::numeric) THEN (t_s_secyear.year_total / t_
s_firstyear.year_total) ELSE NULL::numeric END)
                     Rows Removed by Join Filter: 1
                     ->  Parallel Seq Scan on year_total t_s_secyear  (cost=0.00..11135.30 rows=15817 width=128) (actual time=0.016..24.628 rows=12725 loops=3)
                           Filter: ((sale_type = 's'::text) AND (dyear = 2002))
                           Rows Removed by Filter: 115344
                     ->  Parallel Hash  (cost=57122.20..57122.20 rows=9 width=109) (actual time=169.815..169.819 rows=9 loops=3)
                           Buckets: 1024  Batches: 1  Memory Usage: 104kB
                           ->  Parallel Hash Join  (cost=45527.58..57122.20 rows=9 width=109) (actual time=132.662..169.765 rows=9 loops=3)
                                 Hash Cond: (t_s_firstyear.customer_id = t_c_secyear.customer_id)
                                 ->  Parallel Seq Scan on year_total t_s_firstyear  (cost=0.00..11535.52 rows=15751 width=25) (actual time=0.013..38.041 rows=12641 loops=3)
                                       Filter: ((year_total > '0'::numeric) AND (sale_type = 's'::text) AND (dyear = 2001))
                                       Rows Removed by Filter: 115428
                                 ->  Parallel Hash  (cost=45527.32..45527.32 rows=21 width=84) (actual time=130.180..130.184 rows=18 loops=3)
                                       Buckets: 1024  Batches: 1  Memory Usage: 104kB
                                       ->  Parallel Hash Join  (cost=34346.97..45527.32 rows=21 width=84) (actual time=117.629..130.160 rows=18 loops=3)
                                             Hash Cond: (t_c_secyear.customer_id = t_c_firstyear.customer_id)
                                             Join Filter: (CASE WHEN (t_c_firstyear.year_total > '0'::numeric) THEN (t_c_secyear.year_total / t_c_firstyear.year_total) ELSE NULL::numeric END > CASE WHEN (t_w_firstyear.year_total > '0'::numeric) THEN (t_w
_secyear.year_total / t_w_firstyear.year_total) ELSE NULL::numeric END)
                                             Rows Removed by Join Filter: 13
                                             ->  Parallel Seq Scan on year_total t_c_secyear  (cost=0.00..11135.30 rows=11852 width=25) (actual time=12.308..24.099 rows=9059 loops=3)
                                                   Filter: ((sale_type = 'c'::text) AND (dyear = 2002))
                                                   Rows Removed by Filter: 119010
                                             ->  Parallel Hash  (cost=34344.50..34344.50 rows=197 width=75) (actual time=104.896..104.899 rows=123 loops=3)
                                                   Buckets: 1024  Batches: 1  Memory Usage: 104kB
                                                   ->  Parallel Hash Join  (cost=22766.22..34344.50 rows=197 width=75) (actual time=84.766..104.797 rows=123 loops=3)
                                                         Hash Cond: (t_c_firstyear.customer_id = t_w_firstyear.customer_id)
                                                         ->  Parallel Seq Scan on year_total t_c_firstyear  (cost=0.00..11535.52 rows=11186 width=25) (actual time=19.056..37.938 rows=8771 loops=3)
                                                               Filter: ((year_total > '0'::numeric) AND (sale_type = 'c'::text) AND (dyear = 2001))
                                                               Rows Removed by Filter: 119298
                                                         ->  Parallel Hash  (cost=22758.18..22758.18 rows=643 width=50) (actual time=65.584..65.586 rows=435 loops=3)
                                                               Buckets: 2048  Batches: 1  Memory Usage: 208kB
                                                               ->  Parallel Hash Join  (cost=11595.79..22758.18 rows=643 width=50) (actual time=61.023..65.217 rows=435 loops=3)
                                                                     Hash Cond: (t_w_secyear.customer_id = t_w_firstyear.customer_id)
                                                                     ->  Parallel Seq Scan on year_total t_w_secyear  (cost=0.00..11135.30 rows=4882 width=25) (actual time=20.529..24.004 rows=3751 loops=3)
                                                                           Filter: ((sale_type = 'w'::text) AND (dyear = 2002))
                                                                           Rows Removed by Filter: 124319
                                                                     ->  Parallel Hash  (cost=11535.52..11535.52 rows=4822 width=25) (actual time=40.416..40.416 rows=3775 loops=3)
                                                                           Buckets: 16384  Batches: 1  Memory Usage: 832kB
                                                                           ->  Parallel Seq Scan on year_total t_w_firstyear  (cost=0.00..11535.52 rows=4822 width=25) (actual time=33.524..39.425 rows=3775 loops=3)
                                                                                 Filter: ((year_total > '0'::numeric) AND (sale_type = 'w'::text) AND (dyear = 2001))
                                                                                 Rows Removed by Filter: 124295
 Planning Time: 13.150 ms
 Execution Time: 216.206 ms
 */

/*
-- NO stats:
Limit  (cost=69317.09..69317.33 rows=2 width=120) (actual time=204.058..206.367 rows=8 loops=1)
   ->  Gather Merge  (cost=69317.09..69317.33 rows=2 width=120) (actual time=204.056..206.363 rows=8 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         ->  Sort  (cost=68317.07..68317.07 rows=1 width=120) (actual time=195.113..195.119 rows=3 loops=3)
               Sort Key: t_s_secyear.customer_id, t_s_secyear.customer_first_name, t_s_secyear.customer_last_name, t_s_secyear.customer_email_address
               Sort Method: quicksort  Memory: 25kB
               Worker 0:  Sort Method: quicksort  Memory: 25kB
               Worker 1:  Sort Method: quicksort  Memory: 25kB
               ->  Parallel Hash Join  (cost=57121.58..68317.06 rows=1 width=120) (actual time=173.245..195.016 rows=3 loops=3)
                     Hash Cond: (t_s_secyear.customer_id = t_s_firstyear.customer_id)
                     Join Filter: (CASE WHEN (t_c_firstyear.year_total > '0'::numeric) THEN (t_c_secyear.year_total / t_c_firstyear.year_total) ELSE NULL::numeric END > CASE WHEN (t_s_firstyear.year_total > '0'::numeric) THEN (t_s_secyear.year_total / t_
s_firstyear.year_total) ELSE NULL::numeric END)
                     Rows Removed by Join Filter: 1
                     ->  Parallel Seq Scan on year_total t_s_secyear  (cost=0.00..11135.30 rows=16035 width=128) (actual time=0.020..24.165 rows=12725 loops=3)
                           Filter: ((sale_type = 's'::text) AND (dyear = 2002))
                           Rows Removed by Filter: 115344
                     ->  Parallel Hash  (cost=57121.47..57121.47 rows=9 width=109) (actual time=169.205..169.210 rows=9 loops=3)
                           Buckets: 1024  Batches: 1  Memory Usage: 104kB
                           ->  Parallel Hash Join  (cost=45527.24..57121.47 rows=9 width=109) (actual time=131.289..169.125 rows=9 loops=3)
                                 Hash Cond: (t_s_firstyear.customer_id = t_c_secyear.customer_id)
                                 ->  Parallel Seq Scan on year_total t_s_firstyear  (cost=0.00..11535.52 rows=15645 width=25) (actual time=0.012..37.873 rows=12641 loops=3)
                                       Filter: ((year_total > '0'::numeric) AND (sale_type = 's'::text) AND (dyear = 2001))
                                       Rows Removed by Filter: 115428
                                 ->  Parallel Hash  (cost=45526.98..45526.98 rows=21 width=84) (actual time=129.705..129.709 rows=18 loops=3)
                                       Buckets: 1024  Batches: 1  Memory Usage: 104kB
                                       ->  Parallel Hash Join  (cost=34347.66..45526.98 rows=21 width=84) (actual time=117.340..129.684 rows=18 loops=3)
                                             Hash Cond: (t_c_secyear.customer_id = t_c_firstyear.customer_id)
                                             Join Filter: (CASE WHEN (t_c_firstyear.year_total > '0'::numeric) THEN (t_c_secyear.year_total / t_c_firstyear.year_total) ELSE NULL::numeric END > CASE WHEN (t_w_firstyear.year_total > '0'::numeric) THEN (t_w
_secyear.year_total / t_w_firstyear.year_total) ELSE NULL::numeric END)
                                             Rows Removed by Join Filter: 13
                                             ->  Parallel Seq Scan on year_total t_c_secyear  (cost=0.00..11135.30 rows=11584 width=25) (actual time=12.331..23.934 rows=9059 loops=3)
                                                   Filter: ((sale_type = 'c'::text) AND (dyear = 2002))
                                                   Rows Removed by Filter: 119010
                                             ->  Parallel Hash  (cost=34345.16..34345.16 rows=200 width=75) (actual time=104.590..104.594 rows=123 loops=3)
                                                   Buckets: 1024  Batches: 1  Memory Usage: 104kB
                                                   ->  Parallel Hash Join  (cost=22766.43..34345.16 rows=200 width=75) (actual time=84.767..104.493 rows=123 loops=3)
                                                         Hash Cond: (t_c_firstyear.customer_id = t_w_firstyear.customer_id)
                                                         ->  Parallel Seq Scan on year_total t_c_firstyear  (cost=0.00..11535.52 rows=11302 width=25) (actual time=19.111..37.700 rows=8771 loops=3)
                                                               Filter: ((year_total > '0'::numeric) AND (sale_type = 'c'::text) AND (dyear = 2001))
                                                               Rows Removed by Filter: 119298
                                                         ->  Parallel Hash  (cost=22758.33..22758.33 rows=648 width=50) (actual time=65.501..65.503 rows=435 loops=3)
                                                               Buckets: 2048  Batches: 1  Memory Usage: 208kB
                                                               ->  Parallel Hash Join  (cost=11595.67..22758.33 rows=648 width=50) (actual time=60.796..64.982 rows=435 loops=3)
                                                                     Hash Cond: (t_w_secyear.customer_id = t_w_firstyear.customer_id)
                                                                     ->  Parallel Seq Scan on year_total t_w_secyear  (cost=0.00..11135.30 rows=4932 width=25) (actual time=20.225..23.680 rows=3751 loops=3)
                                                                           Filter: ((sale_type = 'w'::text) AND (dyear = 2002))
                                                                           Rows Removed by Filter: 124319
                                                                     ->  Parallel Hash  (cost=11535.52..11535.52 rows=4812 width=25) (actual time=40.506..40.507 rows=3775 loops=3)
                                                                           Buckets: 16384  Batches: 1  Memory Usage: 864kB
                                                                           ->  Parallel Seq Scan on year_total t_w_firstyear  (cost=0.00..11535.52 rows=4812 width=25) (actual time=33.413..39.510 rows=3775 loops=3)
                                                                                 Filter: ((year_total > '0'::numeric) AND (sale_type = 'w'::text) AND (dyear = 2001))
                                                                                 Rows Removed by Filter: 124295
 Planning Time: 12.515 ms
 Execution Time: 206.519 ms
*/
