私はテーブルを持っていますtb
:
ApplyID, ApplyDate,
=================================
John, 2008-01-23 12:00:01
Joe, 2008-01-23 12:00:02
Mary, 2008-01-23 12:00:02
Snoopy, 2008-01-23 12:00:06
Snoopy, 2008-01-23 12:00:07
Snoopy, 2008-01-23 12:00:11
John, 2008-01-23 12:00:21
各行について、次の 5 秒間に何行あるかを数えたいと思います。
次のような出力:
ApplyID, ApplyDate, Sessions
=================================
John, 2008-01-23 12:00:01, 3
Joe, 2008-01-23 12:00:02, 4
Mary, 2008-01-23 12:00:02, 4
Snoopy, 2008-01-23 12:00:06, 3
Snoopy, 2008-01-23 12:00:07, 2
Snoopy, 2008-01-23 12:00:11, 1
John, 2008-01-23 12:00:21, 1
私が使用するクエリ:
SELECT p1.ApplyID,
p1.ApplyDate,
(
SELECT COUNT(*)
FROM tb p2
WHERE p2.ApplyDate >= p1.ApplyDate
AND UNIX_TIMESTAMP(p2.ApplyDate)- UNIX_TIMESTAMP(p1.ApplyDate) <= 5
) AS sessions
FROM tb p1
ORDER BY ApplyDate
機能しますが、結果が表示されるまでに時間がかかります。クエリのパフォーマンスを向上させるより良い方法はありますか?