我编写了一个简单的Django应用程序,它显示了表中的值.该表非常简单,与其他表没有关系,所有值都是整数:
patientID region1 region2 regionN region200000
patient1 30 23 9 14
patient2 1 12 9 22
patientN 23 21 12 8
Run Code Online (Sandbox Code Playgroud)
唯一的问题是我有~200 000个基因组区域 - 这是固定数字(只有患者数量在增加).
这样的桌子最好的数据库系统是什么?这些数据是超级结构化的,简单而同质的 - 非常适合SQL,但建议它应该是noSQL?
最好有一个表有200000列,然后有N个表(N个患者)有200000行.
该设计不是 "完美的SQL",因为它违反了数据库规范化的基本规则.并且没有关系数据库支持单个表的200000列(其中大多数列具有大约1500到2000列的限制)
即使区域的数量是"固定的",在关系数据库中,每个"固定"事物也不应该有一列.
这是一种典型的多对多关系,通常用三个表建模.
一个用于患者,一个用于区域:
create table patient
(
id integer primary key,
... other columns
);
create table region
(
id integer primary key,
... other columns
);
Run Code Online (Sandbox Code Playgroud)
然后,您需要患者和地区之间的映射表:
create table person_region_map
(
person_id integer not null references person,
region_id integer not null references region,
primary key (person_id, region_id)
);
Run Code Online (Sandbox Code Playgroud)
由于两列上的主键,映射确保区域和人员的每个组合仅出现一次.
另一种选择是在关系数据库中利用JSON功能,这在当今很常见.它是否可用于您在很大程度上取决于您使用的实际DBMS产品.
在PostgreSQL中你可以考虑这样的事情:
create table patient
(
id integer primary key,
regions jsonb,
... other columns
);
Run Code Online (Sandbox Code Playgroud)
然后,您将插入一个JSON值,其中包含区域的键/值映射.这具有额外的好处,即如果区域未分配给区域,则不会占用空间:
insert into patient (id, regions)
values (42, '{"region1": 30, "region10": 9}');
Run Code Online (Sandbox Code Playgroud)
使用Postgres,可以非常有效地索引和查询.