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 1sN5uN-006kaV-Pw for pgsql-general@arkaria.postgresql.org; Fri, 28 Jun 2024 07:20:35 +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 1sN5uL-001yre-1E for pgsql-general@arkaria.postgresql.org; Fri, 28 Jun 2024 07:20:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sN5uK-001yrW-Jo for pgsql-general@lists.postgresql.org; Fri, 28 Jun 2024 07:20:33 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sN5uI-003xA1-HO for pgsql-general@lists.postgresql.org; Fri, 28 Jun 2024 07:20:32 +0000 Received: by mail-wm1-x32e.google.com with SMTP id 5b1f17b1804b1-42122ac2f38so1594735e9.1 for ; Fri, 28 Jun 2024 00:20:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719559229; x=1720164029; darn=lists.postgresql.org; h=in-reply-to:disposition-notification-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=679LMcGKfrlqEyRKYh6M9T96CLhYeazFI4o4eVqfIGM=; b=Ic/lSA46343K7J+krPhrz9kGjHLUEX7hhB56NhFxpKGuDw7/z6oDt3mq4OY2foFXuu +qjD4G4NX+eYs/IfoM9wmpzTDrz5o53XMC/iNyS5ohn6jSYkP6UNu0Xek8aplztOwaee GyM6RkSFv6NYdx++g+bCwyIccub1qlB3u9HC3xovdeqzL3qFunM3oUUXOSbOqWEfFh5m tqXiG/V+Oqt7/l9LU+jsM8TyNw5JHSNq+u1ZA06OTDHnh4pA8vgN5M7o2De0tjJHqduV KP3KIh5C3qYT8CANzlId9SnkOnyxiZowgWzj6ARzmFHzUFwUwTDq5BQCNyeiHSjNS7/q L75A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719559229; x=1720164029; h=in-reply-to:disposition-notification-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=679LMcGKfrlqEyRKYh6M9T96CLhYeazFI4o4eVqfIGM=; b=Apx3sdfz1ii3j7O2f4VHKwixBO7oDG1twbUIIwWNKrUVT8eXQf9UV9c8EUTeTF6hH6 c+mmw34qtgmFT0Vt5Wkd2V0++HvohsUxy4HgIhTKluHn2TtuHo+TO9/4F37ikUhAveIA txKfqp5CmOMbpvDUEmDQZJ7bLqvxj+YR2pH3u++MKAq5JzbtXfEC1EazV8K6mBMGvmIq Ug0DJHuNlVmiZJR++xusLuACSfW8dQv03ElUavX5ZVi+3iyeEX5u84Rt2bnzpvTrXnR0 paon3fz9IAGHxm7CMjXsPbn53CHaBYETDyHfAm7hLJ7c6w6n2iUHIOivSIQFe8dbsK9m wXQQ== X-Gm-Message-State: AOJu0Yy8t0hEwEnPOyuWiyABQ2C6vWwEBQ1FDJFeExI7rhboy3N7EoSv 1AApznmJXRnjMftJT6V+mTEuReLelqfReUOv2rWt1uGqy14mEDs9x+kv X-Google-Smtp-Source: AGHT+IHXtLnkXhzT8JXwftO1lf+z+ntXK4R1mOBWbDdV2ULJQ1PWq8j85e7E2zRzyor9GAi57Rariw== X-Received: by 2002:a05:600c:3510:b0:421:805f:ab3c with SMTP id 5b1f17b1804b1-4256d569dbcmr7094545e9.14.1719559228852; Fri, 28 Jun 2024 00:20:28 -0700 (PDT) Received: from [192.168.1.80] ([2.229.48.88]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4256af55d0fsm21917435e9.19.2024.06.28.00.20.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 28 Jun 2024 00:20:28 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------O8cVNOXxNnH1DZL4zHaN45l1" Message-ID: <84196842-d6bc-4813-8740-48acc227a1dc@gmail.com> Date: Fri, 28 Jun 2024 09:20:26 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: A way to optimize sql about the last temporary-related row To: David Rowley Cc: PostgreSQL General References: <451083be-83e8-413d-bc3a-ed7f3a6d99a9@gmail.com> Content-Language: it From: "agharta82@gmail.com" Disposition-Notification-To: "agharta82@gmail.com" In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------O8cVNOXxNnH1DZL4zHaN45l1 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit HOO-HA! This is HUGE! Only 2.2 seconds on my data!!!! Amazing! distinct on (field) *followed by "*" *is a hidden gem! Thank you so much and thanks to everyone who helped me!  Thank you very much!! Cheers, Agharta Il 27/06/24 6:16 PM, David Rowley ha scritto: > > > 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 > --------------O8cVNOXxNnH1DZL4zHaN45l1 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

HOO-HA! This is HUGE!

Only 2.2 seconds on my data!!!! Amazing!

distinct on (field) followed by "*" is a hidden gem!

Thank you so much and thanks to everyone who helped me!  Thank you very much!!

Cheers,

Agharta

 


Il 27/06/24 6:16 PM, David Rowley ha scritto:


On Fri, 28 Jun 2024, 3:20 am agharta82@gmail.com, <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
--------------O8cVNOXxNnH1DZL4zHaN45l1--