【发布时间】:2021-05-26 19:15:07
【问题描述】:
我正在跟踪网站上发生的用户事件(登录、注销、页面加载等)。
我想运行一个查询以获取以下用户:
- 已登录
- 尚未登出
- 90 分钟前未登录
在下面的示例中,我创建了一些数据,应该只返回一个使用user_id = 3 登录的用户,因为
-
user_id 1已登出 -
user_id 2超时 -
user_id 3已登录且未超时或手动注销
假设现在时间是 2021-02-24 12:15:00
| id | user_id | description | created_at |
|---|---|---|---|
| 19954 | 3 | log in | 2021-02-24 12:00:00 |
| 16085 | 1 | log out | 2021-02-24 12:00:00 |
| 11844 | 2 | log in | 2021-02-24 10:00:00 |
| 16850 | 1 | log in | 2021-02-24 10:00:00 |
我当前的查询是这样的,但它运行得很慢。
SELECT DISTINCT(user_id), id, created_at
FROM events e1
WHERE id = (
SELECT id
FROM events e2
WHERE e2.user_id = e1.user_id
AND description IN ('log in', 'log out')
ORDER BY created_at desc
LIMIT 1
)
AND description = 'log in'
AND created_at > NOW() - INTERVAL 90 MINUTE
ORDER BY created_at desc
我的索引如下。
| PRIMARY | BTREE | TRUE | id |
| description_index | BTREE | FALSE | description |
| user_desc_created_index | BTREE | FALSE | user_id,description,created_at |
| user_id_description_index | BTREE | FALSE | user_id,description |
我想我可能需要一个联接而不是子查询,但我不确定具体如何。有人可以帮忙吗?
【问题讨论】:
标签: mysql join subquery query-optimization