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 1fr2NU-0001UA-0c for pgsql-general@arkaria.postgresql.org; Sat, 18 Aug 2018 14:38:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fr2NS-00041V-5L for pgsql-general@arkaria.postgresql.org; Sat, 18 Aug 2018 14:38:54 +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 1fr2NR-00041N-U4 for pgsql-general@lists.postgresql.org; Sat, 18 Aug 2018 14:38:53 +0000 Received: from tamriel.snowman.net ([2001:470:e38f::11]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fr2NO-0007Dv-7a for pgsql-general@postgresql.org; Sat, 18 Aug 2018 14:38:52 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id 46F995F79C; Sat, 18 Aug 2018 10:38:48 -0400 (EDT) Date: Sat, 18 Aug 2018 10:38:48 -0400 From: Stephen Frost To: kpi6288@gmail.com Cc: pgsql-general@postgresql.org Subject: Re: CTE with JOIN of two tables is much faster than a regular query Message-ID: <20180818143848.GB3326@tamriel.snowman.net> References: <004b01d436d6$ff96c840$fec458c0$@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="EZN6irWB3fS+DSGO" Content-Disposition: inline In-Reply-To: <004b01d436d6$ff96c840$fec458c0$@gmail.com> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --EZN6irWB3fS+DSGO Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Greetings, * kpi6288@gmail.com (kpi6288@gmail.com) wrote: > The CTE mentioned below completes the query in 4.5 seconds while the regu= lar > query takes 66 seconds. I read from EXPLAIN ANALYSE that the regular query > starts with a full table scan over "Doc" while the CTE joins the two tabl= es > first and applies the filter condition in the 2nd step.=20 >=20 > I believe that some rows in "Doc" which are not referenced by "F" contain= a > large amount of data in the field "szText" and this will slow down the IL= IKE > operator.=20 Yup, that appears to be what's happening. > What can I do to improve the performance of the regular query without usi= ng > 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. 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. Thanks! Stephen --EZN6irWB3fS+DSGO Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJbeC93AAoJEO1sijiDR2RVV1gP/jVnlTbo49Otn7uM/0JOQEoh jnIl04MNJVGdpsDd40XYCuMJzKKQl9GNrX9vJNzFpyp66t2j+8hGWtWpWpH6/5zN odTYLYv/kbXrAYAR29Wjy8XZyoB5zs3GVlX9rsCMSlq33lSJUGM6xQBneMQUut7e C9VcVBP7BBAo/sbP7p0Yl7rSF/s2vXu0b8wMRlx20vZEQBUd2eAMr3OfdI86FrMi 0vMlwlwdBURd4D+pegXEO3Triv3vlIWHe48qmfU0bs0Gj3wGtBb85KSrOp8S7S7Z f9d5P99rXwjxC4pu2AnK++Hgs6zaXTd+rhQSUjfMNUJH6iTEsfT71wqbMstdUcSe E814rD4Nj5E5GYeqssJ3PnhaxlizjDUsqRb0cHn6+sm2+033iDPv7SYyWKSeUAtu /qz9dshziHDxvf/9IEJQ9e2PuenhIRBG2yZ/css62ltoQHpR4RbYEawCvBAtTeQI ON8NN5nQo6TOFVHUJeU3Qc1eQ46BLtVxJrCniNRS7z1Hxf5DiyrGbqXxyEKkg5L2 zHgmnA1vOggY8opbemsOpzvpcbcT4atAiDYfLHvStrWlItUa830x18UoTa1Bogl3 WZtPQaMmjgreKeRbp0S49GVJKKCyEXPEtiFhy6vYa8P74IVOzjwGpLnhpFDYcCH/ mdLL+n/2fBvegpWt0dFc =8BiW -----END PGP SIGNATURE----- --EZN6irWB3fS+DSGO--