【发布时间】:2011-04-22 22:16:40
【问题描述】:
我目前正在 PHP 构建一个脚本,该脚本必须在完成其目的时更新统计信息。该脚本由网络浏览器访问,根据流量,它可以同时执行。我必须保证统计数据是正确的。
为了给你图片,假设我们有一张桌子:
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。
这个脚本预计会有相当多的流量,我想弄清楚当脚本执行并且有几个实例同时工作时如何使事情正常工作。您认为哪种方法/技术最适合此类任务?
【问题讨论】:
-
每天一行还是每个 user_id 每天一行?无论您想保证哪一个,
primary key和unique都是您的朋友。 -
每天每个 user_id 一行。我会改正的 - 很抱歉这个错误:)
标签: php postgresql simultaneous