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.96) (envelope-from ) id 1vmsHK-00HGj5-1J for pgsql-general@arkaria.postgresql.org; Mon, 02 Feb 2026 11:39:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vmsHI-00DaXE-1E for pgsql-general@arkaria.postgresql.org; Mon, 02 Feb 2026 11:39: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.96) (envelope-from ) id 1vmsHH-00DaX6-31 for pgsql-general@lists.postgresql.org; Mon, 02 Feb 2026 11:39:36 +0000 Received: from mail-ej1-x630.google.com ([2a00:1450:4864:20::630]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vmsHG-00000000BYy-3vve for pgsql-general@lists.postgresql.org; Mon, 02 Feb 2026 11:39:35 +0000 Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-b8871718b05so679988066b.2 for ; Mon, 02 Feb 2026 03:39:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1770032373; cv=none; d=google.com; s=arc-20240605; b=kV5uaERZQW3lhPDdAhzRdmCXK8rnWgED65u22oM9QKRCqJMH8SHdnQ+JjfnFcsiBTZ Tr0zSU5WkZ0fGNYcBJBWQM0rEFR0AXCoZix39pBUFPLBQEgT8RxAiqYDD8nSRWOgWkP8 LU9DWEWQsWKAs3n8bCsa6Lp5hvtTpewkFs7uhEkcNjYscSYtoAI8ZlGFiwkDEzPtXhmm r/l9GswuzsueRifgNxA1d/chgC6/v5xeS/casAJN6QSNy5H0nFve0FXv+TakL10El9Em hbnmMC7xmqbWRVJPmMc9iAIDtehsVQlPmkbeIWR3ToMi4J7T2J/7xXrPssIs8zS0uh6F j+NA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=ssqWQqTpwjWvZd9b18xLdO4GgUi8y7BHUBU/xUFY6Lo=; fh=4rCG0PM8n0FOokGy8sSWDJpgPdCgp6yIXcpABJ7tUh0=; b=apNFqqE/0CCDuMPoT2EZTFoFDv7RQGjSVvyKY1wnEX7hKz6fwXIMg3gTQbgMrbNdX5 zuw5YkJ54/uZbbX0K+tUjMBPxagQLRi6pgTfxx7XqHWAafYi/9dMEjVh554DUb4s4GnK 5CV5c/XK07/txfXljfvD+xZwYO5Y3u+UTnMf9m1reLXUnokzm3mui2tQhq5zW1Yu+Cs7 gLA0PtpTHbPPvORqsG1Pu5W05krsGNjjUgiXSxdUv6hIgN9l5Zoh3jfAttV8PtCpqwfX IW7ockkf4ciS+9paM39H0ICf8s+K8AN7Yr1gRguV2uSfn//zAo/Njw0P9YlnZMywl+et RKIw==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770032373; x=1770637173; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=ssqWQqTpwjWvZd9b18xLdO4GgUi8y7BHUBU/xUFY6Lo=; b=JolOv1LvpPOvw+R0I+ZzBLv/sIILz6sqcwKaqrHvwDmXDVpB3hZ7Bg/FKQYbhfuChk jNNrqfVYXHLtRmzkjmnmOrwqYs1Hod25HmZFf+gYCWCdctXV20LQ1reoktbimWLz+RiF MpExtUQaa8ThTarXyH5fbvGpM71FuIOoMryxgP6cAJUYO4JIZEKS8yignp0xlT5fPgRI ntvLYx1+q9DH6x1bBzRadXJxEi7banmBl72Cht8lNd2U0qbCxue6MFl9NfIAUcufeWp6 6egiglvfvTW5n6klHZiusRi+cx1XSUI/6xKWB0mgNyhMAJANgV3/HPFXG6g4tW2JbFfl +BpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770032373; x=1770637173; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ssqWQqTpwjWvZd9b18xLdO4GgUi8y7BHUBU/xUFY6Lo=; b=OgrEmXvOZmbR6/48iznsNoO/9x6UCRPGLgeEjbp+LD2xvO+blRe3f64c2RA1rnbTsw AFIdhzMCGuagoQuF/LelaSnFh7FP2CXUCzcqFDinJxeUhlROEgzAPTOJ45OYIDK6Olii 9NcY+H5lfrnt/YwsoDT8iS0PfJZJSBUbMS5XIpWseuhb9n2zI1VTv+r2jO0tOO8COGM+ 7EYtBeJ2n1XphQD2rzjXjnfwnJgMvcIVnogy/WM8TAdfyfhp3zLzPfDyupijdZ0/0hOy ot3nFGLh7i1Qb7KDAE/cB57BviHEdQshtBY4Um62GgQujMTLxGMxpXdy5THaQmbugcM0 3ULQ== X-Gm-Message-State: AOJu0Yz7PEJpcDDNHOalgvuaEjkGB1Hrm+Sa9o+lg1yYKdOOy/zjPEMU /QQsyutzr9drIN4z1tWyQyNey7HlaEDpmVhsR8JodRTTh0DD/L+pBpsZKzdNJm8gXzO6Tfs+buL cfBKF04gOC9TPQnZCoMSLoQ5appLg6LqFuNVv X-Gm-Gg: AZuq6aJj6o4KxlszbFyDj0vWWW2DCwzOgHVGrR8zE/+HFIEr32LiiFwi9UwMpX90Chi wMQnG0/hyr6gZzjuKNVCbE1JsJd6FTpTyZ/kLaHQmFtK0UYjSFHWDeUn0i3gtYI0L4pNXmxrlcy wyh/0DUmJ6QjK00Ek5kiAdJmS5dr6INFbuUOBeGzwa5h9OufJYj8g5DT31wxDSjA0+AND6QjmYY VEOuLoMOlrm1tDOrjUr3k0R1QmH2UneN47KMZzqNWR2aFTlZAim3QyzhSsUR+anf/mow4IKrv5M 9IeBb8U6WuUL5HoOlw3rTcMZJ7lBkqFI+jBk0ixr X-Received: by 2002:a17:907:7b87:b0:b88:5d85:48f with SMTP id a640c23a62f3a-b8dff53c87amr743171766b.14.1770032372949; Mon, 02 Feb 2026 03:39:32 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: yudhi s Date: Mon, 2 Feb 2026 17:09:21 +0530 X-Gm-Features: AZwV_QjyzWbdUdYdqGHdRVWIcjzb3piLBYxheZYWxZ7Q4o-Ua6ga93jUhvM5qzQ Message-ID: Subject: Re: Top -N Query performance issue and high CPU usage To: pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="000000000000d4a5b10649d5c635" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000d4a5b10649d5c635 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Feb 2, 2026 at 3:17=E2=80=AFAM Peter J. Holzer w= rote: > > If you do have that many simultaneous accesses to the landing page, and > you can't speed up the query significantly (I take it you've seen the > suggestion to check whether there's an index on > APP_schema.txn_tbl.tran_date), then maybe you don't need to perform it > for every user? I don't know what the query is supposed to do, but > unless the "ent_id" is really a user id, it doesn't seem to be specific > to the user. So maybe you can cache the result for a minute or an hour > and show the same result to everybody who logs in during that time. > > > There was no index on column tran_date , I created one and it's making the query finish in ~200ms, a lot faster than in the past. Below is the portion of the query and its plan which actually consumes most of the resource and time post the new index creation. https://gist.github.com/databasetech0073/344df46c328e02b98961fab0cd221492 1) Now the part which takes time is the "nested loop" join on the "ent_id" column. Can we do anything to make it much better/faster? 2) Also another question I had was, with this new index the table scan of txn_tbl is now fully eliminated by the "Index Scan Backward" even i have other columns from that table projected in the query, so how its getting all those column values without visiting table but just that index scan backward operation? --000000000000d4a5b10649d5c635 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Mon, Feb 2, = 2026 at 3:17=E2=80=AFAM Peter J. Holzer <hjp-pgsql@hjp.at> wrote:

If you do have that many simultaneous accesses to the landing page, and
you can't speed up the query significantly (I take it you've seen t= he
suggestion to check whether there's an index on
APP_schema.txn_tbl.tran_date), then maybe you don't need to perform it<= br> for every user? I don't know what the query is supposed to do, but
unless the "ent_id" is really a user id, it doesn't seem to b= e specific
to the user. So maybe you can cache the result for a minute or an hour
and show the same result to everybody who logs in during that time.

=C2=A0 =C2=A0 =C2=A0 =C2=A0

There w= as no index on column=C2=A0 tran_date = =C2=A0, I created one and it's making the query finish in=C2=A0 ~200ms,= a lot faster than in the past. Below is the portion of the query and its p= lan which actually consumes most of the resource and time post the new inde= x creation.


1) Now the part=C2=A0 which takes time is the "nested loop" join= on the "ent_id"=C2=A0 column. Can we do anything to make it much= better/faster?

2) Also another question I had was= ,=C2=A0=C2=A0with this new index the table scan of txn_tbl is now fully eli= minated by the "Index Scan Backward" even i have other columns fr= om that table projected in the query, so how its getting all those column v= alues without visiting table but just that index scan backward operation?


--000000000000d4a5b10649d5c635--