我有两个表,其中一个包含下载 url 的历史记录,而另一个表包含有关每个 url 的详细信息。
以下查询按过去一小时内的重复次数对 URL 进行分组。
SELECT COUNT(history.url) as total, history.url
FROM history
WHERE history.time > UNIX_TIMESTAMP()-3600
GROUP BY history.url
ORDER BY COUNT(history.url) DESC
LIMIT 30
Run Code Online (Sandbox Code Playgroud)
上面的查询大约需要 800ms 执行,不够快,但可以接受,
但是,当与缓存表连接时,新查询大约需要25s才能执行,速度非常慢。
SELECT th.total, th.url, tc.url, tc.json
FROM (SELECT COUNT(history.url) as total, history.url
FROM history
WHERE history.time > UNIX_TIMESTAMP()-3600
GROUP BY history.url
ORDER BY COUNT(history.url) DESC
LIMIT 30
) th
INNER JOIN (SELECT cache.url, cache.json FROM cache) tc
ON th.url = tc.url
GROUP BY th.url
ORDER BY th.total DESC
LIMIT …Run Code Online (Sandbox Code Playgroud)