pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomas Vondra <tomas.vondra@enterprisedb.com>
To: A Shaposhnikov <artyom@gmail.com>
Cc: pgsql-general <pgsql-general@lists.postgresql.org>
Subject: Re: increasing effective_cache_size slows down join queries by a factor of 4000x
Date: Tue, 15 Feb 2022 14:55:42 +0100
Message-ID: <ee5300c3-7f49-ed52-0e7f-33563f240ba8@enterprisedb.com> (raw)
In-Reply-To: <CA+1Wm9UY+8q8abkcMOBmTcSRdHiufKJ0vwHK=QAPmJFBiTxijQ@mail.gmail.com>
References: <CA+1Wm9U_sP9237f7OH7O=-UTab71DWOO4Qc-vnC78DfsJQBCwQ@mail.gmail.com>
	<CAHOFxGqBXyC_OjrNbekaQGKC6=Xo-ac5L-aJxS5QASAsSVRyJg@mail.gmail.com>
	<CA+1Wm9XwEBvdUHWQawmji_Z4wMbCzfvqgmYg0QDMkR5Q_LtikA@mail.gmail.com>
	<d41dcd64-3ce0-4e75-7690-8ea056c5c9f4@enterprisedb.com>
	<CA+1Wm9VPLD1JwzT12=361Je0Jm4K9GXwGWMRLUyzfa5mg_zLzA@mail.gmail.com>
	<747d822c-eae1-df4e-f4c3-099a5a00a721@enterprisedb.com>
	<CA+1Wm9WQiZH6TOs5_mmCxDhXYVp=VZsWJqnAD5AdYKJt06E+cQ@mail.gmail.com>
	<d8b5f126-8043-4252-01fb-8684383fa1b2@enterprisedb.com>
	<CA+1Wm9UY+8q8abkcMOBmTcSRdHiufKJ0vwHK=QAPmJFBiTxijQ@mail.gmail.com>



On 2/15/22 01:06, A Shaposhnikov wrote:
> 
> Interestingly I have a second PG 14.2 database, with identical table 
> definitions, but about 10% smaller row counts, and the exact same query 
> works fast there without the 2nd condition:
> 

Are you sure about the 10%? Because in the plans from the first machine 
I see this:

 >
 > -> Index Scan using team_pkey on team t (cost=0.57..11382381.88
 > rows=78693167 width=175) (actual time=0.016..0.695 rows=854 loops=1)
 >

while the second machine does this:

> 
> -> Index Scan using team_pkey on team t (cost=0.57..2366113.83 
> rows=2807531 width=160) (actual time=0.031..0.801 rows=888 loops=1)
> 

That's 2.8M vs. 78M, quite far from "10% difference". Not sure about 
team_aliases table, that's imposible to say from the plans.

This may matter a lot, because we use effective cache size to calculate 
cache hit ratio for the query, with relation sizes as an input. So 
smaller relations (or larger effective_cache_size) means cheaper random 
I/O, hence preference for nested loop join.

The other thing is data distribution - that may matter too.


IMO it's pointless to investigate this further - we know what's causing 
the issue. The optimizer is oblivious that merge join will have to skip 
large part of the second input, due to the implicit condition. Notice 
that adding the condition changes the cost from:

  Limit (cost=81.33..331.82 rows=1000 width=183) ...

to

  Limit (cost=81.33..720.48 rows=1000 width=183) ...

So it seems *more* expensive than the first plan. Taken to the extreme 
the planner could theoretically have chosen to use the first plan (and 
delay the condition until after the join).

regards

-- 
Tomas Vondra
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company





view thread (14+ messages)

Message-ID: <ee5300c3-7f49-ed52-0e7f-33563f240ba8@enterprisedb.com>
Permalink:  ../ee5300c3-7f49-ed52-0e7f-33563f240ba8@enterprisedb.com/
Also on:    postgresql.org/message-id/ee5300c3-7f49-ed52-0e7f-33563f240ba8@enterprisedb.com

 · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tomas.vondra@enterprisedb.com, artyom@gmail.com
  Subject: Re: increasing effective_cache_size slows down join queries by a factor of 4000x
  In-Reply-To: <ee5300c3-7f49-ed52-0e7f-33563f240ba8@enterprisedb.com>
  References: <CA+1Wm9U_sP9237f7OH7O=-UTab71DWOO4Qc-vnC78DfsJQBCwQ@mail.gmail.com> <CAHOFxGqBXyC_OjrNbekaQGKC6=Xo-ac5L-aJxS5QASAsSVRyJg@mail.gmail.com> <CA+1Wm9XwEBvdUHWQawmji_Z4wMbCzfvqgmYg0QDMkR5Q_LtikA@mail.gmail.com> <d41dcd64-3ce0-4e75-7690-8ea056c5c9f4@enterprisedb.com> <CA+1Wm9VPLD1JwzT12=361Je0Jm4K9GXwGWMRLUyzfa5mg_zLzA@mail.gmail.com> <747d822c-eae1-df4e-f4c3-099a5a00a721@enterprisedb.com> <CA+1Wm9WQiZH6TOs5_mmCxDhXYVp=VZsWJqnAD5AdYKJt06E+cQ@mail.gmail.com> <d8b5f126-8043-4252-01fb-8684383fa1b2@enterprisedb.com> <CA+1Wm9UY+8q8abkcMOBmTcSRdHiufKJ0vwHK=QAPmJFBiTxijQ@mail.gmail.com> <ee5300c3-7f49-ed52-0e7f-33563f240ba8@enterprisedb.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