我有一个Web应用程序,它的功能之一就是不断地向一个大表追加行。行大致如下:
id bigint not null primary key
visited datetime not null
ip_address
# Other fields
这是一个跟踪表,你可能已经猜到了。这张桌子的使用
是完全附加的,也就是说,在它们
已插入。
但是,我们的数据量已经显著增加,这变得很有必要
将统计信息单独处理到其他表中,而不是查询
数据并立即计算。基本上我写了一个独立的程序
大概是这样的(伪代码)
while (true) {
Select rows from tracking table where id > last_id
Feed rows to stats processing thread
last_id = max(id from rows)
sleep some amount of time (~30sec is what I'm currently using)
}
不过,我担心我会错过排。ID中可能会出现一个间隙,因为
在我从跟踪表中选择行时,一些行ID
已保留,但尚未提交这些事务中的数据,并且
下一个循环我已经转到新的ID。
我一直在想如何协调这一点,因为计算时缺少数据
统计数据不是一个选择。
以下是我一直在考虑的各种选择:
-
重新设计表或查询,例如访问时间
-
不要使用关系数据库,而是使用某种数据队列系统?
-
查询表的时间间隔,即id>last_id和visited<now()-有时
还有一种可能是我没有考虑过的选择。最好的是什么
如何查询表以避免丢失任何数据?