基础知识:我有一个统计访问者的脚本。一些有用的信息存储在 MySQL 数据库中。现在一年多后,我有超过 180'000 条数据记录,读取实际访问者的脚本非常慢。
我有这个脚本来计算访问者并检查它是同一访问者还是新访问者。为此,我有一个 3 小时的时间范围,只有在这个范围之后,访问才会计算另一个时间。
脚本:
$besucher_query = mysql_query('SELECT `time`, `agent` FROM `besucher` ORDER BY `ip` ASC');
while($besucher = mysql_fetch_array($besucher_query)) {
    $newtime = $besucher['time'];
    $newagent = $besucher['agent'];
    $limit = 3 /*std*/ * 60 /*min*/ * 60 /*sek*/; // three hours before same visitor counts again
    $diff = $newtime - $limit;
    if($oldtime <= $diff or ($oldtime > $diff and $newagent != $oldagent))
        $besucherzahl++;
    $oldtime = $besucher['time'];
    $oldagent = $besucher['agent'];
        }
$anzahl_besucher = $besucherzahl;
echo $anzahl_besucher;
几年前我创建了这个脚本,我知道这是最丑陋的方法。时间以 UNIX Timestamp 格式存储。
现在我的问题是:如何直接在 MySQL 中将其与上述条件(3 小时规则)“分组”?