查看“日志”数据库中的新(不同)事件

发布于 2024-10-07 17:36:47 字数 674 浏览 0 评论 0原文

我有一个应用程序,其数据库中有几个不相关的表。我将使用 SO 主页的“自动更新”版本作为示例进行解释,所以假设我有表“users”、“comments”和“问题”。

主页客户端需要定期轮询服务器,并获取所有已发生的新“事件”的日志。即,我想(以某种方式)显示自上次民意调查以来添加到 SO 中的新问题、评论和用户。

一种方法是简单地在客户端保留一个变量,其中包含每个表的最后一个索引,将其发送到服务器,然后让服务器向我发送新用户、评论和问题。

问题是,当我添加新类型的信息(例如投票)时会发生什么。现在我必须在客户端存储另一个变量,并且服务器必须轮询另一个表。等等,对于我保留的每一种新类型的信息。

我正在寻找一种解决方案来帮助我避免这种情况。

另一个问题 - 假设我想查看自上次以来发生的所有“事件”,但根据它们发生的时间排序。

我的一个方向是拥有一个“事件”表,其中包含有关每个事件发生时间的信息。然后我可以仅轮询该表,并获取已发生的所有新事件的列表。问题是每个事件都非常不同(新评论与新投票具有不同的列等)所以我不确定如何实现这一点,或者这是否是一个好主意。

有人知道我该如何解决这个问题吗?这似乎是经常出现的事情,但不幸的是,我对数据库并没有太多经验。

谢谢!

I have an application which has several unrelated tables in its db. I'll explain by using an "auto-updating" version of the SO homepage as an example, so lets say I have the tables "users", "comments" and "questions".

The homepage client side needs to periodically poll the server, and get a log of all the new "events" that have happened. I.e., I'd like to display (somehow) the new questions, comments and users that have been added to SO since the last poll.

On way would be to simply keep a variable on the client side containing the last index of each of my tables, send it to the server, and have the server send me the new users, comments and questions.

The problem is, what happens when I add a new type of information, say, votes. Now I have to store another variable on the client-side, and the server has to poll another table. And so on, for every new type of information I keep.

I'm looking for a solution that helps me avoid this.

Another problem - say I'd like to see all the "events" that have happened since last time, but sorted according to when they took place.

One direction I had is to have a single "events" table, which contains the info about when each event happened. I can then poll only this table, and get a list of all the new events that have happened. The problem is that each event is pretty different (a new comment has different columns than a new upvote, etc.) So I'm not sure how to implement this, or if this is even a good idea.

Does anybody have any ideas how I can solve this? This seems like something that would come up a lot, but I don't really have much experience with databases, unfortunately.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

川水往事 2024-10-14 17:36:47

在我看来,您正试图通过数据库设计来证明未来。虽然这可以通过 EVA 模型 来完成,但我警告不要这样做,因为该值它的附加值往往不值得付出代价。

相反,您应该对数据库进行尽可能接近现实的建模,而不是您打算如何使用它。

然后使用 SQL 将数据投影为您需要的方式。您可以通过语句来执行此操作,这些语句将提供您需要的元数据,

例如

Select 
  Count(ID) , 'Comments' Type
From 
  Comments
Where
  lastUpdate > #InputParamter1#
UNION Select 
  Count(ID) , 'Questions' Type
From 
  Questions
Where
  lastUpdate > #InputParamter1#

或者(这并没有经常使用)

从数据库中一次性返回多个结果集

Select 
    userid,
    ComentText
From 
    Comments
 Where
      lastUpdate > #InputParamter1#;
Select
    userId,
    Questions,
    Tags
From 
    Questions
 Where
      lastUpdate > #InputParamter1#

也就是说,您仍然需要编写一些如果您添加新内容,请编写代码,但它应该仅限于更新 sql、为数据添加新容器,然后编写代码以显示给最终用户,然后验证和存储它。

老实说,添加需要一些工作的新东西的想法对我来说似乎并不那么糟糕。

It sounds to me like you're trying to future proof via database design. While this can be done through something an EVA model I caution against that because the value its adds tend to not be worth the cost.

Instead you should model the database as closely to reality as possible and not how you intend to use it.

Then use SQL to project the data to how you need it. You can do this by statements that will either deliver the meta data that you need

e.g.

Select 
  Count(ID) , 'Comments' Type
From 
  Comments
Where
  lastUpdate > #InputParamter1#
UNION Select 
  Count(ID) , 'Questions' Type
From 
  Questions
Where
  lastUpdate > #InputParamter1#

Or (and this doesn't get used Often enough)

Return more than one result set from your database in one go

Select 
    userid,
    ComentText
From 
    Comments
 Where
      lastUpdate > #InputParamter1#;
Select
    userId,
    Questions,
    Tags
From 
    Questions
 Where
      lastUpdate > #InputParamter1#

That said you will still have to write some code if you add new stuff but it should be limited to updating your sql, adding new containers for your data and then code to display to the end users and then to validate and store it.

Honestly the idea of adding new stuff requiring some work doesn't seem that awful to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文