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 1t3K9O-00Btcu-EA for pgsql-general@arkaria.postgresql.org; Tue, 22 Oct 2024 19:02:38 +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 1t3K9M-002IB9-NC for pgsql-general@arkaria.postgresql.org; Tue, 22 Oct 2024 19:02:37 +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 1t3K9M-002IA2-BY for pgsql-general@lists.postgresql.org; Tue, 22 Oct 2024 19:02:36 +0000 Received: from mail-lf1-x134.google.com ([2a00:1450:4864:20::134]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1t3K9K-002KYW-4k for pgsql-general@lists.postgresql.org; Tue, 22 Oct 2024 19:02:35 +0000 Received: by mail-lf1-x134.google.com with SMTP id 2adb3069b0e04-539e690479cso6398688e87.3 for ; Tue, 22 Oct 2024 12:02:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729623752; x=1730228552; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=o/ma3JdM4T5LCuk1DvGZqhZw5vs38P/fM1VUPxGru6w=; b=bMp4u969slu/5MmAJTaXEaWlm2W+De1r/2obFpf7lPeX8WQu1h20pSuuFgH7Y9naQv ihb8PEOZN2g4NU5lgcY3A4sP2AEGVvHPHnqTouP8CkQ1OePuEwdGrWvlOze5vQXoLk4O F4HYX6UIVxfVA9c5vB99xaUhzLIPKz8WU/3hXa5vKZZQaypnaYYZe1cDX+d7mxam2uW1 5bP2grAI142NFNZEQuVOKMEXLx009+DKNvPG6qXuyXBU+cqoGZxAEQkV2OPoNNHy/DYz 0AKKnmGw6lS+NBMsjvz1w5x2Fe7nfju75Y7MZCPv3sYQcJ3JSNszNSn0a5f9x6I/f4Ae /ilg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729623752; x=1730228552; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=o/ma3JdM4T5LCuk1DvGZqhZw5vs38P/fM1VUPxGru6w=; b=rocT4vR6zg8EcbgZjHsDJj4tP2y6PzoxDnN4bqOxPjOgTjVFbOZiDNdAu9MozlFBHS Pk/0DNiBS5VpsXrox8bZIRcpMizmhaHNVpufCi+2a45hDpSd00Y8V1b6BqQfz77eklvl n1Vk6wovx0ZygZ3PhqU33dFzeA5VxPr208+6xN1rpoPMLzRbDZLqtMnd65lbMMQaou/p peiAu/9hWqgXDIWORuN9OD1lix5u2WY8G/W0eeI1tuL/STwZdZn2eDctlV+oAybmtwdb fTmdc89xuS9Vfefkm0BHwTGTsfMw6jzYJbO8hfiOkifcuzZc23xiFw98cBL4+m1ZyrxD 3uaw== X-Gm-Message-State: AOJu0YzZHyXLob1tKLIrCe5OCX6pgJZmUQ7oFK2z8+9zDjsx/V6vxZm1 jhPNpb+Znq7aPGbVNJtTPtAnSDLuTtvX3XVgXjrYDAKVIHtrJgRsYoBtNcOkWWX/QgJ54OKbsJ8 Ol/dXnv7Ykepvl5Imz37jxg49zDoeegv5 X-Google-Smtp-Source: AGHT+IHQvYuzwQxLfYum2zte28gDtzXNkayAqBLSjyZqx3ANy16/EgOoNL3fTYUo9+6u5Dxo3B4zIBfTHb6v3uaCIXo= X-Received: by 2002:a05:6512:3b99:b0:539:e436:f1cd with SMTP id 2adb3069b0e04-53b191f7d9emr349544e87.16.1729623751833; Tue, 22 Oct 2024 12:02:31 -0700 (PDT) MIME-Version: 1.0 References: <20241016213610.wbfqj3r7gg5ba6eh@hjp.at> In-Reply-To: From: Greg Sabino Mullane Date: Tue, 22 Oct 2024 15:01:55 -0400 Message-ID: Subject: Re: Query performance issue To: yudhi s Cc: pgsql-general@lists.postgresql.org, hjp-pgsql@hjp.at Content-Type: multipart/alternative; boundary="00000000000052c4620625156962" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000052c4620625156962 Content-Type: text/plain; charset="UTF-8" To be frank, there is so much wrong with this query that it is hard to know where to start. But a few top items: * Make sure all of the tables involved have been analyzed. You might want to bump default_statistics_target up and see if that helps. * As mentioned already, increase work_mem, as you have things spilling to disk (e.g. external merge Disk: 36280kB) * Don't use the "FROM table1, table2, table3" syntax but use "FROM table1 JOIN table2 ON (...) JOIN table3 ON (...) * Try not to use subselects. Things like WHERE x IN (SELECT ...) are expensive and hard to optimize. * You have useless GROUP BY clauses in there. Remove to simplify the query * There is no LIMIT. Does the client really need all 135,214 rows? Cheers, Greg --00000000000052c4620625156962 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
To be frank, there is so much wrong with this query that i= t is hard to know where to start. But a few top items:

* Make sure a= ll of the tables involved have been analyzed. You might want to bump defaul= t_statistics_target up and see if that helps.

* As mentioned already= , increase work_mem, as you have things spilling to disk (e.g. external mer= ge Disk: 36280kB)

* Don't use the "FROM table1, table2, tab= le3" syntax but use "FROM table1 JOIN table2 ON (...) JOIN table3= ON (...)

* Try not to use subselects. Things like WHERE x IN (SELEC= T ...) are expensive and hard to optimize.

* You have useless GROUP= BY clauses in there. Remove to simplify the query

* There is no LIM= IT. Does the client really need all 135,214 rows?

Cheers,
Greg
--00000000000052c4620625156962--