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 1fqxfR-0004Lu-SW for pgsql-general@arkaria.postgresql.org; Sat, 18 Aug 2018 09:37:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fqxfL-0006Yu-VQ for pgsql-general@arkaria.postgresql.org; Sat, 18 Aug 2018 09:37:03 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fqxfL-0006YC-D4 for pgsql-general@lists.postgresql.org; Sat, 18 Aug 2018 09:37:03 +0000 Received: from mail-wm0-x232.google.com ([2a00:1450:400c:c09::232]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1fqxfI-0007CI-F6 for pgsql-general@postgresql.org; Sat, 18 Aug 2018 09:37:02 +0000 Received: by mail-wm0-x232.google.com with SMTP id t25-v6so9356060wmi.3 for ; Sat, 18 Aug 2018 02:37:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version:content-language :thread-index; bh=6uQdbDkh76yoJCMoWVyljtVkT5QfMQ4FohieEua4fbA=; b=sTkE7X1mhubEixfz+a/yivqkbsZxG9e62rMYdEp6GAas/K4TQimD44/3ft63aIYu67 oxCTiQVA7R5svIeOncyyOCsdSZIz2W2KPIBjTGQOpOLBbtF/81Th4x1AS+/yg3uSsPGU yrQdpPEe32RvYOSRrxkfm+cvvJ43xtMuvKMhOdAa6NFuhJLBHimwODrFmNZvRprl4Uje 6Wmk2L5SbfScQ9z/Pc5/nLbMy/Vl07+cqmTqOKfimA+RZRHZiCVm+CQYgoBvgRDU9K66 /UmdZyozhyRgzn8K/j7lrvKQJS4vFjz1aJOAJbIHd3nFe/I0QINyP1I/1BfkGEKDwbTD vr/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:subject:date:message-id:mime-version :content-language:thread-index; bh=6uQdbDkh76yoJCMoWVyljtVkT5QfMQ4FohieEua4fbA=; b=kSlM5vcl0eLsLkA1q/iBaZkwpTsjDxzgczfJT/3FPKsPIFHHTLsviPjT7HKQBdvlsb ayXLHte9UVwXYWDgNpOLj/RuNE3SLxVTQPAr24Kxsb3xKr3ydBeOPbt5vm4smgbfNp6g XWgr1p7Z4S/qLX8+JlMSjJmZ1EwSBLyzqTCHpDFPwrvMy0bt1b6Jc1Os1OPnGLxOb4YX np0x4U0Nsi3DfPUT4qzFt7OHdd3h90vuy+P1ls3ty4yRTmw9+WPf3/H+J+MQ8HE3VONb 9/ep6YCysZRP6dcec7h8tdiO8UxOFl+TE3Mjv+yNVqqwFVH5R72e8hlfMicRBNBcTKvq ZglQ== X-Gm-Message-State: AOUpUlFGMMm6RL892q/ppeyK/rPul7I8U25vW9qH9ZUouZd8kc+sOkID ygiWXIZfIL9yijP2ur2eL5I6NJQg X-Google-Smtp-Source: AA+uWPzlKhcstY3af016oRR8K9KU18Ar3cdFjmp7xTNHhospLAfVItBR8ywFCG8j9kzBVtK8Gcteag== X-Received: by 2002:a1c:7c0c:: with SMTP id x12-v6mr21033966wmc.58.1534585014968; Sat, 18 Aug 2018 02:36:54 -0700 (PDT) Received: from PC016 (mail.ibeq.com. [212.100.42.241]) by smtp.gmail.com with ESMTPSA id i6-v6sm3392262wrr.10.2018.08.18.02.36.53 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 18 Aug 2018 02:36:54 -0700 (PDT) From: To: Subject: CTE with JOIN of two tables is much faster than a regular query Date: Sat, 18 Aug 2018 11:36:53 +0200 Message-ID: <004b01d436d6$ff96c840$fec458c0$@gmail.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_004C_01D436E7.C3211EE0" X-Mailer: Microsoft Outlook 16.0 Content-Language: de Thread-Index: AdQ21q7i8MPelmnBSZWF1tWFHnTjjw== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multipart message in MIME format. ------=_NextPart_000_004C_01D436E7.C3211EE0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Running PostgreSQL 9.5 on Windows. The CTE mentioned below completes the query in 4.5 seconds while the regular 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 tables first and applies the filter condition in the 2nd step. 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 ILIKE operator. What can I do to improve the performance of the regular query without using a CTE? This is a much simplified extract from a larger application: CREATE TABLE Doc ( oID UUID NOT NULL PRIMARY KEY, uDocID UUID NOT NULL UNIQUE, szText TEXT ); CREATE TABLE F ( oID UUID NOT NULL PRIMARY KEY, uDocRef UUID, CONSTRAINT F_fkey1 FOREIGN KEY (uDocRef) REFERENCES Doc (uDocID) ); -- just in case . ALTER TABLE Doc ALTER uDocID SET STATISTICS 10000; ALTER TABLE Doc ALTER szText SET STATISTICS 10000; VACUUM ANALYSE Doc; SELECT COUNT(*) FROM Doc; => 125946 records ALTER TABLE F ALTER uDocRef SET STATISTICS 10000; VACUUM ANALYSE F; SELECT COUNT(*) FROM F; => 32605 records Result with CTE: EXPLAIN ANALYSE WITH a AS ( SELECT F.oID, Doc.szText FROM F JOIN Doc ON F.uDocRef = Doc.udocid ) SELECT * FROM a WHERE szText ILIKE '%480GB%'; "CTE Scan on a (cost=9463.42..10197.03 rows=52 width=48) (actual time=478.770..4551.613 rows=10 loops=1)" " Filter: (sztext ~~* '%480GB%'::text)" " Rows Removed by Filter: 32595" " CTE a" " -> Hash Join (cost=973.61..9463.42 rows=32605 width=359) (actual time=36.998..100.337 rows=32605 loops=1)" " Hash Cond: (doc.udocid = f.udocref)" " -> Seq Scan on doc (cost=0.00..7691.46 rows=125946 width=359) (actual time=0.008..18.269 rows=125946 loops=1)" " -> Hash (cost=566.05..566.05 rows=32605 width=32) (actual time=35.825..35.825 rows=32605 loops=1)" " Buckets: 32768 Batches: 1 Memory Usage: 2294kB" " -> Seq Scan on f (cost=0.00..566.05 rows=32605 width=32) (actual time=0.005..14.677 rows=32605 loops=1)" "Planning time: 4.689 ms" "Execution time: 4554.893 ms" Result with regular query: EXPLAIN ANALYSE SELECT F.oID, Doc.szText FROM F JOIN Doc ON F.uDocRef = Doc.udocid WHERE szText ILIKE '%480GB%'; "Hash Join (cost=8006.56..8694.93 rows=5 width=359) (actual time=66500.415..66506.978 rows=10 loops=1)" " Hash Cond: (f.udocref = doc.udocid)" " -> Seq Scan on f (cost=0.00..566.05 rows=32605 width=32) (actual time=0.002..3.143 rows=32605 loops=1)" " -> Hash (cost=8006.32..8006.32 rows=19 width=359) (actual time=66500.023..66500.023 rows=16 loops=1)" " Buckets: 1024 Batches: 1 Memory Usage: 19kB" " -> Seq Scan on doc (cost=0.00..8006.32 rows=19 width=359) (actual time=8864.720..66499.991 rows=16 loops=1)" " Filter: (sztext ~~* '%480GB%'::text)" " Rows Removed by Filter: 125930" "Planning time: 263.542 ms" "Execution time: 66507.003 ms" ------=_NextPart_000_004C_01D436E7.C3211EE0 Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Running PostgreSQL 9.5 on Windows. =

 

The CTE mentioned below completes the query in 4.5 seconds = while the regular 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 tables first and applies = the filter condition in the 2nd step. =

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 ILIKE operator. =

 

What can I do to improve the performance of the regular = query without using a CTE?

 

This is a much simplified extract = from a larger application:

 

CREATE TABLE Doc = (

  = oID UUID NOT NULL PRIMARY KEY,

  uDocID UUID NOT NULL = UNIQUE,

  szText TEXT

  );

 

CREATE TABLE F = (

  = oID UUID NOT NULL PRIMARY KEY,

  uDocRef = UUID,

  CONSTRAINT F_fkey1 FOREIGN KEY (uDocRef) REFERENCES = Doc (uDocID)

  ); 

 

-- just in case = …

ALTER TABLE Doc ALTER uDocID SET STATISTICS = 10000;

ALTER TABLE Doc ALTER szText SET STATISTICS = 10000;

VACUUM ANALYSE Doc;

 

SELECT COUNT(*) FROM = Doc;

=3D> 125946 records

 

ALTER TABLE F ALTER uDocRef SET = STATISTICS 10000;

VACUUM ANALYSE F;

 

SELECT COUNT(*) FROM = F;

=3D> = 32605 records

 

Result with CTE:

 

EXPLAIN ANALYSE =

  WITH a AS (

  SELECT F.oID, = Doc.szText

  FROM F

  JOIN Doc ON F.uDocRef =3D = Doc.udocid

)

SELECT *

  FROM a

  WHERE szText ILIKE = '%480GB%';

 

"CTE Scan on a  (cost=3D9463.42..10197.03 = rows=3D52 width=3D48) (actual time=3D478.770..4551.613 rows=3D10 = loops=3D1)"

"  Filter: (sztext ~~* = '%480GB%'::text)"

"  Rows Removed by Filter: = 32595"

"  CTE a"

"    = ->  Hash Join  (cost=3D973.61..9463.42 rows=3D32605 = width=3D359) (actual time=3D36.998..100.337 rows=3D32605 = loops=3D1)"

"         = Hash Cond: (doc.udocid =3D f.udocref)"

"         = ->  Seq Scan on doc  (cost=3D0.00..7691.46 rows=3D125946 = width=3D359) (actual time=3D0.008..18.269 rows=3D125946 = loops=3D1)"

"         = ->  Hash  (cost=3D566.05..566.05 rows=3D32605 width=3D32) = (actual time=3D35.825..35.825 rows=3D32605 = loops=3D1)"

"         =        Buckets: 32768  Batches: = 1  Memory Usage: 2294kB"

"         =        ->  Seq Scan on f  = (cost=3D0.00..566.05 rows=3D32605 width=3D32) (actual = time=3D0.005..14.677 rows=3D32605 = loops=3D1)"

"Planning time: 4.689 ms"

"Execution time: 4554.893 = ms"

 

Result with regular query:

 

EXPLAIN ANALYSE =

SELECT = F.oID, Doc.szText

FROM F

JOIN Doc ON F.uDocRef =3D = Doc.udocid

WHERE szText ILIKE '%480GB%';

 

"Hash Join  = (cost=3D8006.56..8694.93 rows=3D5 width=3D359) (actual = time=3D66500.415..66506.978 rows=3D10 = loops=3D1)"

"  Hash Cond: (f.udocref =3D = doc.udocid)"

"  ->  Seq Scan on f  = (cost=3D0.00..566.05 rows=3D32605 width=3D32) (actual = time=3D0.002..3.143 rows=3D32605 = loops=3D1)"

"  ->  Hash  = (cost=3D8006.32..8006.32 rows=3D19 width=3D359) (actual = time=3D66500.023..66500.023 rows=3D16 = loops=3D1)"

"        Buckets: = 1024  Batches: 1  Memory Usage: = 19kB"

"        = ->  Seq Scan on doc  (cost=3D0.00..8006.32 rows=3D19 = width=3D359) (actual time=3D8864.720..66499.991 rows=3D16 = loops=3D1)"

"         =      Filter: (sztext ~~* = '%480GB%'::text)"

"         =      Rows Removed by Filter: = 125930"

"Planning time: 263.542 = ms"

"Execution time: 66507.003 = ms"

 

 

 

 

------=_NextPart_000_004C_01D436E7.C3211EE0--