问题描述
我想编写一个点击计数器脚本来跟踪网站上图片的点击次数和原始IP.每天的展示次数超过数十万,因此计数器会每秒增加多次.
I want to write a hit counter script to keep track of hits on images on a website and the originating IPs. Impressions are upwards of hundreds of thousands per day, so the counters will be incremented many times a second.
我正在寻找一种简单的,自托管的方法(php,python脚本等).我当时在考虑使用MySQL来跟踪此情况,但我猜测还有一种更有效的方法.有什么好的记帐方法?
I'm looking for a simple, self-hosted method (php, python scripts, etc.). I was thinking of using MySQL to keep track of this, but I'm guessing there's a more efficient way. What are good methods of keeping counters?
推荐答案
一个有趣的主题.增加一个计数器(可能很简单),只是已经是一个事务……这意味着,它可以将整个数据库锁定的时间超过了合理的时间!-)这很容易成为瓶颈整个系统.
A fascinating subject. Incrementing a counter, simple as it may be, just has to be a transaction... meaning, it can lock out the whole DB for longer than makes sense!-) It can easily be the bottleneck for the whole system.
如果您需要严格精确的计数,但又不需要立即更新它们,我最喜欢的方法是将可计数的信息附加到日志中(出于数据更新目的,经常切换日志).关闭日志后(其中包含成千上万的可计数事件),脚本可以读取并更新单个事务中所需的所有内容-可能不直观,但比成千上万个单锁要快得多.
If you need rigorously exact counts but don't need them to be instantly up-to-date, my favorite approach is to append the countable information to a log (switching logs as often as needed for data freshness purposes). Once a log is closed (with thousands of countable events in it), a script can read it and update all that's needed in a single transaction -- maybe not intuitive, but much faster than thousands of single locks.
然后有非常快的计数器,仅统计准确-但由于您不说这种不精确性是可以接受的,因此我不会更深入地解释它们.
Then there's extremely-fast counters that are only statistically accurate -- but since you don't say that such imprecision is acceptable, I'm not going to explain them in more depth.
这篇关于如何为网站编写高效的计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!