Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fr2pW-0002xc-QK for pgsql-general@arkaria.postgresql.org; Sat, 18 Aug 2018 15:07:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fr2pV-00023j-Cu for pgsql-general@arkaria.postgresql.org; Sat, 18 Aug 2018 15:07:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fr2pV-00023V-4z for pgsql-general@lists.postgresql.org; Sat, 18 Aug 2018 15:07:53 +0000 Received: from mail-wm0-x22b.google.com ([2a00:1450:400c:c09::22b]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fr2pS-0007um-Rm for pgsql-general@postgresql.org; Sat, 18 Aug 2018 15:07:52 +0000 Received: by mail-wm0-x22b.google.com with SMTP id y9-v6so9914659wma.5 for ; Sat, 18 Aug 2018 08:07:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:references:in-reply-to:subject:date:message-id:mime-version :content-transfer-encoding:content-language:thread-index; bh=Hd/J8ZwBiCkEo7797SPORa7xj8MDHMwAANbde5sxIKk=; b=IeZldlldRrl16mKFjKbbdWSrKhjSsk5id341hXl/P/BLu3S4ccggmmnbHJQ2tUglWh lLLyM8B/RliMzSItB+aIsahyb7RhCvxYPfTPV+rnE9N5gpAvHGcoK176AzRjWVW8eSXI 64J2Ghtx5xqEowHeGd+tIK46TxnAtsHF+dYp31qPhPifThTSuGFZtxK8VbiWqT0ZjbL1 lTrLXNrQjNXmcLNzFnh6Gekn949mEDIcDWkLiWuPoaLcCqcoIPfPFv0HsQd4qdDim40e Ad/F/EAC2Q41w2xJ1lvdcaO398FmiGjxL2YK0Xp/GM/6aQ5A4kggm55VVAPlwMHY/0p3 g1/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:references:in-reply-to:subject:date :message-id:mime-version:content-transfer-encoding:content-language :thread-index; bh=Hd/J8ZwBiCkEo7797SPORa7xj8MDHMwAANbde5sxIKk=; b=fb9NYcLe6riNaCWFdTJuOR1tSmKw+XV40KBqTYxzxCZXOygNVV0ASIt5y19U4rx6UZ rLGI/NrhVW6eXazTzVWwH2JPeCvuiw7TOfTn0D4xfqfPew2ekFqQUaaJGisFTG0Yi4kD AvIJ2vAM5o7KTcrP762NZ9Rj67hKUFAZxY6rTMdacV2W4qVMxZS+fQffvJkGrDG9f8Bm YkKYi5v4Vwp8ERfXTbiKVO06eW1NATWMdlMk8aPfVdbBcbHnFvcya+xUlQ/OfKWI58eV IB1RQh7J/e9S0aNXNSFxb4z8aCQBE0SJEU/SCQhBI9/Znlamt65gWEB6o0J9SoZ5yYAa GvvQ== X-Gm-Message-State: AOUpUlFin3nBO1TJ8Nc/7T8fCXbyoP5gZ8pYrmqy/RtUfim9yElnn1iI 2uPKmkHk1NW6gHtUqoF8CzYZdWn3 X-Google-Smtp-Source: AA+uWPy2RFjJnKfii1e20BBkr+hfqEeXFS0IxGpguzkB2+8biDptu/irlRzqjT9VlGk1fL2Cn0WTDQ== X-Received: by 2002:a1c:3b54:: with SMTP id i81-v6mr21594990wma.143.1534604868983; Sat, 18 Aug 2018 08:07:48 -0700 (PDT) Received: from PC016 (mail.ibeq.com. [212.100.42.241]) by smtp.gmail.com with ESMTPSA id n8-v6sm3674215wrt.56.2018.08.18.08.07.48 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 18 Aug 2018 08:07:48 -0700 (PDT) From: To: References: <004b01d436d6$ff96c840$fec458c0$@gmail.com> <20180818143848.GB3326@tamriel.snowman.net> In-Reply-To: <20180818143848.GB3326@tamriel.snowman.net> Subject: AW: CTE with JOIN of two tables is much faster than a regular query Date: Sat, 18 Aug 2018 17:07:47 +0200 Message-ID: <006401d43705$399f6aa0$acde3fe0$@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 16.0 Content-Language: de Thread-Index: AQMeRW6BypEtj0kC9MzG04zOKiRbkAH+DP1CoiGMV8A= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk > -----Urspr=FCngliche Nachricht----- > Von: Stephen Frost > Gesendet: Samstag, 18. August 2018 16:39 Hello, >=20 > > What can I do to improve the performance of the regular query = without > > using a CTE? >=20 > You could possibly build a trigram index on the field you're = searching, which > could avoid the full table scan. Of course, that index could be quite large, so > there's downsides to that. If these are words you're looking for then = you > could use PG's full text indexing to build indexes on the words and = then use > that instead. If you are fine working with words but are concerned = about > misspellings then you can extract out the distinct words, build a = trigram index > on those, find the most similar words based on the input and then = search for > those words using the FTI. >=20 > Unfortunately, we don't currently pay attention to things like average string > length when considering the cost of performing an 'ilike', so we = figure that > doing the filtering first and then the join will be faster, but that obviously falls > over in some cases, like this one. Using the CTE forces PG to (today, = at least) > do the join first, but that isn't really good to rely on. A trigram index would be a possible help in this particular scenario but size and updating the index in other parts of the application would be probably create other issues. I may try it, though.=20 But thanks to confirming my assumption. I just thought that it should be obvious to the optimizer to do the join first and filter on this result. = But I'm reading you r post that there is nothing that I can do to modify the behavior of the optimizer. Or is there a way to specify the cost for an operator (ILIKE in this case) on a specific column?=20 Thanks=20 Klaus