MySQL—计算每个记录下5秒内的行数。

时间:2021-11-04 15:29:25

I have a table tb:

我有一个表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  

I want to count how many rows in next 5 seconds for each row.
Output like:

我想在接下来的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  

The query I use:

我使用的查询:

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

It works but will take a long time to show result. Any better way to increase query performance?

它是有效的,但要显示结果需要很长时间。有没有更好的方法来提高查询性能?

2 个解决方案

#1


3  

Sometimes a join outperforms a subquery. You could try:

有时连接的性能超过子查询。你可以试试:

select  p1.ApplyID
,       p1.ApplyDate,
,       count(*)
from    tb p1
join    tb p2
on      p2.ApplyDate between p1.ApplyDate and p1.ApplyDate + interval 5 second
group by
        p1.ApplyID
,       p1.ApplyDate
order by
        p1.ApplyDate

Creating an index on ApplyDate might help:

在ApplyDate上创建一个索引可能会有帮助:

create index IX_TB_ApplyDate on tb (ApplyDate, ApplyID)

Two notes. Because you only select ApplyID and ApplyDate, this index is even covering for your query. And make sure your query doesn't use UNIX_TIMESTAMP, which may prevent MySQL from using the index.

两个音符。因为您只选择ApplyID和ApplyDate,所以这个索引甚至覆盖了您的查询。确保查询不使用UNIX_TIMESTAMP,这可能会阻止MySQL使用索引。

#2


0  

SELECT p1.ApplyID, p1.ApplyDate, COUNT(*) as SESSIONS
FROM tb p1, tb p2
WHERE p2.ApplyDate >= p1.ApplyDate
AND UNIX_TIMESTAMP(p2.ApplyDate)- UNIX_TIMESTAMP(p1.ApplyDate) <= 5
ORDER BY p1.ApplyDate

#1


3  

Sometimes a join outperforms a subquery. You could try:

有时连接的性能超过子查询。你可以试试:

select  p1.ApplyID
,       p1.ApplyDate,
,       count(*)
from    tb p1
join    tb p2
on      p2.ApplyDate between p1.ApplyDate and p1.ApplyDate + interval 5 second
group by
        p1.ApplyID
,       p1.ApplyDate
order by
        p1.ApplyDate

Creating an index on ApplyDate might help:

在ApplyDate上创建一个索引可能会有帮助:

create index IX_TB_ApplyDate on tb (ApplyDate, ApplyID)

Two notes. Because you only select ApplyID and ApplyDate, this index is even covering for your query. And make sure your query doesn't use UNIX_TIMESTAMP, which may prevent MySQL from using the index.

两个音符。因为您只选择ApplyID和ApplyDate,所以这个索引甚至覆盖了您的查询。确保查询不使用UNIX_TIMESTAMP,这可能会阻止MySQL使用索引。

#2


0  

SELECT p1.ApplyID, p1.ApplyDate, COUNT(*) as SESSIONS
FROM tb p1, tb p2
WHERE p2.ApplyDate >= p1.ApplyDate
AND UNIX_TIMESTAMP(p2.ApplyDate)- UNIX_TIMESTAMP(p1.ApplyDate) <= 5
ORDER BY p1.ApplyDate