PostgreSQL表中行的递增值
|
我目前正在用PHP构建一个脚本,该脚本必须在完成其目的后才能更新统计信息。该脚本可以通过Web浏览器访问,并且可以根据通信量同时执行。我必须保证统计数据是正确的。
为了给您图片,我们有一张桌子:
CREATE TABLE statistics(
user_id integer NOT NULL,
date integer NOT NULL, -- for unix time
stat1 integer NOT NULL DEFAULT 0,
stat2 integer NOT NULL DEFAULT 0,
stat3 integer NOT NULL DEFAULT 0 -- and so on...
);
-- Let\'s insert some testing data for a couple of users and days...
-- Day one
INSERT INTO statistics(1, 1303520820, 1, 1, 1);
INSERT INTO statistics(2, 1303520820, 1, 1, 1);
-- Day two
INSERT INTO statistics(1, 1303603200, 1, 1, 1);
INSERT INTO statistics(2, 1303603200, 1, 1, 1);
-- Day three
INSERT INTO statistics(1, 1303689600, 1, 1, 1);
INSERT INTO statistics(2, 1303689600, 1, 1, 1);
每天都会在表格中插入新的一行,这样我们就可以获得每日,每周,每月,每年的统计信息。我必须确保每天每个user_id仅插入一行。同样,无论何时执行UPDATE查询,它都会适当地增加stat1,stat2,stat3列。
预计该脚本会有很多流量,我想弄清楚在脚本执行时如何使事情正常进行,并且有几个实例同时工作。您找到最适合此类任务的方法/技术?
没有找到相关结果
已邀请:
3 个回复
芭隘的盘石
无论采取什么其他措施,您都绝对应该这样做。
俯乡骚钵皆
括宠
如果尝试插入错误的date值,则会引发异常。 如果日期字段类型为TIMESTAMP或TIMESTAMPTZ,则CHECK更为复杂:
通过更改86400(秒计数),您可以将约束调整为各种量子:例如15分钟为900。