HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Why is PostgreSQL choosing the more expensive join order?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlwhytheordermorejoinchoosingexpensive

Problem

PostgreSQL using defaults, plus

default_statistics_target=1000
random_page_cost=1.5


Version

PostgreSQL 10.4 on x86_64-pc-linux-musl, compiled by gcc (Alpine 6.4.0) 6.4.0, 64-bit


I've vacuumed and analyzed. The query is very straightforward:

SELECT r.price
FROM account_payer ap
  JOIN account_contract ac ON ap.id = ac.account_payer_id
  JOIN account_schedule "as" ON ac.id = "as".account_contract_id
  JOIN schedule s ON "as".id = s.account_schedule_id
  JOIN rate r ON s.id = r.schedule_id
WHERE ap.account_id = 8


Every id column is the primary key, and everything being joined on is a foreign key relationship, and each foreign key has an index. Plus an index for account_payer.account_id.

It takes 3.93s to return 76k rows.

```
Merge Join (cost=8.06..83114.08 rows=3458267 width=6) (actual time=0.228..3920.472 rows=75548 loops=1)
Merge Cond: (s.account_schedule_id = "as".id)
-> Nested Loop (cost=0.57..280520.54 rows=6602146 width=14) (actual time=0.163..3756.082 rows=448173 loops=1)
-> Index Scan using schedule_account_schedule_id_idx on schedule s (cost=0.14..10.67 rows=441 width=16) (actual time=0.035..0.211 rows=89 loops=1)
-> Index Scan using rate_schedule_id_code_modifier_facility_idx on rate r (cost=0.43..486.03 rows=15005 width=10) (actual time=0.025..39.903 rows=5036 loops=89)
Index Cond: (schedule_id = s.id)
-> Materialize (cost=0.43..49.46 rows=55 width=8) (actual time=0.060..12.984 rows=74697 loops=1)
-> Nested Loop (cost=0.43..49.32 rows=55 width=8) (actual time=0.048..1.110 rows=66 loops=1)
-> Nested Loop (cost=0.29..27.46 rows=105 width=16) (actual time=0.030..0.616 rows=105 loops=1)
-> Index Scan using account_schedule_pkey on account_schedule "as" (cost=0.14..6.22 rows=105 width=16) (actual time=0.014..0.098 rows=105 loops=1)
-> Index Scan using account_contract_pkey on account_contract ac (cost=0.14..0.20 ro

Solution

It seems either your statistics are not accurate (run vacuum analyze to refresh them) either you have correlated columns in your model (and so you'll need to perform create statistics to inform the planer of that fact).

The join_collapse parameter allows the planner to rearrange joins so it performs first the one that fetches less data. But, for performance, we can't let the planner do that on a query with a lot of joins. By default, it's set to 8 joins max. By setting it to 1, you simply disable that ability.

So how does postgres foresees how many rows this query should fetch ? It uses statistics to estimate the number of rows.

What we can see in your explain plans is that there are several inaccurate rows number estimation (first value is estimate, second is actual).

For example,here :

Materialize  (cost=0.43..49.46 rows=55 width=8) (actual time=0.060..12.984 rows=74697 loops=1)


The planner estimated to get 55 rows when he actually got 74697.

What I'd do (if I were in your shoes) is :

  • analyze the five tables involved to refresh statistics



  • Replay explain analyze



  • Look at the difference between estimate row numbers and actual row numbers



  • If estimate row numbers are right, maybe the plan changed and is more efficient. If everything is ok, you might consider changing your autovacuum settings so analyze (and vacuum) performs more often



  • If estimate row numbers are still wrong, it seems like you have correlated data in your table (third normal form violation).You might consider declaring it with CREATE STATISTICS (documentation here)



If you need more information about row estimates and its calculations, you'll find everything you need in Tomas Vondra's conf talk "Create statistics - What is it for ?" (slides here)

Code Snippets

Materialize  (cost=0.43..49.46 rows=55 width=8) (actual time=0.060..12.984 rows=74697 loops=1)

Context

StackExchange Database Administrators Q#210857, answer score: 4

Revisions (0)

No revisions yet.