我目前正在PHP構建一個腳本,它在完成它的目的時必須更新統計信息。該腳本可以通過網絡瀏覽器訪問,並且可以同時執行該流量。我必須保證統計數據是正確的。PostgreSQL增量表中的行數
爲了讓您的圖片,讓我們說我們有一個表:
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每天一行?無論你想保證哪一個,「主鍵」和「唯一」都是你的朋友。 – 2011-04-22 22:33:56
每個user_id每天一行。我會糾正 - 對不起,錯誤:) – tftd 2011-04-22 22:35:40