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 1jdWrN-0002zG-SX for pgsql-general@arkaria.postgresql.org; Tue, 26 May 2020 10:31:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jdWrL-0003Nx-Jf for pgsql-general@arkaria.postgresql.org; Tue, 26 May 2020 10:30:59 +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 1jdWrL-0003Nq-BP for pgsql-general@lists.postgresql.org; Tue, 26 May 2020 10:30:59 +0000 Received: from securemail-y20.synaq.com ([196.35.198.97]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jdWrI-0004PS-Jl for pgsql-general@postgresql.org; Tue, 26 May 2020 10:30:58 +0000 Received: from [197.90.32.26] (helo=[192.168.0.8]) by securemail-pl-omx7.synaq.com with esmtpa (Exim 4.92.3) (envelope-from ) id 1jdWrD-000RN9-Rc for pgsql-general@postgresql.org; Tue, 26 May 2020 12:30:52 +0200 Subject: Re: Slow SELECT To: pgsql-general References: <0fb25867-78d2-d57f-134a-c3fccac7bbde@chagford.com> From: Frank Millman Message-ID: <2460452e-0f9b-cd33-fd93-8d506b03f2d0@chagford.com> Date: Tue, 26 May 2020 12:30:47 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Thunderbird/68.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit X-SYNAQ-Pinpoint-Information: Please contact SYNAQ for more information X-SYNAQ-Pinpoint-ID: 1jdWrD-000RN9-Rc X-SYNAQ-Pinpoint: Found to be clean X-SYNAQ-Pinpoint-SpamCheck: not spam, SpamAssassin (not cached, score=-1.1, required 9, autolearn=disabled, ALL_TRUSTED -1.00, DCC_REPUT_13_19 -0.10) X-Pinpoint-From: frank@chagford.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2020-05-26 12:04 PM, David Rowley wrote: > On Tue, 26 May 2020 at 19:23, Frank Millman wrote: >> The table sizes are - >> my_table : 167 rows >> table_1 : 21 rows >> table_2 : 11 rows >> table_3 : 3 rows >> table_4 : 16 rows >> >> Therefore for each tran_date in my_table there are potentially >> 21x11x3x16 = 11088 rows. Most will be null. >> >> I want to select the row_id for the last tran_date for each of those >> potential groups. This is my select - >> >> SELECT ( >> SELECT a.row_id FROM my_table a >> WHERE 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.deleted_id = 0 >> ORDER BY a.tran_date DESC LIMIT 1 >> ) >> FROM table_1 b, table_2 c, table_3 d, table_4 e >> >> Out of 11088 rows selected, 103 are not null. > > Perhaps SQL Server is doing something to rewrite the subquery in the > target list to a LEFT JOIN. PostgreSQL currently does not do that. > > 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. > Thank you David. I tried that and it produced the correct result in 53ms, which is what I am looking for. It will take me some time to understand it fully, so I have some homework to do! Much appreciated. Frank