我目前正在 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。
该脚本预计会有相当多的流量,我想弄清楚当脚本执行时如何使事情正常工作并且有几个实例同时工作。您认为哪种方法/技术最适合此类任务?