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 1sMrPf-0056kW-Jk for pgsql-general@arkaria.postgresql.org; Thu, 27 Jun 2024 15:51:55 +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 1sMrPd-00BRwg-So for pgsql-general@arkaria.postgresql.org; Thu, 27 Jun 2024 15:51:54 +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 1sMrPd-00BRwX-De for pgsql-general@lists.postgresql.org; Thu, 27 Jun 2024 15:51:53 +0000 Received: from mail-lf1-x135.google.com ([2a00:1450:4864:20::135]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sMrPW-003PLb-Jq for pgsql-general@lists.postgresql.org; Thu, 27 Jun 2024 15:51:52 +0000 Received: by mail-lf1-x135.google.com with SMTP id 2adb3069b0e04-52cd628f21cso7485443e87.3 for ; Thu, 27 Jun 2024 08:51:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719503504; x=1720108304; 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=KYqHSboXrsOZukMLMncZ7KPpmd/dnk5raqd6j9/LuBg=; b=Bxoh8YSOOQNPYyrjyhntnljPL364168pyacEGbHpCcBlavSLoEpMy9FBbGcOrTDRfZ y54F0s5xdJ5TbmKf4httIuLma8lAgIaHYv8Zro0e4lETocSr/qqkKrIIi9fyZit8SIni kM/+MQD9/bh4bMPtpyBfb6XtK+wVpGMgeskS/ZZGFOvC4v3c7YkTD13AxRdtkJ39sEbW 5pFyG3AOCdhY6wv9mqXEhc6UzLYFW96sG2LieZgiPJOpuuXuMCGeqiDACnrajIbjmHTU fOcB2UIrxzUEHVIL4TCd1ihBFM+RqwfWC6ogo14PfLFKlpcZYp9dp8Y5UaKc6WEhvAjq zd8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719503504; x=1720108304; 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=KYqHSboXrsOZukMLMncZ7KPpmd/dnk5raqd6j9/LuBg=; b=ayYMwBjS66njadWp02KVwAS1ksEejc6sP/yzpq4C5fH6I5bMxiXtvtmxtRXQMK7kuD mUG+2vS8dbNviZ4k3WMJeHs6+vYkOGKNtU6ZNS5Y8sE690nuMKD76OnrDrCtvxZj4cMM KqzOceZTZdIURySGb8IeHok0yydk0QS3lt8q8smRWmi5VRV+7vn/WTvMVhfZFrF7nu2w 0Ni5/9h/7s5jVq25iRf8GVCLHcCrOqGWCjGtW29fOb/2mezrVHpllG0Wk65wdiIK6GtS 7l9Ba5IdxdNVVe6oi7MMbI7UJ0qnxw8cYa5JpdVUuo5P1LyTj7G+v+dx9Sejcx+PTKkq euzQ== X-Gm-Message-State: AOJu0YxnPjRt+iaEuXc7lvafJQrzKaRE/6Zy0jpz4gf/FnaPSsCsm47L X2yIHBT6bVxkxBNHsiVVwu+uTAgZ3aUUfZhgrVsPyLmE8uCOeXc= X-Google-Smtp-Source: AGHT+IGRB6p+G8yLC1j0mHXx2Xv+dF/OX5U5cXu4RefEKUnPsbGYw6jihRhNul9xE/34reaNlprPMw== X-Received: by 2002:ac2:4c86:0:b0:52c:dba6:b4cb with SMTP id 2adb3069b0e04-52ce1832bcdmr8989218e87.13.1719503503424; Thu, 27 Jun 2024 08:51:43 -0700 (PDT) Received: from [192.168.1.80] ([2.229.48.88]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-424c8280a87sm73746365e9.24.2024.06.27.08.51.42 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 27 Jun 2024 08:51:43 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------2rAnYRD29yT8hvgP830F9dHC" Message-ID: Date: Thu, 27 Jun 2024 17:51:42 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: A way to optimize sql about the last temporary-related row To: "David G. Johnston" Cc: "pgsql-general@lists.postgresql.org" 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. --------------2rAnYRD29yT8hvgP830F9dHC Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi, Thanks for you reply. About syntax you're right, but I couldn't think of anything better :(((  I'm here for that too, to solve the problem in a fancy way, with your great support. In practice, I need to get back a dataset with the last association (the most datatime recent record) for all the distinct entries of integer_field_2 based on filter:  integer_field_1 = 1 As said in another reply, the query needs to be performant even if data is not in cache (systemctl stop postgresql-16 && sync && echo 3 > /proc/sys/vm/drop_caches  && systemctl start postgresql-16). Many thanks for your support. Agharta Il 27/06/24 5:33 PM, David G. Johnston ha scritto: > 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 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; > > > 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’m going by the general query form and the “last row” aspect of the > question.  I haven’t gone and confirmed your specific query can > benefit from this approach. The window expression does give me pause. > > David J. > --------------2rAnYRD29yT8hvgP830F9dHC Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

Hi,

Thanks for you reply.

About syntax you're right, but I couldn't think of anything better :(((  I'm here for that too, to solve the problem in a fancy way, with your great support.

In practice, I need to get back a dataset with the last association (the most datatime recent record) for all the distinct entries of integer_field_2 based on filter:  integer_field_1 = 1

As said in another reply, the query needs to be performant even if data is not in cache (systemctl stop postgresql-16 && sync && echo 3 > /proc/sys/vm/drop_caches  &&  systemctl start postgresql-16).

Many thanks for your support.

Agharta









Il 27/06/24 5:33 PM, David G. Johnston ha scritto:
On Thursday, June 27, 2024, 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;


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’m going by the general query form and the “last row” aspect of the question.  I haven’t gone and confirmed your specific query can benefit from this approach. The window expression does give me pause.

David J.

--------------2rAnYRD29yT8hvgP830F9dHC--