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 1sMr9S-0055B7-Hj for pgsql-general@arkaria.postgresql.org; Thu, 27 Jun 2024 15:35:10 +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 1sMr9Q-00BDIi-Tg for pgsql-general@arkaria.postgresql.org; Thu, 27 Jun 2024 15:35:09 +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 1sMr7l-00B8ZZ-OO for pgsql-general@lists.postgresql.org; Thu, 27 Jun 2024 15:33:26 +0000 Received: from mail-oo1-xc31.google.com ([2607:f8b0:4864:20::c31]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sMr7f-003PCj-Dl for pgsql-general@lists.postgresql.org; Thu, 27 Jun 2024 15:33:24 +0000 Received: by mail-oo1-xc31.google.com with SMTP id 006d021491bc7-5c21a17760fso1415842eaf.1 for ; Thu, 27 Jun 2024 08:33:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719502399; x=1720107199; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=2RQxYrvCLmP/nk4iPz1wuPmklDSsUSL3Ht6fkApqYgQ=; b=DzUJ6MsTiYCJIACR5uCX2LzIg/wzU+CAe5WCDLgI2gbc0Z7joBI2PpEYbksBaWoRgl jb14WxaFcQDHX+eNRTXEqYAonOf1NG8lkxUtlpe+Gs2GExAcMHvc6zU+EWsAgwaoYA2g 7BUMGNQJIw2nMiD35FHTuP/B3hUFLbeTWesOULuzk8tZG2vimDWX77EClGfCYN/flGwK Mfk5tvK3T+Usm2ygPFaoDXoJV5c+ww70KsFtmidDvDyNQaTFMV2FFg6Gw/GM18NxdG4y 4Pd+NtJhzTcYFGJ8XOzv1EUnRP4/JeIhHK3LTxPZeraW0WGrPI5VCt9/6vtpUKYHIQGb VIgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719502399; x=1720107199; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=2RQxYrvCLmP/nk4iPz1wuPmklDSsUSL3Ht6fkApqYgQ=; b=rih8EtrWaBnnR+69WtUladdda4W9bEZ4RUnPb/pDq7RRKceMuuZ7LFoEUwxIe8XhF9 +Y3EzRoeuVtTvt6PaILquDjd5xFP6TBgcqPbY01NZVD2E/3q9QUA7sVrUl/7J04NexHq oieB9BFXcCBPqYErqu5n20uWjYEkrOTxOniaVvA+tkMC3AHTSZw8ze9NL4MeisXPszLh cMj937Cf9lvaXf9oogzpZyejMjGVodsZmGGwZdbajyvDTT9w4JKB4EM8rTLYMmbQ0keC 3Jap/U16USxEVcTupEaEKjd5lSI1rZfVz0yocU2e/Nw8Lxmy8rAb+2xBgdIqDwOx98cK Hf5w== X-Gm-Message-State: AOJu0YwpYYVt4uy3TFPEKshTOfC87WOp8ApLrK8cUN2qIan/80fxOAp5 o68pS9Goo7/PuO7mH4eHowRY+j8DdPVRZqNa0vAhIcILuDhJfCpDY6QZVsKzuLfRIkJSy+fDBAz 3pgSgn37JBPfmkcx0Vbxip4PY4gk= X-Google-Smtp-Source: AGHT+IGBvDMAGGiztkcuz5QvUT/OSipvy8aJ2uRzDY7FLflFxtkS7zc1D/2EZIhHD7DTOPF5f7P+KKl9wc7fxaJab6o= X-Received: by 2002:a05:6820:2c01:b0:5c2:2663:bbe with SMTP id 006d021491bc7-5c226631054mr5379546eaf.1.1719502398609; Thu, 27 Jun 2024 08:33:18 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:ac9:7f0f:0:b0:539:aa10:6c7 with HTTP; Thu, 27 Jun 2024 08:33:18 -0700 (PDT) In-Reply-To: <451083be-83e8-413d-bc3a-ed7f3a6d99a9@gmail.com> References: <451083be-83e8-413d-bc3a-ed7f3a6d99a9@gmail.com> From: "David G. Johnston" Date: Thu, 27 Jun 2024 08:33:18 -0700 Message-ID: Subject: Re: A way to optimize sql about the last temporary-related row To: "agharta82@gmail.com" Cc: "pgsql-general@lists.postgresql.org" Content-Type: multipart/alternative; boundary="000000000000a8dfce061be0d9d0" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000a8dfce061be0d9d0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thursday, June 27, 2024, agharta82@gmail.com wrote= : > > Now the query: > explain (verbose, buffers, analyze) > with last_table_ids as materialized( > select xx from ( > select LAST_VALUE(pk_id) over (partition by integer_field_2 order by > datetime_field_1 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWIN= G) > xx > from test_table > where integer_field_1 =3D 1 > and datetime_field_1 <=3D CURRENT_TIMESTAMP > ) ww group by ww.xx > > ), > last_row_per_ids as ( > select tt.* from last_table_ids lt > inner join test_table tt on (tt.pk_id =3D lt.xx) > > ) > > select * /* or count(*) */ from last_row_per_ids; > > > Do you think there is a way to optimize the query? Write a lateral subquery to pick the first row of a descending ordered query? Using group to select ranked rows is both semantically wrong and potentially optimization blocking. I=E2=80=99m going by the general query form and the =E2=80=9Clast row=E2=80= =9D aspect of the question. I haven=E2=80=99t gone and confirmed your specific query can ben= efit from this approach. The window expression does give me pause. David J. --000000000000a8dfce061be0d9d0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thursday, June 27, 2024, agharta8= 2@gmail.com <agharta82@gmail.= com> wrote:

Now the query:
explain (verbose, buffers, analyze)
with last_table_ids as materialized(
=C2=A0 select xx from (
=C2=A0 select LAST_VALUE(pk_id) over (partition by integer_field_2 order by= datetime_field_1 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING= ) xx
=C2=A0 from test_table
=C2=A0 where integer_field_1 =3D 1
=C2=A0 and datetime_field_1 <=3D CURRENT_TIMESTAMP
=C2=A0 ) ww group by ww.xx

),
last_row_per_ids as (
=C2=A0 select tt.* from last_table_ids lt
=C2=A0 inner join test_table tt on (tt.pk_id =3D lt.xx)

)

select * /* or count(*) */ from last_row_per_ids;


Do you think there is a way to optimize the query?
Write a lateral subquery to pick the first row of a descending = ordered query? Using group to select ranked rows is both semantically wrong= and potentially optimization blocking.

I=E2=80=99= m going by the general query form and the =E2=80=9Clast row=E2=80=9D aspect= of the question.=C2=A0 I haven=E2=80=99t gone and confirmed your specific = query can benefit from this approach. The window expression does give me pa= use.

David J.

--000000000000a8dfce061be0d9d0--