0

我有一个相当“活跃”的 CDR 表,我想在最后 5 分钟内每隔 5 分钟从中选择一次记录。问题是它在其他一些列上生成了一个 SHA ID,所以我所依赖的只是一个时间戳字段,我通过该字段按日期过滤以选择我想要的记录的时间窗口。

下一个问题是,显然我不能保证我的脚本每次都能精确运行,或者服务器的挂钟是正确的(这没关系),最重要的是几乎肯定会有不止一个记录每秒说 3 行 '2013-08-08 14:57:05' 并且在第二个过期之前可能会再插入一个。

到 '2013-08-08 14:57:05' 并获取记录BETWEEN '2013-08-08 14:57:05' AND '2013-08-08 15:02:05'时,我会错过更多的 '2013-08-08 14:57:05' 记录。

本质上:

  • 不精确的挂钟时间
  • 没有顺序 ID
  • 每秒多条记录
  • 查询执行时间
  • 运行查询的频率不可靠

都在阻止我在指定的滚动时间窗口中获得一组有效的行。关于如何解决这些问题的任何建议?

4

2 回答 2

0

I probably not got all the details but to answer to your question title "Reliably select from a database table at fixed time intervals"...

I don't think you could even hope for a query to be run at "second precise" time.

One key problem with that approach is that you will have to deal with concurrent access and lock. You might be able to send the query at fixed time maybe, but your query might be waiting on the DB server for several seconds (or being executed seeing fairly outdated snapshot of the db). Especially in your case since the table is apparently "busy".


As a suggestion, if I were you, I would spend some time to think about queue messaging systems (like http://www.rabbitmq.com/ just to cite one, not presaging it is somehow "your" solution). Anyway those kind of tools are probably more suited to your needs.

于 2013-08-08T14:13:05.600 回答
0

如果您使用的是同一个时钟,那么我看不出事情会出错的原因。您要考虑的解决方案是日期时间表。这样一来,每次您根据服务器时间更新开始和停止时间时......然后随着添加的内容,它将保证在该时间范围内。

我的意思是,您可以通过硬编码来做到这一点,但我的方式会强制将起点和终点存储在数据库中以供使用。

我会使用 Cron 来处理一些时间间隔和时间。不使用从那以后的时间,而只是通过一直检查来不锁定数据库。

于 2013-08-08T14:15:35.797 回答