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 1sMrnf-0059Kg-1N for pgsql-general@arkaria.postgresql.org; Thu, 27 Jun 2024 16:16:43 +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 1sMrnd-00Bf29-Ck for pgsql-general@arkaria.postgresql.org; Thu, 27 Jun 2024 16:16:41 +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 1sMrnd-00Bf20-1k for pgsql-general@lists.postgresql.org; Thu, 27 Jun 2024 16:16:41 +0000 Received: from mail-lf1-x129.google.com ([2a00:1450:4864:20::129]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sMrna-003PVW-N1 for pgsql-general@lists.postgresql.org; Thu, 27 Jun 2024 16:16:40 +0000 Received: by mail-lf1-x129.google.com with SMTP id 2adb3069b0e04-52cdf2c7454so10507760e87.1 for ; Thu, 27 Jun 2024 09:16:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719504997; x=1720109797; 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=yYdDx8lU5Izb48ceqN5uojC6pz5OmalAWadbAyy+xa0=; b=FaDGusjHFFgN9z8FOOyLREPeqPZgqV7sob0v3GjLXjwk2nPsjGCdu9b63GzK9aPKJz JZnqHG9qf+Wzf/TR1ONihPxgPgmj45qAvcPnICbZGqGPC8U0+U5zAPQuZM+JDvloLyKi k3Ni03X+tLfwBfdL7iW3E1bJm0i7E5W+fsqZ3eWYyqnS3Bjkb44mCxaMRaZ/8RX2J/p5 3H12hNv0Prsbbli2PMvRWDz/0jZMEEUTW/48q4zAM/DJktyVvydF+l/xA4zpibAiTnTW eTirYN/SAouFrAq9fEPqy4fUkpB0v8R3bPLiKcrWddZnF12pUiMMaUNCmJ6cMgw1fAzg S+JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719504997; x=1720109797; 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=yYdDx8lU5Izb48ceqN5uojC6pz5OmalAWadbAyy+xa0=; b=AtAnx3Eik6M1XUS9YpLUejFgqNXYrD6O2y4bnPFwns7VLxU6b8AXWiiKtKdEwf/hfj 1nMNxu31MM+U8m7c7h5TvUmfEsL8XBB6YJjCUgvCr2ODdFoLXsyYSXMz6IrtWT6UGDTo E/QrBXxke2SGVWMM3FU7tmgxQNX3x2Gg0dWQz5ScEA6XgvOOYbDcOKOJp5Y3r2DxNM/5 fqKsF9BLZvbn2IW9S5f2aDEDxBDlMvuRnzpv+pUxooAotALifmQz1PhBIeZ8Clu4d04V DWgffIHPsVOhDULqwrDeejakOPIeIuk5ymwYfInKS/Ve0iXyl6r54lFwO4X3y36gIlms EFnQ== X-Gm-Message-State: AOJu0YwdOpil+5IpL0mKahIduYQ1wDzgVltFkVUXRCB4dS7rSmLYRpjS Ku8uBYBoenSWzYD0bWzQ/B97D4PBQOKIOxW3OMoFPCOiAhWy2hi1nk6PBhsYDVyFfFXx9S0uBo9 9VwkhfuxgejjFxv/5XweE67O2LWbplA== X-Google-Smtp-Source: AGHT+IHdN7Qu02w8CvfBNRneQb3DMxBtCLR4xXU7H6Y7VhNLtjep/JHFF+QOyR/SsOzGpR/nMAm9WBwIseqVG3cNlFs= X-Received: by 2002:a05:6512:3449:b0:52c:dbf9:7e54 with SMTP id 2adb3069b0e04-52ce1836330mr9760672e87.41.1719504996879; Thu, 27 Jun 2024 09:16:36 -0700 (PDT) MIME-Version: 1.0 References: <451083be-83e8-413d-bc3a-ed7f3a6d99a9@gmail.com> In-Reply-To: <451083be-83e8-413d-bc3a-ed7f3a6d99a9@gmail.com> From: David Rowley Date: Fri, 28 Jun 2024 04:16:24 +1200 Message-ID: Subject: Re: A way to optimize sql about the last temporary-related row To: agharta82@gmail.com Cc: PostgreSQL General Content-Type: multipart/alternative; boundary="000000000000875f07061be1747d" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000875f07061be1747d Content-Type: text/plain; charset="UTF-8" On Fri, 28 Jun 2024, 3:20 am 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 > FOLLOWING) xx > from test_table > where integer_field_1 = 1 > and datetime_field_1 <= 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 = lt.xx) > > ) > > select * /* or count(*) */ from last_row_per_ids; > > > This query, on my PC, takes 46 seconds!!! > (Away from laptop and using my phone) Something like: select distinct on (integer_field_2) * from test_table where integer_field_1 = 1 and datetime_field_1 <= CURRENT_TIMESTAMP order by integer_field_2,datetime_field_1 desc; Might run a bit faster. However if it's slow due to I/O then maybe not much faster. Your version took about 5 seconds on my phone and my version ran in 1.5 seconds. It's difficult for me to check the results match with each query from my phone. A quick scan of the first 10 or so records looked good. If the updated query is still too slow on cold cache then faster disks might be needed. David > --000000000000875f07061be1747d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Fri, 28 Jun 2024, 3:20 am agharta82@gmail.com, <agharta82@gmail.com> wrote:
=C2=A0
Now the query:
explain (verbose, buffers, analyze)
with last_table_ids as materialized(
=C2=A0=C2=A0 select xx from (
=C2=A0=C2=A0 select LAST_VALUE(pk_id) over (partition by integer_field_2 or= der by
datetime_field_1 RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED
FOLLOWING) xx
=C2=A0=C2=A0 from test_table
=C2=A0=C2=A0 where integer_field_1 =3D 1
=C2=A0=C2=A0 and datetime_field_1 <=3D CURRENT_TIMESTAMP
=C2=A0=C2=A0 ) ww group by ww.xx

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

)

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


This query, on my PC, takes 46 seconds!!!

(Away from laptop and using my pho= ne)

Something like:

select distinct on (integer_= field_2) * from test_table where integer_field_1 =3D 1 and datetime_field_1= <=3D CURRENT_TIMESTAMP order by integer_field_2,datetime_field_1 desc;<= br>

Might run a bit fast= er.=C2=A0 However if it's slow due to I/O then maybe not much faster.= =C2=A0 Your version took about 5 seconds on my phone and my version ran in = 1.5 seconds.

It's di= fficult for me to check the results match with each query from my phone. A = quick scan of the first 10 or so records looked good.

If the updated query is still too slow on col= d cache then faster disks might be needed.

David
=
--000000000000875f07061be1747d--