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 1sMr2T-0054Sp-L6 for pgsql-general@arkaria.postgresql.org; Thu, 27 Jun 2024 15:27:57 +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 1sMr2S-00B0ax-1d for pgsql-general@arkaria.postgresql.org; Thu, 27 Jun 2024 15:27:56 +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 1sMr2R-00B0ap-MQ for pgsql-general@lists.postgresql.org; Thu, 27 Jun 2024 15:27:56 +0000 Received: from mail-ot1-x329.google.com ([2607:f8b0:4864:20::329]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sMr2P-003qL5-O4 for pgsql-general@lists.postgresql.org; Thu, 27 Jun 2024 15:27:55 +0000 Received: by mail-ot1-x329.google.com with SMTP id 46e09a7af769-700ca6171f1so1527245a34.2 for ; Thu, 27 Jun 2024 08:27:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719502072; x=1720106872; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=5Hnsvn8Y09l0EzZ5lzwVeDCVPkXtglIvTO6q2zZMT1U=; b=WaaM0e2KfP+jXsUTFK7mLQQila+A9f58Pw/q7w+3sGac6wY+32VsFQ+17sIEPwxlPB bNOQbf2W+4vsM8Ugh3VhvhZ7NS6alfQa+m4TirZmYK2ipXqOVm+lnyKG9SAYwCNogkgp EjcRqXZZuCYPpqQ0MHyiGn+wiIVrfMwiVbXSpRf4P/5Efkxw9J2zeaul7stv+y/jqCYm FU3vMX4nzaorGWhimXk6wZiL82qdiursp86Aym5/RxiYcc4poFuh1Boyi6ZqNUQwQej8 dFtaYbYJH/bbfkxy3g5f+yRr+FKCOp8/x95NxS0BKJvfEAVWiLqJUmfZkoTx/XFewv5q LEkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719502072; x=1720106872; h=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=5Hnsvn8Y09l0EzZ5lzwVeDCVPkXtglIvTO6q2zZMT1U=; b=bu9ReYOiKnt9H+oS6QPYZTJpK/Xa3HNjfq4hbyVbHBle6EQAtNvebkIvyFwa+W3KYK 1T5pYvCHhpRJxwfrJ/J+/imhhtL7TA2aIsPCy++tNjxg+5MnxWtw9CLCwgAb4FAHY1d5 zba4yDL0WJ5MymdR+M27+9FongJtWQDlYYuL1tF05nsSPryNT9Xol0rdyr8zc/nWD99+ AwZ1MuEb7U/SrcHSQUkyODmrD51R0NaBn+SqLbRDQwp0zQ+82tfrsNlVh3uj4AFo64e9 TA0tU/pzYZVN1QA9KuBlk0XJ1H+Y0H7ssJtz5QgVc53Lf0Bhtr5IFs7S9rxAKpK5WyJG mJvQ== X-Gm-Message-State: AOJu0YyE+WPkV0R4P11konfAeiE5BoK83XdTIbg1USnysPDimSJOPL5G hheDPC/48cUexVgqVMkWk1XprBap3lCGVtT55l27Oe80ewJ4Bv2bxrLRCy3H1RIkMTqCrzs1SGg 3JOEQiweoSPzvw7z9ghIRhrtB8fuD1g== X-Google-Smtp-Source: AGHT+IE401nklpVxXjLh0Uox/aXLcXygnHO4vwxOlw+xrxSSNR8kG2FAqXgbufDrfIyaaBUsJeZqU0Y5q29QRbuEOOE= X-Received: by 2002:a05:6870:c110:b0:254:c397:8127 with SMTP id 586e51a60fabf-25d06efd538mr13314406fac.58.1719502071668; Thu, 27 Jun 2024 08:27:51 -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: Ron Johnson Date: Thu, 27 Jun 2024 11:27:40 -0400 Message-ID: Subject: Re: A way to optimize sql about the last temporary-related row To: "pgsql-generallists.postgresql.org" Content-Type: multipart/alternative; boundary="0000000000002c27fb061be0c6ae" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000002c27fb061be0c6ae Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jun 27, 2024 at 11:20=E2=80=AFAM 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 =3D 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 =3D floor(random() * (6-1+1) + 1)::int, > integer_field_2 =3D 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. --0000000000002c27fb061be0c6ae Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Thu, Jun 27, 2024 at 11:20=E2=80=AFAM = agharta82@gmail.com <agharta82@gmail.com> wrote:
[snip]=C2=A0
-- 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 =3D timestamp '2000-01-01 00:00:00' + random() * <= br> (timestamp '2024-05-31 23:59:59' - timestamp '2000-01-01 00:00:= 00'),
integer_field_1 =3D floor(random() * (6-1+1) + 1)::int,
integer_field_2 =3D 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 vacu= uming before creating indices.
=C2=A0
--0000000000002c27fb061be0c6ae--