pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Geier <geidav.pg@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: PostgreSQL Developers <pgsql-hackers@lists.postgresql.org>
Subject: Re: Optimize join selectivity estimation by not reading MCV stats for unique join attributes
Date: Mon, 14 Nov 2022 10:19:18 +0100
Message-ID: <16acb727-24e8-6453-1711-aae339802966@gmail.com> (raw)
In-Reply-To: <522124.1668179795@sss.pgh.pa.us>
References: <b9846ca0-5f1c-9b26-5881-aad3f42b07f0@gmail.com>
	<522124.1668179795@sss.pgh.pa.us>

Hi Tom,
> There won't *be* any MCV stats for a column that ANALYZE perceives to
> be unique, so I'm not quite sure where the claimed savings comes from.
We save if one join attribute is unique while the other isn't. In that 
case stored MCV stats are read for the non-unique attribute but then 
never used. This is because MCV stats in join selectivity estimation are 
only used if they're present on both columns
> Please provide a concrete example.

A super simple case already showing a significant speedup is the 
following. The more ways to join two tables and the more joins overall, 
the higher the expected gain.

CREATE TABLE bar(col INT UNIQUE);
CREATE TABLE foo (col INT);
INSERT INTO foo SELECT generate_series(1, 1000000, 0.5);
SET default_statistics_target = 10000;
ANALYZE foo, bar;
\timing on
EXPLAIN SELECT * FROM foo, bar WHERE foo.col = bar.col;

Running the above query five times gave me average runtimes of:

- 0.62 ms without the patch and
- 0.48 ms with the patch.

--
David Geier
(ServiceNow)






view thread (13+ messages)  latest in thread

Message-ID: <16acb727-24e8-6453-1711-aae339802966@gmail.com>
Permalink:  ../16acb727-24e8-6453-1711-aae339802966@gmail.com/
Also on:    postgresql.org/message-id/16acb727-24e8-6453-1711-aae339802966@gmail.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-hackers@postgresql.org
  Cc: geidav.pg@gmail.com, tgl@sss.pgh.pa.us
  Subject: Re: Optimize join selectivity estimation by not reading MCV stats for unique join attributes
  In-Reply-To: <16acb727-24e8-6453-1711-aae339802966@gmail.com>
  References: <b9846ca0-5f1c-9b26-5881-aad3f42b07f0@gmail.com> <522124.1668179795@sss.pgh.pa.us> <16acb727-24e8-6453-1711-aae339802966@gmail.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