Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ggFBC-0007h7-UC for pgsql-general@arkaria.postgresql.org; Sun, 06 Jan 2019 20:37:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ggFBA-0007FP-Sc for pgsql-general@arkaria.postgresql.org; Sun, 06 Jan 2019 20:37:52 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ggFBA-0007D9-Kd for pgsql-general@lists.postgresql.org; Sun, 06 Jan 2019 20:37:52 +0000 Received: from n3.nabble.com ([162.255.23.22]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ggFB5-0006qG-Tn for pgsql-general@postgresql.org; Sun, 06 Jan 2019 20:37:51 +0000 Received: from n3.nabble.com (localhost [127.0.0.1]) by n3.nabble.com (Postfix) with ESMTP id 4BE8B114146BD for ; Sun, 6 Jan 2019 13:37:45 -0700 (MST) Date: Sun, 6 Jan 2019 13:37:45 -0700 (MST) From: legrand legrand To: pgsql-general@postgresql.org Message-ID: <1546807065242-0.post@n3.nabble.com> In-Reply-To: References: <87r2dqrisj.fsf@news-spur.riddles.org.uk> Subject: Re: Adding LEFT JOIN to a query has increased execution time 10 times MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alexander Farber wrote > Good evening, thank you for the useful hints! > > With the further help of the IRC folks the query has been optimized (7-10 > seconds -> 0.3 second) by adding the following indices: > > CREATE INDEX ON words_games (player1, COALESCE(finished, 'INFINITY')); > CREATE INDEX ON words_games (player2, COALESCE(finished, 'INFINITY')); > CREATE INDEX ON words_moves (gid, played DESC); > CREATE INDEX ON words_social (uid, stamp DESC); > CREATE INDEX ON words_geoip USING SPGIST (block); > > and switching to LEFT JOIN LATERAL for finding the most recent records in > words_moves and words_social tables: > > [...] > > Planning time: 0.587 ms > Execution time: 0.367 ms > (36 rows) > > I was told that it still could be improved (by rearranging WHERE clauses?) > > Regards > Alex Hi Alexander, It seems that you have done a very nice tuning exercise with this query, that finishes now in less than 1 ms !!! and I have learned about LEFT JOIN LATERAL syntax too ! As you didn't spoke about DML activity ... May I suggest you to take some time to monitor the application before to continue optimizing this query ? Take time to check that: - the result is ok, - performances are stable, - there is no regression on other queries, - inserts,updates, deletes, copy are still working fast, - size of added objects are coherent and stable, - query complexity stay manageable, - there is no other application part to optimize, - ... Regards PAscal -- Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html