时间轴数据库架构?

天神天空

我在为时间表找到良好的数据库结构/架构时遇到了麻烦。

我有一个站点,用户可以在其中创建许多类型的对象,这些对象具有许多不同的属性,因此保存在许多不同的表中。

我的时间轴确实显示了按日期排序的所有不同对象。

我的想法是在创建新对象时在时间表中插入一条记录:

insert into timeline (date, objectType, objectId) values (...)

要显示我的时间轴,我要做:

select * from timeline order by date desc limit 100

没问题 接下来,我需要获取将在时间轴上显示的对象数据,只需:

select * from objectType where id = objectId 

在我的时间轴中的每个条目。这将成为问题,这些是许多查询的方式。

当然,我可以使用以下方法进行微调:

select * from objectType where id in (objectId, objectId)

但是,当我的时间轴中有许多objectTypes时,仍然会有很多查询。

有人对更好的方法有个好主意吗?

乔尔·布朗

您不会说您使用哪种SQL风格,因此很难说出要使用哪种语法。这是一些伪代码给您一个想法:

创建一个临时表(或视图)以使用连接不同类型的可显示详细信息,UNION然后从中选择TOP / LIMIT。

您可以将时间线加入各种对象类型,并根据每种对象类型的本机列的转换,将每个对象类型的属性投影为一致的可显示格式:

CREATE VIEW displayable_events
AS
select 
  e.date, e.objectType, e.objectID, 
  o.ColumnOfInterest1 + '-' + o.ColumnOfInterest2 as Description
                      -- Or whatever you want to show
from timeline e 
  inner join objectTypeA o
    on e.objectID = o.objectID
where e.objectType = 'TypeA' -- or whatever you're using to distinguish them.
--
UNION ALL
--
select 
  e.date, e.objectType, e.objectID, 
  o.ColumnOfInterest1 + 'anything else you like' as Description
from timeline e 
  inner join objectTypeB o
    on e.objectID = o.objectID
where e.objectType = 'TypeB' 
--
UNION ALL
--
... and so forth for all of your object types...

然后,您可以从该视图(或临时表)中进行选择,如下所示:

SELECT * from displayable_events
WHERE -- whatever you use to distinguish whose timeline you want ---
ORDER BY date DESC
LIMIT 100

这节省了您为每个时间线条目单独执行所有查询的时间,因为它使用了基于集合的方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章