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 1jdTvj-0003Z7-OW for pgsql-general@arkaria.postgresql.org; Tue, 26 May 2020 07:23:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jdTvi-0004H2-2U for pgsql-general@arkaria.postgresql.org; Tue, 26 May 2020 07:23:18 +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 1jdTvh-0004Gv-QM for pgsql-general@lists.postgresql.org; Tue, 26 May 2020 07:23:17 +0000 Received: from securemail-y25.synaq.com ([196.35.198.11]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jdTvZ-0002ls-Nn for pgsql-general@postgresql.org; Tue, 26 May 2020 07:23:16 +0000 Received: from [197.90.32.26] (helo=[192.168.0.8]) by securemail-pl-omx11.synaq.com with esmtpa (Exim 4.92.3) (envelope-from ) id 1jdTvL-000fJD-J1 for pgsql-general@postgresql.org; Tue, 26 May 2020 09:22:55 +0200 From: Frank Millman Subject: Slow SELECT To: pgsql-general@postgresql.org Message-ID: <0fb25867-78d2-d57f-134a-c3fccac7bbde@chagford.com> Date: Tue, 26 May 2020 09:22:49 +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 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: 1jdTvL-000fJD-J1 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 Hi all I have a SELECT that runs over 5 times slower on PostgreSQL compared with Sql Server and sqlite3. I am trying to understand why. I have a table that looks like this (simplified) - CREATE TABLE my_table ( row_id SERIAL PRIMARY KEY, deleted_id INT DEFAULT 0, fld_1 INT REFERENCES table_1(row_id), fld_2 INT REFERENCES table_2(row_id), fld_3 INT REFERENCES table_3(row_id), fld_4 INT REFERENCES table_4(row_id), tran_date DATE, tran_total DEC(21,2) ); CREATE UNIQUE INDEX my_table_ndx ON my_table (fld_1, fld_2, fld_3, fld_4, tran_date) WHERE deleted_id = 0; 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. On identical data, this takes 0.06 sec on SQL Server, 0.04 sec on sqlite3, and 0.31 sec on PostgreSQL. I have looked at the EXPLAIN, but I don't really know what to look for. I can supply it if that would help. Thanks for any advice. Frank Millman