Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jdXy2-0005wA-2B for pgsql-general@arkaria.postgresql.org; Tue, 26 May 2020 11:41:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jdXy0-0004eI-W3 for pgsql-general@arkaria.postgresql.org; Tue, 26 May 2020 11:41:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jdXy0-0004eB-Ny for pgsql-general@lists.postgresql.org; Tue, 26 May 2020 11:41:56 +0000 Received: from relay10.mail.gandi.net ([217.70.178.230]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jdXxx-00050a-H0 for pgsql-general@postgresql.org; Tue, 26 May 2020 11:41:56 +0000 Received: from [172.16.255.146] (82.30.10.93.rev.sfr.net [93.10.30.82]) (Authenticated sender: vik@postgresfriends.org) by relay10.mail.gandi.net (Postfix) with ESMTPSA id 3E193240010; Tue, 26 May 2020 11:41:50 +0000 (UTC) Subject: Re: Slow SELECT To: David Rowley , Frank Millman Cc: pgsql-general References: <0fb25867-78d2-d57f-134a-c3fccac7bbde@chagford.com> From: Vik Fearing Message-ID: <9f49d31a-31ce-4d19-5053-9d98902bc62d@postgresfriends.org> Date: Tue, 26 May 2020 13:41:49 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 5/26/20 12:04 PM, David Rowley wrote: > Since "my_table" is small, you'd likely be much better doing a manual > rewrite of the query to join a subquery containing the required > details from "my_table". It looks like you want the row_id from the > latest tran_date for each fld_N column. So something like: > > SELECT a.row_id > FROM table_1 b > CROSS JOIN table_2 c > CROSS JOIN table_3 d > CROSS JOIN table_4 e > LEFT OUTER JOIN ( > SELECT fld_1,fld_2,fld_3,fld_4,row_id,tran_date, > ROW_NUMBER() OVER (PARTITION BY fld_1,fld_2,fld_3,fld_4 ORDER BY > tran_date DESC) row_num > FROM my_table > WHERE deleted_id = 0 > ) a ON a.fld_1 = b.row_id AND a.fld_2 = c.row_id AND a.fld_3 = > d.row_id AND a.fld_4 = e.row_id AND a.row_num = 1; > > Should do it. You could also perhaps do something with DISTINCT ON > instead of using ROW_NUMBER(). That might be a bit more efficient, but > it's unlikely to matter too much since there are only 167 rows in that > table. I would expect a lateral query to be better here. SELECT a.* FROM table_1 AS b CROSS JOIN table_2 AS c CROSS JOIN table_3 AS d CROSS JOIN table_4 AS e CROSS JOIN LATERAL ( SELECT * FROM my_table AS a WHERE (a.fld_1, a.fld_2, a.fld_3, a.fld_4) = (b.row_id, c.row_id, d.row_id, e.row_id) AND a.deleted = 0 ORDER BY a.tran_date DESC FETCH FIRST ROW ONLY ) AS a WHERE a.row_id IS NOT NULL; You will likely want an index on my_table (fld_1, fld_2, fld_3, fld_4, tran_date) if your dataset gets bigger. This query is 100% Standard SQL, so it *should* work on other engines. That doesn't mean it will, though. -- Vik Fearing