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 1tPnCU-00FLA2-9t for pgsql-general@arkaria.postgresql.org; Mon, 23 Dec 2024 18:30:42 +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 1tPnCT-00EO8z-Jz for pgsql-general@arkaria.postgresql.org; Mon, 23 Dec 2024 18:30:41 +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 1tPnCT-00EO8r-6m for pgsql-general@lists.postgresql.org; Mon, 23 Dec 2024 18:30:41 +0000 Received: from mail-ot1-x32c.google.com ([2607:f8b0:4864:20::32c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tPnCQ-001GLR-1p for pgsql-general@postgresql.org; Mon, 23 Dec 2024 18:30:40 +0000 Received: by mail-ot1-x32c.google.com with SMTP id 46e09a7af769-71e157a79c8so922505a34.2 for ; Mon, 23 Dec 2024 10:30:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734978637; x=1735583437; darn=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=cnjLS3Q8onY4HWYbZIoGkV3oN51yfseQTPyI65zbURY=; b=Ymo729pZ2RJiJ+I0rDBgJRG//3WYYxd5/jKYfA+JUCDkY4QaXwRVuvW0TWBjTlkdLQ /UQEbMGaGvj3vTM6T0drCaKigfsplQNMPYbnc2WzjgLs27bu00ujSSmY8eLzp/AKGwBi A16k5Tf4SS8BsRuFLXJDBuPefyG2Dc3To46ZhB3n7ssTvRY11jCv5JdesR+O9elL2kkn QDX6ItYWtZ84SJ86VAEyBv/6e+4rFFK1l0QsR64TqrGfZEjGnoWoQ3K3FjQ/rnNuX4nv nlA1i7g1IOxxgocWUZO1S4CI33UIYj7n8rfA3GNNooTnUwOWEyn6FMyMg8PgClrAQ0Vr HgGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734978637; x=1735583437; 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=cnjLS3Q8onY4HWYbZIoGkV3oN51yfseQTPyI65zbURY=; b=Cv0nTy5maeAz7MUAuMvCdOdSbo1SBr3EOUGguN54qe/KMUVYWgyvwvoRkn4FxIxB5C jyFropFe/11BRNgDy6KyfYMcsABmBlUaK+ot+yLwHNuzvYZ4fJLO/E0EsfILE4DJmKXc 2EZTNNbqpN2T6E73HXw9b8beLv8FaBzWWHzpIUDDCVszki6Nm8d+bVwKiox1RtoSDav2 4ca/4H5icKQRP4f/SWiSt2dCsCvXosS9ia0qXM+JGTuQtRXHjdqrNOEaN0790/9TFxk4 DEZyCxh1XV6hzWbD13WE37VK9nuXSdxGRCL5QOLUAFNKrQdt/40wrlSTUCynpQh+ePQR zjJA== X-Gm-Message-State: AOJu0Yx9prkNlpgC2DGjXoIsqbYO5g+LtjGsB6DoEwG+XFon1vfURG6V Z+Uuyc/162rcCIord20Kxnfben1qLG0mst1yz2EPQSSDI+FtZeYBcriKsl//ZeCTW/xeF+jN7tn U2IELhdPcvf3fo5EdcEJA3cVDQgE= X-Gm-Gg: ASbGncuK6MBbyDraT9u9BTAo9XovC14oCXuAfJhNZBPzqXcv38LF4wqPUoVQxKY9CHG fgrQLI18fMWqEQuJkXPoPk+IBDb1bIdYZYlK7pGzn3CJ5l6iRleng587g6cBJLEtPonMXtB3q X-Google-Smtp-Source: AGHT+IGgFCr3Ez5RSMDDL+NV/cNziSGE8rN2Ww6jWq/2I+7+oBglzl6UNTMhagDZCwSFSOoP7Cezr6o0cuc/xY/vNrs= X-Received: by 2002:a05:6830:25c1:b0:71a:214b:4a00 with SMTP id 46e09a7af769-720ff8e8659mr9400778a34.28.1734978636710; Mon, 23 Dec 2024 10:30:36 -0800 (PST) MIME-Version: 1.0 References: <06e1f1ee-74b2-43a2-9a63-da20ae455ae2@aklaver.com> In-Reply-To: From: Divyansh Gupta JNsThMAudy Date: Tue, 24 Dec 2024 00:00:25 +0530 Message-ID: Subject: Re: Need help in database design To: Ron Johnson Cc: pgsql-general Content-Type: multipart/alternative; boundary="000000000000558bb60629f431ea" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000558bb60629f431ea Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Range partition can help when you applies filter for a specific range but in my case I need to apply filter on userid always, however I have date columns but there is less variation in timestamp which I have that's why didn't go for range partition. On Mon, 23 Dec 2024, 11:57=E2=80=AFpm Ron Johnson, wrote: > > 1. I bet you'd get better performance using RANGE partitioning. > 2. Twenty million rows per userid is a *LOT*. No subdivisions (like date > range)? > > On Mon, Dec 23, 2024 at 1:23=E2=80=AFPM Divyansh Gupta JNsThMAudy < > ag1567827@gmail.com> wrote: > >> Adrian, Please check this out; >> >> PARTITION BY HASH (userid); CREATE TABLE dbo.googledocs_tbl_clone_part_0 >> PARTITION OF dbo.googledocs_tbl_clone FOR VALUES WITH (modulus 84, >> remainder 0); ... CREATE TABLE dbo.googledocs_tbl_clone_part_83 PARTITIO= N >> OF dbo.googledocs_tbl_clone FOR VALUES WITH (modulus 84, remainder 83); >> >> On Mon, Dec 23, 2024 at 11:48=E2=80=AFPM Divyansh Gupta JNsThMAudy < >> ag1567827@gmail.com> wrote: >> >>> Adrian, the partition is on userid using hash partition with 84 >>> partitions >>> >>> Ron, there could be more than 20 Million records possible for a single >>> userid in that case if I create index on userid only not on other colum= n >>> the query is taking more than 30 seconds to return the results. >>> >>> On Mon, 23 Dec 2024, 11:40=E2=80=AFpm Ron Johnson, >>> wrote: >>> >>>> If your queries all reference userid, then you only need indices on >>>> gdid and userid. >>>> >>>> On Mon, Dec 23, 2024 at 12:49=E2=80=AFPM Divyansh Gupta JNsThMAudy < >>>> ag1567827@gmail.com> wrote: >>>> >>>>> I have one confusion with this design if I opt to create 50 columns I >>>>> need to create 50 index which will work with userid index in Bitmap o= n the >>>>> other hand if I create a JSONB column I need to create a single index= ? >>>>> >>>>> On Mon, 23 Dec 2024, 11:10=E2=80=AFpm Ron Johnson, >>>>> wrote: >>>>> >>>>>> Given what you just wrote, I'd stick with 50 separate t* columns. >>>>>> Simplifies queries, simplifies updates, and eliminates JSONB convers= ions. >>>>>> >>>>>> On Mon, Dec 23, 2024 at 12:29=E2=80=AFPM Divyansh Gupta JNsThMAudy < >>>>>> ag1567827@gmail.com> wrote: >>>>>> >>>>>>> Values can be updated based on customer actions >>>>>>> >>>>>>> All rows won't have all 50 key value pairs always if I make those >>>>>>> keys into columns the rows might have null value on the other hand = if it is >>>>>>> JSONB then the key value pair will not be there >>>>>>> >>>>>>> Yes in UI customers can search for the key value pairs >>>>>>> >>>>>>> During data population the key value pair will be empty array in >>>>>>> case of JSONB column or NULL in case of table columns, later when c= ustomer >>>>>>> performs some actions that time the key value pairs will populate a= nd >>>>>>> update, based on what action customer performs. >>>>>>> >>>>>>> On Mon, 23 Dec 2024, 10:51=E2=80=AFpm Divyansh Gupta JNsThMAudy, < >>>>>>> ag1567827@gmail.com> wrote: >>>>>>> >>>>>>>> Let's make it more understandable, here is the table schema with 5= 0 >>>>>>>> columns in it >>>>>>>> >>>>>>>> CREATE TABLE dbo.googledocs_tbl ( >>>>>>>> gdid int8 GENERATED BY DEFAULT AS IDENTITY( INCREMENT BY 1 MINVALU= E >>>>>>>> 1 MAXVALUE 9223372036854775807 START 1 CACHE 1 NO CYCLE) NOT NULL, >>>>>>>> userid int8 NOT NULL, >>>>>>>> t1 int4 NULL, >>>>>>>> t2 int4 NULL, >>>>>>>> t3 int4 NULL, >>>>>>>> t4 int4 NULL, >>>>>>>> t5 int4 NULL, >>>>>>>> t6 int4 NULL, >>>>>>>> t7 int4 NULL, >>>>>>>> t8 int4 NULL, >>>>>>>> t9 int4 NULL, >>>>>>>> t10 int4 NULL, >>>>>>>> t11 int4 NULL, >>>>>>>> t12 int4 NULL, >>>>>>>> t13 int4 NULL, >>>>>>>> t14 int4 NULL, >>>>>>>> t15 int4 NULL, >>>>>>>> t16 int4 NULL, >>>>>>>> t17 int4 NULL, >>>>>>>> t18 int4 NULL, >>>>>>>> t19 int4 NULL, >>>>>>>> t20 int4 NULL, >>>>>>>> t21 int4 NULL, >>>>>>>> t22 int4 NULL, >>>>>>>> t23 int4 NULL, >>>>>>>> t24 int4 NULL, >>>>>>>> t25 int4 NULL, >>>>>>>> t26 int4 NULL, >>>>>>>> t27 int4 NULL, >>>>>>>> t28 int4 NULL, >>>>>>>> t29 int4 NULL, >>>>>>>> t30 int4 NULL, >>>>>>>> t31 int4 NULL, >>>>>>>> t32 int4 NULL, >>>>>>>> t33 int4 NULL, >>>>>>>> t34 int4 NULL, >>>>>>>> t35 int4 NULL, >>>>>>>> t36 int4 NULL, >>>>>>>> t37 int4 NULL, >>>>>>>> t38 int4 NULL, >>>>>>>> t39 int4 NULL, >>>>>>>> t40 int4 NULL, >>>>>>>> t41 int4 NULL, >>>>>>>> t42 int4 NULL, >>>>>>>> t43 int4 NULL, >>>>>>>> t44 int4 NULL, >>>>>>>> t45 int4 NULL, >>>>>>>> t46 int4 NULL, >>>>>>>> t47 int4 NULL, >>>>>>>> t48 int4 NULL, >>>>>>>> t49 int4 NULL, >>>>>>>> t50 int4 NULL, >>>>>>>> CONSTRAINT googledocs_tbl_pkey PRIMARY KEY (gdid), >>>>>>>> ); >>>>>>>> >>>>>>>> Every time when i query I will query it along with userid >>>>>>>> Ex : where userid =3D 12345678 and t1 in (1,2,3) and t2 in (0,1,2) >>>>>>>> more key filters if customer applies >>>>>>>> >>>>>>>> On the other hand if I create a single jsonb column the schema wil= l >>>>>>>> look like : >>>>>>>> >>>>>>>> CREATE TABLE dbo.googledocs_tbl ( >>>>>>>> gdid int8 GENERATED BY DEFAULT AS IDENTITY( INCREMENT BY 1 MINVALU= E >>>>>>>> 1 MAXVALUE 9223372036854775807 START 1 CACHE 1 NO CYCLE) NOT NULL, >>>>>>>> userid int8 NOT NULL, >>>>>>>> addons_json jsonb default '{}'::jsonb >>>>>>>> CONSTRAINT googledocs_tbl_pkey PRIMARY KEY (gdid), >>>>>>>> ); >>>>>>>> >>>>>>>> and the query would be like >>>>>>>> where userid =3D 12345678 and ((addons_json @> {t1:1}) or >>>>>>>> (addons_json @> {t1:2}) or (addons_json @> {t1:3}) >>>>>>>> more key filters if customer applies >>>>>>>> >>>>>>>> >>>>>>>> >>>>>>>> On Mon, Dec 23, 2024 at 10:38=E2=80=AFPM David G. Johnston < >>>>>>>> david.g.johnston@gmail.com> wrote: >>>>>>>> >>>>>>>>> >>>>>>>>> >>>>>>>>> On Mon, Dec 23, 2024, 10:01 Divyansh Gupta JNsThMAudy < >>>>>>>>> ag1567827@gmail.com> wrote: >>>>>>>>> >>>>>>>>>> >>>>>>>>>> So here my question is considering one JSONB column is perfect o= r >>>>>>>>>> considering 50 columns will be more optimised. >>>>>>>>>> >>>>>>>>> The relational database engine is designed around the column-base= d >>>>>>>>> approach. Especially if the columns are generally unchanging, co= mbined >>>>>>>>> with using fixed-width data types. >>>>>>>>> >>>>>>>>> David J. >>>>>>>>> >>>>>>>>> >>>>>> >>>>>> -- >>>>>> Death to , and butter sauce. >>>>>> Don't boil me, I'm still alive. >>>>>> lobster! >>>>>> >>>>> >>>> >>>> -- >>>> Death to , and butter sauce. >>>> Don't boil me, I'm still alive. >>>> lobster! >>>> >>> > > -- > Death to , and butter sauce. > Don't boil me, I'm still alive. > lobster! > --000000000000558bb60629f431ea Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Range partition can help when you applies filter for a speci= fic range but in my case I need to apply filter on userid always, however I= have date columns but there is less variation in timestamp which I have th= at's why didn't go for range partition.


On Mon, 23 Dec 2024, 11:57=E2=80=AFpm Ron Johnson, <ronljohnsonjr@gmail.com> wrot= e:

1. I bet you'd get better performance using RANGE part= itioning.
2. Twenty million rows per userid=C2=A0is a LOT.= =C2=A0 No subdivisions (like date range)?

On Mon, Dec 23, 2024 at 1:23=E2=80= =AFPM Divyansh Gupta JNsThMAudy <ag1567827@gmail.com> wrote:
=
Ad= rian, Please check this out;

PARTITION BY HASH (userid); = CREATE TABLE dbo.googledocs_tbl_clone_part_0 PARTITION OF dbo.googledocs_tbl_clone FOR VALUES WITH (modulus 84, remainder 0); ... = CREATE TABLE dbo.googledocs_tbl_clone_part_83 PARTITION OF dbo.googledocs_tbl_clone FOR VALUES WITH (modulus 84, remainder 83);

On Mon, Dec 23, 2024 at 11:48=E2=80=AFPM D= ivyansh Gupta JNsThMAudy <ag1567827@gmail.com> wrote:
=

Adrian, th= e partition is on userid using hash partition with 84 partitions

Ron, there could be more than 20 Million records possible fo= r a single userid in that case if I create index on userid only not on othe= r column the query is taking more than 30 seconds to return the results.

On Mon= , 23 Dec 2024, 11:40=E2=80=AFpm Ron Johnson, <ronljohnsonjr@gmail.c= om> wrote:
If your queries all reference userid, then you only= need indices on gdid and userid.

On Mon, Dec 23, 2024 at 12:49=E2=80=AFPM D= ivyansh Gupta JNsThMAudy <ag1567827@gmail.com> wrote:=

= I have one confusion with this design if I opt to create 50 columns I need = to create 50 index which will work with userid index in Bitmap on the other= hand if I create a JSONB column I need to create a single index ?


On Mon= , 23 Dec 2024, 11:10=E2=80=AFpm Ron Johnson, <ronljohnso= njr@gmail.com> wrote:
Given what you just wrote, I'd stick= with 50 separate t* columns.=C2=A0 Simplifies queries, simplifies updates,= and eliminates JSONB conversions.

On Mon, Dec 23, 2024 at 12:29=E2=80=AFPM = Divyansh Gupta JNsThMAudy <ag1567827@gmail.com> wrote:

Values can be updated based on customer actions

All rows won't have all 50 key value pairs always if I m= ake those keys into columns the rows might have null value on the other han= d if it is JSONB then the key value pair will not be there

Yes in UI customers can search for the key value pairs

During data population the key value pair will be empty arra= y in case of JSONB column or NULL in case of table columns, later when cust= omer performs some actions that time the key value pairs will populate and = update, based on what action customer performs.


Let's make it more un= derstandable, here is the table schema with 50 columns in it=C2=A0

C= REATE TABLE dbo.googledocs_tbl (
gdid int8 GENERATED BY DEFAULT AS IDEN= TITY( INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 1 CACHE = 1 NO CYCLE) NOT NULL,
userid int8 NOT NULL,
t1 int4 NULL,
t2 in= t4 NULL,
t3 int4 NULL,
t4 int4 NULL,
t5 int4 NULL,
t6 int4 = NULL,
t7 int4 NULL,
t8 int4 NULL,
t9 int4 NULL,
t10 int4 NU= LL,
t11 int4 NULL,
t12 int4 NULL,
t13 int4 NULL,
t14 int4 N= ULL,
t15 int4 NULL,
t16 int4 NULL,
t17 int4 NULL,
t18 int4 = NULL,
t19 int4 NULL,
t20 int4 NULL,
t21 int4 NULL,
t22 int4= NULL,
t23 int4 NULL,
t24 int4 NULL,
t25 int4 NULL,
t26 int= 4 NULL,
t27 int4 NULL,
t28 int4 NULL,
t29 int4 NULL,
t30 in= t4 NULL,
t31 int4 NULL,
t32 int4 NULL,
t33 int4 NULL,
t34 i= nt4 NULL,
t35 int4 NULL,
t36 int4 NULL,
t37 int4 NULL,
t38 = int4 NULL,
t39 int4 NULL,
t40 int4 NULL,
t41 int4 NULL,
t42= int4 NULL,
t43 int4 NULL,
t44 int4 NULL,
t45 int4 NULL,
t4= 6 int4 NULL,
t47 int4 NULL,
t48 int4 NULL,
t49 int4 NULL,
t= 50 int4 NULL,
CONSTRAINT googledocs_tbl_pkey PRIMARY KEY (gdid),
);<= br>
Every time when i query I will query it along with userid=C2=A0
E= x : where userid =3D 12345678 and t1 in (1,2,3) and t2 in (0,1,2)
more k= ey filters if customer applies=C2=A0

On the other hand if I create a= single jsonb column the schema will look like :

CREATE TABLE dbo.go= ogledocs_tbl (
gdid int8 GENERATED BY DEFAULT AS IDENTITY( INCREMENT BY= 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 1 CACHE 1 NO CYCLE) NOT NU= LL,
userid int8 NOT NULL,
addons_json jsonb default '{}'::j= sonb
CONSTRAINT googledocs_tbl_pkey PRIMARY KEY (gdid),
);

an= d the query would be like=C2=A0
where userid =3D 12345678 and ((addons_j= son=C2=A0@> {t1:1}) or=C2=A0 (addons_json=C2=A0@>= ; {t1:2}) or=C2=A0 (addons_json=C2=A0@>= ; {t1:3})
more key filters if customer applies=C2=A0



On M= on, Dec 23, 2024 at 10:38=E2=80=AFPM David G. Johnston <david.g.johnston@gmail.com> wrote:

On Mon, Dec 23, 2024, 10:01 Divyansh Gupta JNsThMAudy <ag1567827@gmail.com> wrote:


<= /p>

So here my question is considering one JSONB column is pe= rfect or considering 50 columns will be more optimised.

The relational database engine is designed aroun= d the column-based approach.=C2=A0 Especially if the columns are generally = unchanging, combined with using fixed-width data types.

David J.

<= div class=3D"gmail_quote" dir=3D"auto">


--
Death to <Redacted>, and butter sauce.Don't boil me, I'm still alive.
<Redacted> lobs= ter!


--
Death to <Redacted>, and butter sauce.Don't boil me, I'm still alive.
<Redacted> lobs= ter!


--
Death to <Redacted>, and butter sauce.Don't boil me, I'm still alive.
<Redacted> lobs= ter!
--000000000000558bb60629f431ea--