我一直在想这个。一个每天24小时在线访问10000个访问者的站点,运行查询以选择具有lastaction>的所有行是不合适的。时间() - 300(5分钟前)? 此查询将每分钟运行数千次。
答案 0 :(得分:0)
使用像memcached(documentation)这样的缓存内容。
然后,仅在缓存已过期时才运行此查询
SELECT count(*) FROM users WHERE status = 1 // assume status = 1 = logged in here
像这样:
$memcache = memcache_connect('localhost', 11211);
// it is highly important to firewall off your memcache port.
$contents = memcache_get($memcache, 'user_count');
if ($contents === false) {
$count = /* query here */
memcache_set($memcache, 'user_count', $count, 0, 2);
} else {
$count = htmlspecialchars($contents);
// note: slightly unsafe in that there is no security. always escape.
}