pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Matthew Bellew <matthewb@labkey.com>
To: pgsql-bugs@postgresql.org
Subject: Re: BUG #13614: Optimizer choosing
Date: Wed, 14 Oct 2015 04:08:16 +0000 (UTC)
Message-ID: <loom.20151014T055051-633@post.gmane.org> (raw)
References: <20150911153138.352.6826@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

This issue has been affecting a lot of our customers lately, (perhaps it has
become  even more common to use nested loop joins in 9.4.5?).  I have created
a repro to show  the problem.

Given two table A joined to B and a filter of say selectivity Asel% on A and
Bsel% on B.   It is very optimistic to assume that the selectivity of the rows
in A join B  is Asel * Bsel.   But this seems to be what Postgres assumes.  A
very conservative estimate would be  that the join selectivity is
min(Asel,Bsel).  The conservative estimate is probably not  ideal, but is less
likely to lead to a pathological plan (one that is orders of magnitude  worse
than if no filters had been applied at all), than using the very optimistic
estimate.

Just guessing that the geometric mean of the optimistic and pessimistic
estimate.   sqrt(min(Asel,Bsel) * (Asel*Bsel)) might be close to a good
compromise.

Regardless, or the exact estimation it seems clear that

a) the current estimate for joins over filtered tables it way too optmistic b)
the estimate should not be allowed to float down to 1, 1 should only be used
to  mean exactly 1, any other estimate should be capped at some sort of
minimum to help  avoid choosing joins that degrade badly if the estimate is
wrong.


SAMPLE 1 - no UNIQUE indexes, on my machine despite estimates, the optimizer
chooses MERGE JOINS and results are very fast.

DROP TABLE IF EXISTS  A;
DROP TABLE IF EXISTS  B;
DROP TABLE IF EXISTS  C;
DROP TABLE IF EXISTS  D;
DROP TABLE IF EXISTS  E;

SELECT * INTO A 
FROM 
( 
  SELECT seq, 100000-seq as rev, (seq+111) % 100000 as rot, 
  rand, rand%10 as ten, rand%100 as hundred, rand%1000 as thousand, 
  md5(random()::text) AS descr
  FROM (SELECT generate_series(1,100000) AS seq, cast(floor(random()*10000) as 
int4) as rand) _
) _;
--CREATE UNIQUE INDEX ON A (seq);
--CREATE UNIQUE INDEX ON A (rev);
--CREATE UNIQUE INDEX ON A (rot);
CREATE INDEX ON A (hundred);

SELECT * INTO B FROM A;
--CREATE UNIQUE INDEX ON B (seq);
--CREATE UNIQUE INDEX ON B (rev);
--CREATE UNIQUE INDEX ON B (rot);
CREATE INDEX ON B (hundred);

SELECT * INTO C FROM A;
--CREATE UNIQUE INDEX ON C (seq);
--CREATE UNIQUE INDEX ON C (rev);
--CREATE UNIQUE INDEX ON C (rot);
CREATE INDEX ON C (hundred);

SELECT * INTO D FROM A;
--CREATE UNIQUE INDEX ON D (seq);
--CREATE UNIQUE INDEX ON D (rev);
--CREATE UNIQUE INDEX ON D (rot);
CREATE INDEX ON D (hundred);


SELECT * INTO E FROM A;
--CREATE UNIQUE INDEX ON E (seq);
--CREATE UNIQUE INDEX ON E (rev);
--CREATE UNIQUE INDEX ON E (rot);
CREATE INDEX ON E (hundred);


SELECT _a.seq, _b.rand, _c.ten, _d.hundred, _e.descr
FROM 
   (SELECT * FROM A WHERE hundred in (1,2,3,4,5)) _a 
   JOIN (SELECT * FROM B WHERE hundred in (1,2,3,4,5)) _b ON  _a.seq = _b.seq  
   JOIN (SELECT * FROM C WHERE hundred in (1,2,3,4,5)) _c ON _b.rev = _c.rev
   JOIN (SELECT * FROM D WHERE hundred in (1,2,3,4,5)) _d ON _c.seq = _d.seq
   JOIN (SELECT * FROM E  WHERE hundred in (1,2,3,4,5)) _e ON _d.rot = _e.rot



SAMPLE 2- with UNIQUE indexes, on my machine the optimizer starts using NESTED
LOOPS once the row estimate gets to 1.  This case isn't tragically bad (worse
than  SAMPLE 1), but we hit pathological cases in the wild that are
exceedingly hard to  avoid.


 DROP TABLE IF EXISTS  A;
DROP TABLE IF EXISTS  B;
DROP TABLE IF EXISTS  C;
DROP TABLE IF EXISTS  D;
DROP TABLE IF EXISTS  E;

SELECT * INTO A 
FROM 
( 
  SELECT seq, 100000-seq as rev, (seq+111) % 100000 as rot, 
  rand, rand%10 as ten, rand%100 as hundred, rand%1000 as thousand, 
  md5(random()::text) AS descr
  FROM (SELECT generate_series(1,100000) AS seq, cast(floor(random()*10000) as 
int4) as rand) _
) _;
CREATE UNIQUE INDEX ON A (seq);
CREATE UNIQUE INDEX ON A (rev);
CREATE UNIQUE INDEX ON A (rot);
CREATE INDEX ON A (hundred);

SELECT * INTO B FROM A;
CREATE UNIQUE INDEX ON B (seq);
CREATE UNIQUE INDEX ON B (rev);
CREATE UNIQUE INDEX ON B (rot);
CREATE INDEX ON B (hundred);

SELECT * INTO C FROM A;
CREATE UNIQUE INDEX ON C (seq);
CREATE UNIQUE INDEX ON C (rev);
CREATE UNIQUE INDEX ON C (rot);
CREATE INDEX ON C (hundred);

SELECT * INTO D FROM A;
CREATE UNIQUE INDEX ON D (seq);
CREATE UNIQUE INDEX ON D (rev);
CREATE UNIQUE INDEX ON D (rot);
CREATE INDEX ON D (hundred);


SELECT * INTO E FROM A;
CREATE UNIQUE INDEX ON E (seq);
CREATE UNIQUE INDEX ON E (rev);
CREATE UNIQUE INDEX ON E (rot);
CREATE INDEX ON E (hundred);


SELECT _a.seq, _b.rand, _c.ten, _d.hundred, _e.descr
FROM 
   (SELECT * FROM A WHERE hundred in (1,2,3,4,5)) _a 
   JOIN (SELECT * FROM B WHERE hundred in (1,2,3,4,5)) _b ON  _a.seq = _b.seq  
   JOIN (SELECT * FROM C WHERE hundred in (1,2,3,4,5)) _c ON _b.rev = _c.rev
   JOIN (SELECT * FROM D WHERE hundred in (1,2,3,4,5)) _d ON _c.seq = _d.seq
   JOIN (SELECT * FROM E  WHERE hundred in (1,2,3,4,5)) _e ON _d.rot = _e.rot






-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



Message-ID: <loom.20151014T055051-633@post.gmane.org>
Permalink:  ../loom.20151014T055051-633@post.gmane.org/
Also on:    postgresql.org/message-id/loom.20151014T055051-633@post.gmane.org

 · 

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-bugs@postgresql.org
  Cc: matthewb@labkey.com
  Subject: Re: BUG #13614: Optimizer choosing
  In-Reply-To: <loom.20151014T055051-633@post.gmane.org>
  References: <20150911153138.352.6826@wrigleys.postgresql.org> <loom.20151014T055051-633@post.gmane.org>

* 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