需要帮助来优化我的查询,当前它运行大约22秒,并且该表中有110万行。这是我的查询。该查询返回用户的最新时间。
SELECT
t.id, t.user_id, t.created
FROM
timesheets t
WHERE
t.user_id IN (41649 , 41901,41899,41482,41648,41897,41652,
41483,41900,41143,41907,16,42191,42195,42194,42136,42193,42190,42484,42485,42486,42504,
42052,42797,42997,42868,42847,42505,42964)
AND t.id = (SELECT
MAX(sub_t.id)
FROM
timesheets AS sub_t
WHERE
sub_t.user_id = t.user_id
AND sub_t.description = 'in')
我也有该表的表索引(id,user_id,创建的,描述)。任何建议都非常感谢。谢谢
答案 0 :(得分:1)
多列索引version: '3'
services:
reverse-proxy:
image: traefik
command: --api --docker
ports:
- "8880:80"
- "8080:8080"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
whoami:
image: containous/whoami
labels:
- "traefik.frontend.rule=Host:whoami.docker.localhost"
- "traefik.frontend.passHostHeader=true"
可能会有所帮助。
此外,也许使用联接而不是相关子查询。
(user_id, id)
我假设SELECT
t.id, t.user_id, t.created
FROM
timesheets t
JOIN (
SELECT MAX(id) AS maxid
FROM timesheets t
WHERE user_id IN (41649 , 41901,41899,41482,41648,41897,41652,
41483,41900,41143,41907,16,42191,42195,42194,42136,42193,42190,42484,42485,42486,42504,
42052,42797,42997,42868,42847,42505,42964)
AND description = "in"
GROUP BY user_id) t1
ON t.id = t1.maxid
是表的主键,所以它是唯一索引。