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 1q1LtR-0005Wx-Ku for pgsql-general@arkaria.postgresql.org; Tue, 23 May 2023 06:53:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q1LtP-0007Ha-MZ for pgsql-general@arkaria.postgresql.org; Tue, 23 May 2023 06:53:11 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q1LtP-0007HL-AD for pgsql-general@lists.postgresql.org; Tue, 23 May 2023 06:53:11 +0000 Received: from portal-smtp-out.hot.ee ([194.126.101.125]) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q1LtL-001MYg-FD for pgsql-general@postgresql.org; Tue, 23 May 2023 06:53:09 +0000 Received: from localhost (localhost [127.0.0.1]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id 101E15FC4 for ; Tue, 23 May 2023 09:53:03 +0300 (EEST) X-Virus-Scanned: Debian amavisd-new at vm-hot-relay10.estpak.ee Received: from portal-smtp-out.hot.ee ([127.0.0.1]) by localhost (vm-hot-relay10.estpak.ee [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id k7O+8McVhLf0 for ; Tue, 23 May 2023 09:53:00 +0300 (EEST) Received: from mail.hot.ee (hot-relayhost1.estpak.ee [213.180.2.79]) by vm-hot-relay10.estpak.ee (Postfix) with ESMTP id 7E31A5FB4 for ; Tue, 23 May 2023 09:53:00 +0300 (EEST) Message-ID: <7181fbab-1b51-c005-2576-d287fbcd784c@hot.ee> Date: Tue, 23 May 2023 09:53:02 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Content-Language: et To: pgsql-general From: Andrus Subject: How to speed up product code and subcode match Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi! Price list of main products vordlusajuhinnak contains 3 prices for product (column toode) and has 39433 products:     create table vordlusajuhinnak( toode varchar(60), n2 numeric(8,2), n3 numeric(8,2), n4 numeric(8,2) ); toode column in unique, may be primary key in table and contains upper case letters, digits and - characters. product table (toode) contains 733021 products:     CREATE TABLE toode (         grupp character(1),         toode character(60) primary key,         ... lot of other columns       ); Both tables have pattern indexes to speed up queries:     CREATE INDEX toode_toode_pattern_idx         ON toode (toode bpchar_pattern_ops ASC NULLS LAST);     -- This index is probably not used, should removed:     CREATE INDEX vordlusajuhinnak_toode_pattern_idx ON vordlusajuhinnak(toode bpchar_pattern_ops); Product table as both main products and subproducts with sizes. Size is separated by main product code always by / character:     SHOE1-BLACK     SHOE1-BLACK/38     SHOE1-BLACK/41     SHOE1-BLACK/42     SHOE1-BLACK/43     SHOE2/XXL     SHOE2/L Product codes contain upper characers only in this table also. Trying to get prices for all products using     create table peatoode as     select toode.toode , n2, n3, n4     from toode, vordlusajuhinnak     where  toode.toode between vordlusajuhinnak.toode and vordlusajuhinnak.toode||'/z' Takes 4.65 hours. How to speed this query up? Output from explain:     > "Nested Loop  (cost=0.55..272273178.12 rows=3202240012 width=78)" "     > ->  Seq Scan on vordlusajuhinnak  (cost=0.00..721.33 rows=39433 width=32)" "  ->  Index Only Scan using toode_pkey on toode     > (cost=0.55..6092.62 rows=81207 width=60)" "        Index Cond: (toode     > >= (vordlusajuhinnak.toode)::bpchar)" "        Filter: ((toode)::text <= ((vordlusajuhinnak.toode)::text || '/z'::text))" Using  PostgreSQL 13.2, compiled by Visual C++ build 1900, 64-bit in Windows server and psqlODBC driver. Upgrading Postgres is possible, if this helps. Tried also using like:     WHERE toode.toode=vordlusajuhinnak.toode OR       toode.toode LIKE vordlusajuhinnak.toode||'/%' Posted also in https://stackoverflow.com/questions/76311957/how-to-match-character-columns-with-separated-subcolumns Andrus.