gpt4 book ai didi

带有值列表的 MySQL 查询

转载 作者:行者123 更新时间:2023-11-29 02:44:15 24 4
gpt4 key购买 nike

我有一个超过 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)...]

其中第二个第一个值是时间戳,第二个是每个时间间隔内唯一标签的计数。

最佳答案

第一个索引 created_at。接下来构建查询,如 created_at in (timestamp, timestamp+1)。对于每个时间戳,一个一个地运行查询,而不是一次运行所有查询。

关于带有值列表的 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45209598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com