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 1tPn5v-00FKD4-Ig for pgsql-general@arkaria.postgresql.org; Mon, 23 Dec 2024 18:23:56 +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 1tPn5u-00EA3L-O3 for pgsql-general@arkaria.postgresql.org; Mon, 23 Dec 2024 18:23:54 +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 1tPn5u-00EA3C-9y for pgsql-general@lists.postgresql.org; Mon, 23 Dec 2024 18:23:54 +0000 Received: from mail-oo1-xc34.google.com ([2607:f8b0:4864:20::c34]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tPn5r-001GGX-2z for pgsql-general@postgresql.org; Mon, 23 Dec 2024 18:23:53 +0000 Received: by mail-oo1-xc34.google.com with SMTP id 006d021491bc7-5f32f5b0631so2967674eaf.3 for ; Mon, 23 Dec 2024 10:23:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734978229; x=1735583029; 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=YGsCkigjRXPfj6vAZ826g/wofEknbPxGA0MaCQ5HPgU=; b=P5eWHg/zpwWFdbUeKz0F1ZR/4xh5lFYvj00hvl81i5P3rEFeQGdF990I4qmBByhclc LvLkKjZMkxbPXEgUZxoMlAHVenhs+Wvn6NfyHrD0CpYRNGNYjhCDg3e0W6CJNZn5UIWm 99rboX3KojDAj6wDdjFou4Y9bU8R6X6iFPY5HVgnD8aF1leuFHhI3cZor41buwHmgYmu feSScY/+Om8qqwErvrFIG5oTkOWwKslGo3ry4UsqvO360gj6nJ9DZSX/tf9TC8c0Xj52 ZYSFew9zECuBLbHmDyOK1xxX58Jwxg9DdB8d/j5y04KRQ7bLG2drdvZB59/evJpHDZPu 4G7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734978229; x=1735583029; 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=YGsCkigjRXPfj6vAZ826g/wofEknbPxGA0MaCQ5HPgU=; b=H8ystWJGGS4A3e5FBy8xP0ZXBEz4lOXa/2/gZZpMbgDLDUwlwoDwzuyuZQ6ekCB6+2 OAyuHG/17yPHySXgjhSbiyNMTH9FHrMdfBvOp+nylnqWd3o3Ki+uZIqoJM0RWfMt9D6J 3t8LmF3zhlDfZd175dHtnIcNg0t7F0tHNW+vfwwdqhuuF2VdFThe84Zj/JAZ8TXOdw4R 4TC9o2dPQEU1X3Qd6MIg98fNYs5So65BDDM0lM9awXcBtIaHHZB8z1zyD61jNHPUMc15 dNdHrXtVa03dLu8Va0Wg+NlD8OFrVeUMRKlDniT4QqWkj4hTrAW7RXi6rQC7fZW2FvBl yfWg== X-Gm-Message-State: AOJu0YxSORX6v4RDexMyG8kfKTMWH66m7959lu0ov7MD6qtMKrWiyalA 2/Y6ovYk8fSU5k1RJ2it69YAiHbfqg975PPZeYwpNXpHbPsVj61b1l/S4yZejJYXslX5pF8UAy+ laiH+4frbyvhsoHe9B30RkYbqJyo= X-Gm-Gg: ASbGncvO2higg3p0A06P8kIa2WVZ7im290voDt6oMIIyhGPdia3QtlC4LElnCIvFKEB 7AFfWlwlWjiO9lWj+6UXutn/4/V1GMWjDKj8PliKsTfM1/HWyULe3O7cPFc35vcKq4Y0ETcw= X-Google-Smtp-Source: AGHT+IFIDXDaPQTPlV+amwzjl2ob2PEQFijOz9evtZt9sS+4JzrB6H2XDvDGMyaQlsAqGOwb+tALxUf/BwpdKnGHNG8= X-Received: by 2002:a05:6820:988:b0:5f4:ca80:5a3 with SMTP id 006d021491bc7-5f62e78e51emr7750017eaf.5.1734978229431; Mon, 23 Dec 2024 10:23:49 -0800 (PST) MIME-Version: 1.0 References: <06e1f1ee-74b2-43a2-9a63-da20ae455ae2@aklaver.com> In-Reply-To: From: Divyansh Gupta JNsThMAudy Date: Mon, 23 Dec 2024 23:53:36 +0530 Message-ID: Subject: Re: Need help in database design To: Ron Johnson Cc: pgsql-general Content-Type: multipart/alternative; boundary="0000000000000ef3340629f41949" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000000ef3340629f41949 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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, remainde= r 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 Divyansh Gupta JNsThMAudy < ag1567827@gmail.com> wrote: > Adrian, the partition is on userid using hash partition with 84 partition= s > > 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 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, > 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 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, >>> wrote: >>> >>>> Given what you just wrote, I'd stick with 50 separate t* columns. >>>> Simplifies queries, simplifies updates, and eliminates JSONB conversio= ns. >>>> >>>> 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 key= s >>>>> into columns the rows might have null value on the other hand if it i= s >>>>> 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 customer >>>>> performs some actions that time the key value pairs will populate and >>>>> 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 50 >>>>>> columns in it >>>>>> >>>>>> CREATE TABLE dbo.googledocs_tbl ( >>>>>> gdid int8 GENERATED BY DEFAULT AS IDENTITY( INCREMENT BY 1 MINVALUE = 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 will >>>>>> look like : >>>>>> >>>>>> CREATE TABLE dbo.googledocs_tbl ( >>>>>> gdid int8 GENERATED BY DEFAULT AS IDENTITY( INCREMENT BY 1 MINVALUE = 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_j= son @> >>>>>> {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 or >>>>>>>> considering 50 columns will be more optimised. >>>>>>>> >>>>>>> The relational database engine is designed around the column-based >>>>>>> approach. Especially if the columns are generally unchanging, comb= ined >>>>>>> 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! >> > --0000000000000ef3340629f41949 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Adrian, Please check this out;

PARTITION BY HASH (use= rid); CREATE TABLE dbo.go= ogledocs_tbl_clone_part_0 PARTITION OF dbo.googledocs_tbl_clone FOR VALUES WITH (modulus 84, = remainder 0); ... CREATE TABLE dbo.go= ogledocs_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 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 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.com> wrote:
=
If your queries all reference userid, then you only need indices on gd= id and userid.

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

I have one confusion with t= his design if I opt to create 50 columns I need to create 50 index which wi= ll work with userid index in Bitmap on the other hand if I create a JSONB c= olumn I need to create a single index ?


On Mon= , 23 Dec 2024, 11:10=E2=80=AFpm Ron Johnson, <ronljohnsonjr@gmail.c= om> wrote:
Given what you just wrote, I'd stick with 50 se= parate t* columns.=C2=A0 Simplifies queries, simplifies updates, and elimin= ates JSONB conversions.

On Mon, Dec 23, 2024 at 12:29=E2=80=AFPM Divyansh Gu= pta 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.


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 50 columns in it=C2=A0

CREATE TABLE db= o.googledocs_tbl (
gdid int8 GENERATED BY DEFAULT AS IDENTITY( INCREMEN= T BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 1 CACHE 1 NO CYCLE) NO= T NULL,
userid int8 NOT NULL,
t1 int4 NULL,
t2 int4 NULL,
t= 3 int4 NULL,
t4 int4 NULL,
t5 int4 NULL,
t6 int4 NULL,
t7 i= nt4 NULL,
t8 int4 NULL,
t9 int4 NULL,
t10 int4 NULL,
t11 in= t4 NULL,
t12 int4 NULL,
t13 int4 NULL,
t14 int4 NULL,
t15 i= nt4 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,
t2= 7 int4 NULL,
t28 int4 NULL,
t29 int4 NULL,
t30 int4 NULL,
t= 31 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,<= br> CONSTRAINT googledocs_tbl_pkey PRIMARY KEY (gdid),
);

Every t= ime when i query I will query it along with userid=C2=A0
Ex : where user= id =3D 12345678 and t1 in (1,2,3) and t2 in (0,1,2)
more key 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.googledocs_tbl (=
gdid int8 GENERATED BY DEFAULT AS IDENTITY( INCREMENT BY 1 MINVALUE 1 = MAXVALUE 9223372036854775807 START 1 CACHE 1 NO CYCLE) NOT NULL,
userid= int8 NOT NULL,
addons_json jsonb default '{}'::jsonb
CONST= RAINT googledocs_tbl_pkey PRIMARY KEY (gdid),
);

and the query wo= uld be like=C2=A0
where userid =3D 12345678 and ((addons_json=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 Mon, Dec 23, 2024 at 10:38=E2=80=AFPM David G. Johnst= on <david.g.johnston@gmail.com> wro= te:


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


=

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

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

David J.

=


--
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!
--0000000000000ef3340629f41949--