SQL:显示一段时间间隔内(接近)实时站点活动的最佳方式是什么?即“过去一小时内发送的消息”

发布于 2024-11-01 06:17:18 字数 307 浏览 6 评论 0原文

目标是尽可能频繁地更新 UI,但性能是一个问题,并且应尽量减少对数据库的访问。

我使用索引视图来保持不是特定时间间隔的项目的最新统计信息,但这不适用,因为日期是不确定的。

其他选项是:

  • 每 X 分钟运行一次作业来收集统计信息。

  • 添加触发器以响应插入。

  • 每次/以定义的频率运行存储过程。 (大多数相关表已在相关日期建立索引)

是否还有我尚未确定的其他选项以及针对此类问题是否有“首选”解决方案?作业/触发器/SP 选项如何比较?

The goal is to update the UI as frequently as possible but performance is a concern and hits to the database should be minimised.

I used Indexed Views to keep up to date stats on items that were not for a specific time interval however this in not applicable as the date is non-deterministic.

Other options would be to:

  • Run a job every X minutes to gather the stats.

  • Add triggers to respond to insertions.

  • Run a stored procedure every time / at a defined frequency. (Most of the tables concerned are already indexed on the date concerned)

Are there any other options I haven't identified and is there a 'preferred' solution to this kind of problem? How do the Job/Trigger/SP options compare?

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

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

发布评论

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

评论(1

感受沵的脚步 2024-11-08 06:17:18

如果您运行的时间间隔是“固定”的,则可以使用存储聚集索引键的查找表。假设密钥是 INT Identity,您可以创建一个如下表:

Time Datetime,
Qtr_Hour int,
Half_Hour int,
Hour int 

运行一个执行以下操作的查询:

SELECT COUNT(*)
FROM MyTable
WHERE ClusterKey > (SELECT Qtr_Hour
                    FROM LookupTable)

然后使用 MAX(ClusterKey) 的新相关值更新该表。

您需要计算出用例的详细信息,但是如果您有一个 1 行表,您只需在查询时保持最新状态,并且它使用簇键作为参考点,那么应该非常快地获得自上次更新以来行数的增量。

If you are running over "fixed" intervals, you can use a lookup table that stores your clustered index key. Assuming the key is an INT Identity you could make a table like:

Time Datetime,
Qtr_Hour int,
Half_Hour int,
Hour int 

Run a query that executes:

SELECT COUNT(*)
FROM MyTable
WHERE ClusterKey > (SELECT Qtr_Hour
                    FROM LookupTable)

Then update the table with the new relevant value for MAX(ClusterKey).

You would need to work out details for your use case, but if you have a 1 row table that you just keep up to date as you query, and it uses the cluster key as a reference point, it should be very fast to get a delta on the number of rows since the last update.

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