在DB中存储不同类型的消息的方法

时间:2015-12-11 18:59:30

标签: database database-design architecture

我想知道以有效的方式存储这类数据的可能性是什么。

让我们说我需要存储100种不同的消息。所有消息都有一个共同的数据,如消息ID,消息名称,发送者,接收者,插入日期等。每种消息都有自己唯一的列,我们想要存储和索引(快速查询)。

我们不想使用100个不同的表,因此无法使用。 我能提出的最佳方法是使用2-3个表: 1.对于共同数据。 2.当每列具有通用名称时的额外唯一数据:foreign key,column1,column2 .... column20。每列都有索引+外键(20列20个索引)。 3.可选的元数据表,用于描述每条唯一消息的通用列。

更新: 让我说我是传递数据的骨干,有100种不同的数据(消息)。我希望存储通过我发送的每条消息,但不是作为批量数据存储,因为稍后我想根据每种不同消息类型的唯一列查询数据。 有没有更好的方法,我不知道? 谢谢。 BTW数据库是oracle。

更新2:

NoSQL数据库能否为我提供更好的解决方案?

2 个答案:

答案 0 :(得分:1)

你建议的2或3个表的方法对我来说似乎是合理的。

另一种方法是将所有这些唯一列与公共数据一起存储在同一个表中。这不应该阻止你为它们创建索引。

答案 1 :(得分:0)

这是你的答案: enter image description here
您可以根据需要为每种消息类型存储尽可能多的消息

<强>更新 这正是你想要的。如您所述,有不同的消息类型,例如跟踪,货物或其他。这些类别将存储在msg_type表中。您可以根据需要在msg_type中存储尽可能多的类别。

然后说每个msg_type都有许多消息将存储在Messages表中。你可以在这里存储任意数量的消息,没有任何限制

这是您的数据库SQL:

create table msg_type(
type_id number(14) primary key,
type varchar2(50)
);

create sequence msg_type_seq
start with 1 increment by 1;

create or replace trigger msg_type_trig
before insert on msg_type
referencing new as new
for each row
begin
select msg_type_seq.nextval into :new.type_id from dual;
end;
/

create table Messages(
msg_id number(14) primary key,
type_id number(14) constraint Messages_fk references CvCategories(type_id),
msg_date timestamp(0) default sysdate,
msg varchar2(3900));

create sequence Messages_seq
start with 1 increment by 1;

create or replace trigger Messages_trig
before insert on Messages
referencing new as new
for each row
begin 
select Messages_seq.nextval into :new.msg_id from dual;
end;
/