Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tzboe-004CoM-IP for pgsql-performance@arkaria.postgresql.org; Tue, 01 Apr 2025 13:38:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tzbod-002R5u-BM for pgsql-performance@arkaria.postgresql.org; Tue, 01 Apr 2025 13:38:07 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tzbod-002R5F-1c for pgsql-performance@lists.postgresql.org; Tue, 01 Apr 2025 13:38:07 +0000 Received: from mail-ej1-x633.google.com ([2a00:1450:4864:20::633]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tzboa-002Lgr-1P for pgsql-performance@lists.postgresql.org; Tue, 01 Apr 2025 13:38:06 +0000 Received: by mail-ej1-x633.google.com with SMTP id a640c23a62f3a-ac2963dc379so887661766b.2 for ; Tue, 01 Apr 2025 06:38:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1743514683; x=1744119483; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=uL/ZlVaaqFcgxwEXX98ptNFVCvnfPyT0DthlvJmki8U=; b=NhebEiaHrB7nQLINSwkCXMTX1nkWEBjZUUxruynhKQOCBWGAdMUfblTJsOHJzAkP48 eQxoxTZKU+T/UR8Ot1b5hhyngsXJJWnznneZDfntUE7W6n6A+6rQ40KmXH64H7eFeFFH JLwrENPPYnMmNgoGNypAD1KEG8nJI155IIvj3tJq8xDdnG3JoankWcuoyAK53zWRFCvd /SlGLO9VzV7rJP4L3yL8NEfbjbu5qptMhM6+ejXDGfe6moSkThV1v53bQ+bY6D7oB2tp 6IVxzcycQk28A+EWi6/t5yJoG4W8cUo6H4w9Tj0IoJoJrcs5QxE68XoFIRGm/JBs2jIF 2X7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743514683; x=1744119483; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=uL/ZlVaaqFcgxwEXX98ptNFVCvnfPyT0DthlvJmki8U=; b=YZTuaCaaBfDJSE8wPqXYN7wb09JxrGNgCg7I49Zu0FFkPQwyXD+eniqOoDfgCABwal iF/QnmCoENb+I0pUgBYTuE/2yk+8bdf6gPX/ggq/OS49DYEXI3PXNVos5TnopfOgs6If FKctEWlpvbgYLKRFZa31S5aH7s/XmbeiJr7AtIeQH1CKa13kEikpGqjEhMSlwMIOx9jy 26AVGmwFvq/P0MraYql+KR9lwMYABAwCVVth6BoVjKfS1TnGg+APAU8EzLWjg7qtbvOG AQXY+0uPGRHyKXTHlWCrF7VQ2r3FVhNH2mHAesSoqV5UNkLtdOaYD97c0betSvNMH16X TsUg== X-Gm-Message-State: AOJu0YxYxHt27YQCAL0qnaRKmabFMBQjAl+K5foPsEz7DyXQYDnwU2Uy S1mmEDNo+1Y6JYqNoCw1oDI9To4gm1XzoDXte13ClF5a7bQhuW8Pi8rzmUZCx8MrbABRpf87eFw l6Kji47I0GtjUfyBytR4ID/zUsRxp3tVD X-Gm-Gg: ASbGncsq0WbtjO+8Q3tRyWKfmc5EwU88vTvhJ3Y+tCJ7B1osgxucRPkfbgxJuclj5ll kDVaGHgQwlRyOILVReiFI9s+XHKYknfh/f3xGOSb7Ym2h+sD4vappn6DzFl2WUdo4dCaeCmDMJ0 JRgJgWCUYdw44WsGO5nN1/zvowYxs= X-Google-Smtp-Source: AGHT+IEDO8zFAcUBmsz/+xCkhJUi3s1PzRtApIGgrFMwPm/u3xHhxzhKPtLszvdY/sNRiwrj/fkTAbwli0oybWbj7F4= X-Received: by 2002:a17:907:7214:b0:ac6:d9cb:58c0 with SMTP id a640c23a62f3a-ac738b82b65mr966338566b.50.1743514682857; Tue, 01 Apr 2025 06:38:02 -0700 (PDT) MIME-Version: 1.0 From: Chris Joysn Date: Tue, 1 Apr 2025 15:37:51 +0200 X-Gm-Features: AQ5f1JoJk4cQ3DKpuCoUnY9Mm1w8WZNNxH0xJQ_9F0F48V5XuC6obP6-Q50ynB8 Message-ID: Subject: Very slow query performance when using CTE To: pgsql-performance@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000552b500631b7a5bb" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000552b500631b7a5bb Content-Type: text/plain; charset="UTF-8" Hello, I have an issue when using CTEs. A query, which consists of multiple CTEs, runs usually rather fast (~5s on my environment). But it turned out that using one CTE can lead to execution times of up to one minute. That CTE is used two times within the query. In the CTE there are 2600 rows, compared to results of the other CTEs its a fraction of the data. When replacing this CTE and use the original table instead in the jions, the query performs nicely. However, it is not always like this. Running the same query on a almost same set of data, quantity wise, may give indeed good performance when using that CTE. This is the slow performing query using CTE: https://explain.dalibo.com/plan/45ce86d9cfge14c7 And this is the fast performing query without that CTE: https://explain.dalibo.com/plan/4abgc4773gg349b4 The query runs on the very same environment and data. What can be the issue here and how can I address it? --000000000000552b500631b7a5bb Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello,
I have an issue when using CTEs. A q= uery, which consists of multiple CTEs, runs usually rather fast (~5s on my = environment). But it turned out that using one CTE can lead to execution ti= mes of up to one minute.
That CTE is used two times within the qu= ery. In the CTE there are 2600 rows, compared to results of the other CTEs = its a fraction of the data.
When replacing this CTE and use t= he original table instead in the jions, the query performs nicely.
However, it is not always like this. Running the same query on a almost s= ame set of data, quantity wise, may give indeed good performance when using= that CTE.
This is the slow performing query using CTE:=C2=A0
And this is the= fast performing query without that CTE:=C2=A0
The query runs on the very same envi= ronment and data.
What can be the issue here and how c= an I address it?
--000000000000552b500631b7a5bb--