Hi

In analyze of slow queries for my customer I found a relatively small query
with high plan time. Looks so it is related to DISTINCT clause

create table t1(
  t1_id int primary key,
  t1_c1 int,
  t1_c2 int,
  t1_c3 int,
  t1_c4 int,
  t1_c5 int,
  t1_c6 int,
  t1_c7 int,
  t1_c8 int,
  t1_c9 int,
  t1_c10 int);

create table t2(
  t2_id int primary key,
  t2_t1_id int,
  t2_c1 int,
  t2_c2 int,
  t2_c3 int,
  t2_c4 int,
  t2_c5 int,
  t2_c6 int,
  t2_c7 int,
  t2_c8 int,
  t2_c9 int,
  t2_c10 int);

create table t3(
  t3_id int primary key,
  t3_t2_id int,
  t3_c1 int,
  t3_c2 int,
  t3_c3 int,
  t3_c4 int,
  t3_c5 int,
  t3_c6 int,
  t3_c7 int,
  t3_c8 int,
  t3_c9 int,
  t3_c10 int);

create table t4(
  t4_id int primary key,
  t4_t3_id int,
  t4_c1 int,
  t4_c2 int,
  t4_c3 int,
  t4_c4 int,
  t4_c5 int,
  t4_c6 int,
  t4_c7 int,
  t4_c8 int,
  t4_c9 int,
  t4_c10 int);

create table t5(
  t5_id int primary key,
  t5_t4_id int,
  t5_c1 int,
  t5_c2 int,
  t5_c3 int,
  t5_c4 int,
  t5_c5 int,
  t5_c6 int,
  t5_c7 int,
  t5_c8 int,
  t5_c9 int,
  t5_c10 int);

create table t6(
  t6_id int primary key,
  t6_t5_id int,
  t6_c1 int,
  t6_c2 int,
  t6_c3 int,
  t6_c4 int,
  t6_c5 int,
  t6_c6 int,
  t6_c7 int,
  t6_c8 int,
  t6_c9 int,
  t6_c10 int);


explain analyze select *
  from t1
       join t2 on t1_id = t2_t1_id
       join t3 on t2_id = t3_t2_id
       join t4 on t3_id = t4_t3_id
       join t5 on t4_id = t5_t4_id
       join t6 on t5_id = t6_t5_id;

┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                   QUERY PLAN
                                      │
╞════════════════════════════════════════════════════════════════════════════════════════════════════════════════╡
│ Hash Join  (cost=171.73..206.52 rows=1070 width=284) (actual
time=0.036..0.039 rows=0.00 loops=1)              │
│   Hash Cond: (t2.t2_t1_id = t1.t1_id)
                                     │
│   ->  Hash Join  (cost=136.30..168.28 rows=1070 width=240) (never
executed)                                    │
│         Hash Cond: (t3.t3_t2_id = t2.t2_id)
                                     │
│         ->  Hash Join  (cost=102.23..131.38 rows=1070 width=192) (never
executed)                              │
│               Hash Cond: (t4.t4_t3_id = t3.t3_id)
                                     │
│               ->  Hash Join  (cost=68.15..94.49 rows=1070 width=144)
(never executed)                          │
│                     Hash Cond: (t5.t5_t4_id = t4.t4_id)
                                     │
│                     ->  Hash Join  (cost=34.08..57.59 rows=1070 width=96)
(never executed)                     │
│                           Hash Cond: (t6.t6_t5_id = t5.t5_id)
                                     │
│                           ->  Seq Scan on t6  (cost=0.00..20.70 rows=1070
width=48) (never executed)           │
│                           ->  Hash  (cost=20.70..20.70 rows=1070
width=48) (never executed)                    │
│                                 ->  Seq Scan on t5  (cost=0.00..20.70
rows=1070 width=48) (never executed)     │
│                     ->  Hash  (cost=20.70..20.70 rows=1070 width=48)
(never executed)                          │
│                           ->  Seq Scan on t4  (cost=0.00..20.70 rows=1070
width=48) (never executed)           │
│               ->  Hash  (cost=20.70..20.70 rows=1070 width=48) (never
executed)                                │
│                     ->  Seq Scan on t3  (cost=0.00..20.70 rows=1070
width=48) (never executed)                 │
│         ->  Hash  (cost=20.70..20.70 rows=1070 width=48) (never executed)
                                     │
│               ->  Seq Scan on t2  (cost=0.00..20.70 rows=1070 width=48)
(never executed)                       │
│   ->  Hash  (cost=21.30..21.30 rows=1130 width=44) (actual
time=0.025..0.026 rows=0.00 loops=1)                │
│         Buckets: 2048  Batches: 1  Memory Usage: 16kB
                                     │
│         ->  Seq Scan on t1  (cost=0.00..21.30 rows=1130 width=44) (actual
time=0.024..0.024 rows=0.00 loops=1) │
│ Planning:
                                     │
│   Buffers: shared hit=6
                                     │
│ Planning Time: 3.005 ms
                                     │
│ Execution Time: 0.463 ms
                                      │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
(26 rows)



select distinct
  t1_id,
  t1_c1,
  t1_c2,
  t1_c3,
  t1_c4,
  t1_c5,
  t1_c6,
  t1_c7,
  t1_c8,
  t1_c9,
  t1_c10,
  t2_id,
  t2_c1,
  t2_c2,
  t2_c3,
  t2_c4,
  t2_c5,
  t2_c6,
  t2_c7,
  t2_c8,
  t2_c9,
  t2_c10,
  t3_id,
  t3_c1,
  t3_c2,
  t3_c3,
  t3_c4,
  t3_c5,
  t3_c6,
  t3_c7,
  t3_c8,
  t3_c9,
  t3_c10,
  t4_id,
  t4_c1,
  t4_c2,
  t4_c3,
  t4_c4,
  t4_c5,
  t4_c6,
  t4_c7,
  t4_c8,
  t4_c9,
  t4_c10,
  t5_id,
  t5_c1,
  t5_c2,
  t5_c3,
  t5_c4,
  t5_c5,
  t5_c6,
  t5_c7,
  t5_c8,
  t5_c9,
  t5_c10,
  t6_id,
  t6_c1,
  t6_c2,
  t6_c3,
  t6_c4,
  t6_c5,
  t6_c6,
  t6_c7,
  t6_c8,
  t6_c9,
  t6_c10
  from t1
       join t2 on t1_id = t2_t1_id
       join t3 on t2_id = t3_t2_id
       join t4 on t3_id = t4_t3_id
       join t5 on t4_id = t5_t4_id
       join t6 on t5_id = t6_t5_id;


══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
HashAggregate  (cost=383.07..393.77 rows=1070 width=264) (actual
time=0.037..0.040 rows=0.00 loops=1)
  Group Key: t1.t1_id, t1.t1_c1, t1.t1_c2, t1.t1_c3, t1.t1_c4, t1.t1_c5,
t1.t1_c6, t1.t1_c7, t1.t1_c8, t1.t1_c9, t1.t1_c10
  Batches: 1  Memory Usage: 177kB
  ->  Hash Join  (cost=171.73..206.52 rows=1070 width=264) (actual
time=0.023..0.026 rows=0.00 loops=1)
        Hash Cond: (t2.t2_t1_id = t1.t1_id)
        ->  Hash Join  (cost=136.30..168.28 rows=1070 width=224) (never
executed)
              Hash Cond: (t3.t3_t2_id = t2.t2_id)
              ->  Hash Join  (cost=102.23..131.38 rows=1070 width=180)
(never executed)
                    Hash Cond: (t4.t4_t3_id = t3.t3_id)
                    ->  Hash Join  (cost=68.15..94.49 rows=1070 width=136)
(never executed)
                          Hash Cond: (t5.t5_t4_id = t4.t4_id)
                          ->  Hash Join  (cost=34.08..57.59 rows=1070
width=92) (never executed)
                                Hash Cond: (t6.t6_t5_id = t5.t5_id)
                                ->  Seq Scan on t6  (cost=0.00..20.70
rows=1070 width=48) (never executed)
                                ->  Hash  (cost=20.70..20.70 rows=1070
width=48) (never executed)
                                      ->  Seq Scan on t5  (cost=0.00..20.70
rows=1070 width=48) (never executed)
                          ->  Hash  (cost=20.70..20.70 rows=1070 width=48)
(never executed)
                                ->  Seq Scan on t4  (cost=0.00..20.70
rows=1070 width=48) (never executed)
                    ->  Hash  (cost=20.70..20.70 rows=1070 width=48) (never
executed)
                          ->  Seq Scan on t3  (cost=0.00..20.70 rows=1070
width=48) (never executed)
              ->  Hash  (cost=20.70..20.70 rows=1070 width=48) (never
executed)
                    ->  Seq Scan on t2  (cost=0.00..20.70 rows=1070
width=48) (never executed)
        ->  Hash  (cost=21.30..21.30 rows=1130 width=44) (actual
time=0.019..0.020 rows=0.00 loops=1)
              Buckets: 2048  Batches: 1  Memory Usage: 16kB
              ->  Seq Scan on t1  (cost=0.00..21.30 rows=1130 width=44)
(actual time=0.019..0.019 rows=0.00 loops=1)
Planning:
  Buffers: shared hit=6
Planning Time: 80.367 ms
Execution Time: 0.606 ms
(29 rows)

when I rewrote DISTINCT to GROUP BY, the planner is fast again

(2026-11-30 08:50:54) postgres=# explain analyze select
  t1_id,
  t1_c1,
  t1_c2,
  t1_c3,
  t1_c4,
  t1_c5,
  t1_c6,
  t1_c7,
  t1_c8,
  t1_c9,
  t1_c10,
  t2_id,
  t2_c1,
  t2_c2,
  t2_c3,
  t2_c4,
  t2_c5,
  t2_c6,
  t2_c7,
  t2_c8,
  t2_c9,
  t2_c10,
  t3_id,
  t3_c1,
  t3_c2,
  t3_c3,
  t3_c4,
  t3_c5,
  t3_c6,
  t3_c7,
  t3_c8,
  t3_c9,
  t3_c10,
  t4_id,
  t4_c1,
  t4_c2,
  t4_c3,
  t4_c4,
  t4_c5,
  t4_c6,
  t4_c7,
  t4_c8,
  t4_c9,
  t4_c10,
  t5_id,
  t5_c1,
  t5_c2,
  t5_c3,
  t5_c4,
  t5_c5,
  t5_c6,
  t5_c7,
  t5_c8,
  t5_c9,
  t5_c10,
  t6_id,
  t6_c1,
  t6_c2,
  t6_c3,
  t6_c4,
  t6_c5,
  t6_c6,
  t6_c7,
  t6_c8,
  t6_c9,
  t6_c10
  from t1
       join t2 on t1_id = t2_t1_id
       join t3 on t2_id = t3_t2_id
       join t4 on t3_id = t4_t3_id
       join t5 on t4_id = t5_t4_id
       join t6 on t5_id = t6_t5_id
group by
  t1_id,
  t1_c1,
  t1_c2,
  t1_c3,
  t1_c4,
  t1_c5,
  t1_c6,
  t1_c7,
  t1_c8,
  t1_c9,
  t1_c10,
  t2_id,
  t2_c1,
  t2_c2,
  t2_c3,
  t2_c4,
  t2_c5,
  t2_c6,
  t2_c7,
  t2_c8,
  t2_c9,
  t2_c10,
  t3_id,
  t3_c1,
  t3_c2,
  t3_c3,
  t3_c4,
  t3_c5,
  t3_c6,
  t3_c7,
  t3_c8,
  t3_c9,
  t3_c10,
  t4_id,
  t4_c1,
  t4_c2,
  t4_c3,
  t4_c4,
  t4_c5,
  t4_c6,
  t4_c7,
  t4_c8,
  t4_c9,
  t4_c10,
  t5_id,
  t5_c1,
  t5_c2,
  t5_c3,
  t5_c4,
  t5_c5,
  t5_c6,
  t5_c7,
  t5_c8,
  t5_c9,
  t5_c10,
  t6_id,
  t6_c1,
  t6_c2,
  t6_c3,
  t6_c4,
  t6_c5,
  t6_c6,
  t6_c7,
  t6_c8,
  t6_c9,
  t6_c10;
                                                     QUERY PLAN

════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
HashAggregate  (cost=222.57..233.27 rows=1070 width=264) (actual
time=0.044..0.049 rows=0.00 loops=1)
  Group Key: t1.t1_id, t2.t2_id, t3.t3_id, t4.t4_id, t5.t5_id, t6.t6_id
  Batches: 1  Memory Usage: 81kB
  ->  Hash Join  (cost=171.73..206.52 rows=1070 width=264) (actual
time=0.024..0.028 rows=0.00 loops=1)
        Hash Cond: (t2.t2_t1_id = t1.t1_id)
        ->  Hash Join  (cost=136.30..168.28 rows=1070 width=224) (never
executed)
              Hash Cond: (t3.t3_t2_id = t2.t2_id)
              ->  Hash Join  (cost=102.23..131.38 rows=1070 width=180)
(never executed)
                    Hash Cond: (t4.t4_t3_id = t3.t3_id)
                    ->  Hash Join  (cost=68.15..94.49 rows=1070 width=136)
(never executed)
                          Hash Cond: (t5.t5_t4_id = t4.t4_id)
                          ->  Hash Join  (cost=34.08..57.59 rows=1070
width=92) (never executed)
                                Hash Cond: (t6.t6_t5_id = t5.t5_id)
                                ->  Seq Scan on t6  (cost=0.00..20.70
rows=1070 width=48) (never executed)
                                ->  Hash  (cost=20.70..20.70 rows=1070
width=48) (never executed)
                                      ->  Seq Scan on t5  (cost=0.00..20.70
rows=1070 width=48) (never executed)
                          ->  Hash  (cost=20.70..20.70 rows=1070 width=48)
(never executed)
                                ->  Seq Scan on t4  (cost=0.00..20.70
rows=1070 width=48) (never executed)
                    ->  Hash  (cost=20.70..20.70 rows=1070 width=48) (never
executed)
                          ->  Seq Scan on t3  (cost=0.00..20.70 rows=1070
width=48) (never executed)
              ->  Hash  (cost=20.70..20.70 rows=1070 width=48) (never
executed)
                    ->  Seq Scan on t2  (cost=0.00..20.70 rows=1070
width=48) (never executed)
        ->  Hash  (cost=21.30..21.30 rows=1130 width=44) (actual
time=0.018..0.019 rows=0.00 loops=1)
              Buckets: 2048  Batches: 1  Memory Usage: 16kB
              ->  Seq Scan on t1  (cost=0.00..21.30 rows=1130 width=44)
(actual time=0.018..0.018 rows=0.00 loops=1)
Planning:
  Buffers: shared hit=6
Planning Time: 5.945 ms
Execution Time: 0.746 ms
(29 rows)

Reply via email to