我建議使用producer consumer模式,用新的表作爲實施「工作隊列」。不需要存儲過程,因爲觸發器非常簡單。
- 觸發器將填充工作隊列
- 代碼會輪詢工作隊列表。由於表格非常小,因此查詢速度快且負載低。
- 代碼會做任何你需要,完成後從表中刪除行 - 保持它儘可能小
創建具有notification
的ID進行處理和「處理狀態」列的表,例如:
create table work_queue (
id int not null auto_increment,
notification_id int references notifications,
status enum ('ready', 'processing', 'failed')
);
創建一個簡單的觸發器填充一個工作隊列表:
delimiter $
create trigger producer after insert on notifications
for each row begin
insert into work_queue (notification_id, status)
select new.id, 'ready'
where new.unread;
end; $
delimiter ;
你的鱈魚Ë將有僞代碼:
select * from work_queue where status = 'ready' order by id limit 1
update work_queue set status = 'processing' where id = <row.id>
- 你有什麼需要
notifications where id = <row.notification_id>
- 要麼
delete from work_queue where id = <row.id>
或update work_queue set status = 'failed' where id = <row.id>
(你必須弄清楚如何處理失敗的項目做)
- 睡眠1秒(此次停頓需要與通知的峯值到達率大致相同 - 您需要調整此值以平衡work_queue大小和服務器負載)
- 轉到1.
如果您有一個進程輪詢,則不需要鎖定問題。如果你有多個進程輪詢,你需要處理競爭條件。
有趣的想法,我認爲除了民意測驗之外沒有別的辦法, 謝謝你的支持:) –
沒有別的辦法,所以你想讓你的民意測驗儘可能快。我不得不說,這是我一生中贏得了聲譽的最艱難的10點:) – Bohemian
我想用一個內存中的表可能是一個很好的推動這裏 –