From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Alexander Farber <alexander.farber@gmail.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Adding LEFT JOIN to a query has increased execution time 10 times
Date: Sun, 06 Jan 2019 23:00:36 +0000
Message-ID: <87muodqwcf.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <CAADeyWgOOKMyyhEm1DV5btpZ_0+dGdqAsMcA7HuWF4-nXFeq_g@mail.gmail.com>
References: <CAADeyWhxj4Pmwaa0EcjkMRPOydZTYOTyESqzPoFfe-Xj4mri0Q@mail.gmail.com>
<87r2dqrisj.fsf@news-spur.riddles.org.uk>
<CAADeyWh5jLcRViB2rqYOiehVA1e0k0FD3oL19oGqLo6Z2TSuKg@mail.gmail.com>
<CAADeyWgOOKMyyhEm1DV5btpZ_0+dGdqAsMcA7HuWF4-nXFeq_g@mail.gmail.com>
>>>>> "Alexander" == Alexander Farber <alexander.farber@gmail.com> writes:
Alexander> Good evening, thank you for the useful hints!
Alexander> With the further help of the IRC folks the query has been
Alexander> optimized (7-10 seconds -> 0.3 second)
0.3 MILLIseconds, actually.
(You chanced not to catch me around on IRC, but I see that didn't
matter.)
Alexander> by adding the following indices:
Alexander> CREATE INDEX ON words_games (player1, COALESCE(finished, 'INFINITY'));
Alexander> CREATE INDEX ON words_games (player2, COALESCE(finished, 'INFINITY'));
Alexander> CREATE INDEX ON words_moves (gid, played DESC);
Alexander> CREATE INDEX ON words_social (uid, stamp DESC);
I'm not a big fan of using DESC on indexes; it's almost never needed,
because any btree index can be scanned in reverse. (You only actually
need it if you're mixing ASC/DESC orderings in an ORDER BY and want an
index that matches it.)
Alexander> Also I have increased the following parameters in
Alexander> postgresql.conf -
Alexander> from_collapse_limit = 24
Alexander> join_collapse_limit = 24
Ironically, I think these settings don't affect the query now since
removing the EXISTS conditions (which count as joins) means there are
now less than 8 joined tables. But keeping them high is probably a good
idea so that you don't get problems if you ever add another join or two.
Alexander> Now the whole query looks as following and the EXPLAIN
Alexander> output pasted is below -
Just for future reference, when you include explain output in your email
(which you should, for the benefit of the archives - paste sites and
explain.depesz.com have limited lifetimes), it's best if you can make
sure your email client doesn't word-wrap them into near-unreadability.
Alexander> I was told that it still could be improved (by rearranging
Alexander> WHERE clauses?)
Maybe, but once you're down to sub-millisecond execution times, further
optimization is usually only worthwhile for very heavily executed
queries.
--
Andrew (irc:RhodiumToad)
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: andrew@tao11.riddles.org.uk, alexander.farber@gmail.com
Subject: Re: Adding LEFT JOIN to a query has increased execution time 10 times
In-Reply-To: <87muodqwcf.fsf@news-spur.riddles.org.uk>
References: <CAADeyWhxj4Pmwaa0EcjkMRPOydZTYOTyESqzPoFfe-Xj4mri0Q@mail.gmail.com> <87r2dqrisj.fsf@news-spur.riddles.org.uk> <CAADeyWh5jLcRViB2rqYOiehVA1e0k0FD3oL19oGqLo6Z2TSuKg@mail.gmail.com> <CAADeyWgOOKMyyhEm1DV5btpZ_0+dGdqAsMcA7HuWF4-nXFeq_g@mail.gmail.com> <87muodqwcf.fsf@news-spur.riddles.org.uk>
* 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