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 1sMr7g-00551U-Ll for pgsql-general@arkaria.postgresql.org; Thu, 27 Jun 2024 15:33:20 +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 1sMr7f-00B7Df-0C for pgsql-general@arkaria.postgresql.org; Thu, 27 Jun 2024 15:33:19 +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 1sMr7e-00B7DX-Kz for pgsql-general@lists.postgresql.org; Thu, 27 Jun 2024 15:33:18 +0000 Received: from mail-lf1-x12c.google.com ([2a00:1450:4864:20::12c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sMr7c-003qNM-Jn for pgsql-general@lists.postgresql.org; Thu, 27 Jun 2024 15:33:18 +0000 Received: by mail-lf1-x12c.google.com with SMTP id 2adb3069b0e04-52cd8897c73so7217775e87.2 for ; Thu, 27 Jun 2024 08:33:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719502395; x=1720107195; darn=lists.postgresql.org; h=in-reply-to:disposition-notification-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=68/e2jOs2FwzEwTvrNdTxsPvqQn42w26vJgmsiwP8yY=; b=a7K4VClPrAEC7mkPlOyRybGEGNEoUjrNCXaWR/byPURIUMkzJzoPmHV6w+EceDXQmi db2r/LWUyxm2z3K6Q+HuzBglFr4wRKJfJLPrlglrqZWBCLrg1C51U2D52ay78dtuCl+H koS844IKhTlwDgTD1I1nSAPgSER5t7PyALBjIgaqLRXhJWXdVifN7MYzSmJ+tY+SL2Ld oepgb6OoaOkqbCAobUTHVHd4MAtQDpKB0IJpwWjg/WPIjStW0ctI7Dmuxpn8oHUZaVfZ 5DGQLn+3KsxavQbjpB9QHRPQzgTqpp20SPJ7FX1t9Dtjti2WEjDsdpyk0AAlMLek/lMw blWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719502395; x=1720107195; h=in-reply-to:disposition-notification-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=68/e2jOs2FwzEwTvrNdTxsPvqQn42w26vJgmsiwP8yY=; b=nrkzPpYko/A+NT9l0WfD/J545N3LEtNSAiV9SXB4H3PqDSJ3MVim3FUa1Irbktqo+m jPHUHDgZCDJiPOvkw3/BpeacyKfhV9GlnfASpst676WpI//OiKntXx2U+6ehdBeT0xDQ 9y5Rp1rzRdvNJFUl5MtwYdB9mMDpgWjdSwwhih5QDfC9SPmMHpq09GfKtXup9yfXb/TN jbMlVhWZNctaKIYN42bciELqAYc4P4c/pxaWzC+viw+bSxk8osr+71yayAFrYd+6Wc86 zqPSBTFay79DkEZH+4BdF/OCViwUwMyTU+oI/cTNo2cAfwc1YBAxeEW/yyHTEUy4xo8a +62A== X-Gm-Message-State: AOJu0YwPNniAAQm+3RfkivJwm41AwQ2ywHeVTf5tp32jPzuz9Obmxkm/ dya36D1j6Ht877UeSJ9pQCmnBBDgtCZibzAlAw37QulBW8pIkEFp6dKz X-Google-Smtp-Source: AGHT+IGGHE0Z8aa/yFWB3kJCMLUC3gS9dnBiaXkkCX7yA9AQDvla1nhJD409usHPj/Ta2kMNhkcl8g== X-Received: by 2002:a05:6512:688:b0:52c:9d31:3f25 with SMTP id 2adb3069b0e04-52ce185f6edmr10268178e87.43.1719502394616; Thu, 27 Jun 2024 08:33:14 -0700 (PDT) Received: from [192.168.1.80] ([2.229.48.88]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3674369ecdesm2217580f8f.108.2024.06.27.08.33.13 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 27 Jun 2024 08:33:13 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------hKjdAcm0W03a3ez6wGObpgzi" Message-ID: Date: Thu, 27 Jun 2024 17:33:13 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: A way to optimize sql about the last temporary-related row To: 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. --------------hKjdAcm0W03a3ez6wGObpgzi Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Hi, You are right. Too quickly copy-paste on my part :-) I take this opportunity to add a NOT insignificant detail. Before executing the select query I clear the cache: systemctl stop postgresql-16 && sync && echo 3 > /proc/sys/vm/drop_caches  &&  systemctl start postgresql-16 I need to get a performance result even if data is not in cache. My best regards, Agharta Il 27/06/24 5:27 PM, Ron Johnson ha scritto: > On Thu, Jun 27, 2024 at 11:20 AM agharta82@gmail.com > wrote: > [snip] > > -- insert 4M records > insert into test_table(pk_id) select generate_series(1,4000000,1); > > -- now set some random data, distribuited between specific ranges > (as in > my production table) > update test_table set > datetime_field_1 = timestamp '2000-01-01 00:00:00' + random() * > (timestamp '2024-05-31 23:59:59' - timestamp '2000-01-01 00:00:00'), > integer_field_1 = floor(random() * (6-1+1) + 1)::int, > integer_field_2 = floor(random() * (200000-1+1) + 1)::int; > > > -- indexes > CREATE INDEX idx_test_table_integer_field_1 ON > test_table(integer_field_1); > CREATE INDEX xtest_table_datetime_field_1 ON > test_table(datetime_field_1 > desc); > CREATE INDEX idx_test_table_integer_field_2 ON > test_table(integer_field_2); > > > Off-topic: save some resources by vacuuming before creating indices. > --------------hKjdAcm0W03a3ez6wGObpgzi Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

Hi,

You are right. Too quickly copy-paste on my part :-)

I take this opportunity to add a NOT insignificant detail.

Before executing the select query I clear the cache:

systemctl stop postgresql-16 && sync && echo 3 > /proc/sys/vm/drop_caches  &&  systemctl start postgresql-16

I need to get a performance result even if data is not in cache.


My best regards,

Agharta


Il 27/06/24 5:27 PM, Ron Johnson ha scritto:
On Thu, Jun 27, 2024 at 11:20 AM agharta82@gmail.com <agharta82@gmail.com> wrote:
[snip] 
-- insert 4M records
insert into test_table(pk_id) select generate_series(1,4000000,1);

-- now set some random data, distribuited between specific ranges (as in
my production table)
update test_table set
datetime_field_1 = timestamp '2000-01-01 00:00:00' + random() *
(timestamp '2024-05-31 23:59:59' - timestamp '2000-01-01 00:00:00'),
integer_field_1 = floor(random() * (6-1+1) + 1)::int,
integer_field_2 = floor(random() * (200000-1+1) + 1)::int;


-- indexes
CREATE INDEX idx_test_table_integer_field_1 ON test_table(integer_field_1);
CREATE INDEX xtest_table_datetime_field_1 ON test_table(datetime_field_1
desc);
CREATE INDEX idx_test_table_integer_field_2 ON test_table(integer_field_2);


Off-topic: save some resources by vacuuming before creating indices.
 
--------------hKjdAcm0W03a3ez6wGObpgzi--