我有一张超过50kk行的表。
指点杆:
+----+------------+-------------------+
| id | created_at | tag |
+----+------------+-------------------+
| 1 | 1484407910 | visitorDevice643 |
| 2 | 1484407913 | visitorDevice643 |
| 3 | 1484407916 | visitorDevice643 |
| 4 | 1484393575 | anonymousDevice16 |
| 5 | 1484393578 | anonymousDevice16 |
+----+------------+-------------------+
其中' created_at'是添加行的时间戳。 我有一个时间戳列表,例如像这样:
timestamps = [1502744400, 1502830800, 1502917200]
我需要在时间戳的i和i + 1之间的每个时间间隔中选择所有时间戳。
使用Django ORM看起来像:
step = 86400
for ts in timestamps[:-1]:
trackpoint_set.filter(created_at__gte=ts,created_at__lt=ts + step).values('tag').distinct().count()
因为实际上时间戳列表非常长而且表有很多行,所以最后我有500次超时
所以,我的问题是,如何在一个原始SQL查询中连接行和值列表,所以它看起来像[(1502744400, 650), (1502830800, 1550)...]
第二个第一个值是时间戳,第二个值是每个区间中唯一标记的数量。
答案 0 :(得分:1)
第一个索引created_at
。下一个构建查询,如created_at in (timestamp, timestamp+1)
。对于每个时间戳,逐个运行查询而不是一次运行。