小编Que*_*tin的帖子

4M 行表上的简单查询很慢

我在生产中有一个pageviews包含 4M 行的 MySQL 表,用于记录用户在帖子上的页面浏览量。我需要知道特定用户阅读了哪些帖子,但此请求最多需要 15 秒才能执行:

SELECT post_id
FROM pageviews
WHERE user_id = 981
GROUP BY post_id
Run Code Online (Sandbox Code Playgroud)

下面是执行计划:

mysql> EXPLAIN SELECT post_id FROM visits WHERE user_id = 981 GROUP BY post_id;
+----+-------------+--------+------+---------------+---------+---------+-------+-------+----------------------------------------------+
| id | select_type | table  | type | possible_keys | key     | key_len | ref   | rows  | Extra                                        |
+----+-------------+--------+------+---------------+---------+---------+-------+-------+----------------------------------------------+
|  1 | SIMPLE      | visits | ref  | user_id       | user_id | 5       | const | 54696 | Using where; Using temporary; …
Run Code Online (Sandbox Code Playgroud)

mysql performance optimization query-performance

3
推荐指数
1
解决办法
729
查看次数