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 1q1SZ8-00073r-QE for pgsql-general@arkaria.postgresql.org; Tue, 23 May 2023 14:00:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q1SZ6-00057Y-Es for pgsql-general@arkaria.postgresql.org; Tue, 23 May 2023 14:00:40 +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 1q1SZ6-00057P-5a for pgsql-general@lists.postgresql.org; Tue, 23 May 2023 14:00:40 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q1SYz-001fDl-QN for pgsql-general@postgresql.org; Tue, 23 May 2023 14:00:39 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 34NE0PO1908934; Tue, 23 May 2023 10:00:25 -0400 From: Tom Lane To: Andrus cc: pgsql-general Subject: Re: How to speed up product code and subcode match In-reply-to: <7181fbab-1b51-c005-2576-d287fbcd784c@hot.ee> References: <7181fbab-1b51-c005-2576-d287fbcd784c@hot.ee> Comments: In-reply-to Andrus message dated "Tue, 23 May 2023 09:53:02 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <908932.1684850425.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Tue, 23 May 2023 10:00:25 -0400 Message-ID: <908933.1684850425@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Andrus writes: > 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 You could probably have devised a worse data representation if you really tried, but it would have taken some effort. Separate the product code and size into two columns --- if there's somebody who really wants to see them in the above format, give them a view or generated column. Then instead of the impossible-to-optimize queries you showed, you could do something like select toode.toode , n2, n3, n4 from toode, vordlusajuhinnak where toode.toode = vordlusajuhinnak.toode; Anytime you're trying to join two tables on something that isn't a plain equality condition (or ANDed conditions), you're in for a world of hurt. regards, tom lane