MySQL慢查询请求修复,覆盖以提高速度

标签 mysql sql

我在高负载应用程序中收到了请求:

SELECT posts.id as post_id, posts.uid, text, date, like_count, 
       dislike_count, comments_count, post_likes.liked, image, aspect, 
       u.name as user_name, u.avatar, u.avatar_date, u.driver, u.number as user_number, 
       u.city_id as user_city_id, u.birthday as user_birthday, u.show_birthday, u.auto_model, u.auto_color, 
       u.verified , u.gps_x as user_gps_x, u.gps_y as user_gps_y, u.map_activity, u.show_on_map 
FROM posts 
     LEFT OUTER JOIN post_likes ON post_likes.post_id = posts.id and post_likes.uid = '478831' 
     LEFT OUTER JOIN users u ON posts.uid = u.id 
WHERE posts.info = 0  AND
 ( posts.uid = 478831 OR EXISTS(SELECT friend_id 
                                FROM friends 
                                WHERE user_id = 478831 
                                  AND posts.uid = friend_id 
                                  AND confirmed = 2) 
  ) 
order by posts.id desc limit 0, 20;

执行时间在 6-7 秒之间。 - 绝对糟糕。

EXPLAIN EXTENDED output:


*************************** 1. row ***************************
           id: 1
  select_type: PRIMARY
        table: posts
         type: ref
possible_keys: uid,info
          key: info
      key_len: 1
          ref: const
         rows: 471277
     filtered: 100.00
        Extra: Using where

*************************** 2. row ***************************
           id: 1
  select_type: PRIMARY
        table: post_likes
         type: ref
possible_keys: post_id
          key: post_id
      key_len: 8
          ref: anumbers.posts.id,const
         rows: 1
     filtered: 100.00
        Extra:

*************************** 3. row ***************************
           id: 1
  select_type: PRIMARY
        table: u
         type: eq_ref
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 4
          ref: anumbers.posts.uid
         rows: 1
     filtered: 100.00
        Extra:

*************************** 4. row ***************************
           id: 2
  select_type: DEPENDENT SUBQUERY
        table: friends
         type: eq_ref
possible_keys: user_id_2,user_id,friend_id,confirmed
          key: user_id_2
      key_len: 9
          ref: const,anumbers.posts.uid,const
         rows: 1
     filtered: 100.00
        Extra: Using index
4 rows in set, 2 warnings (0.00 sec)
mysql> `show index from posts;`
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| posts |          0 | PRIMARY  |            1 | id          | A         |     1351269 |     NULL | NULL   |      | BTREE      |         |               |
| posts |          1 | uid      |            1 | uid         | A         |      122842 |     NULL | NULL   |      | BTREE      |         |               |
| posts |          1 | gps_x    |            1 | gps_y       | A         |     1351269 |     NULL | NULL   |      | BTREE      |         |               |
| posts |          1 | city_id  |            1 | city_id     | A         |          20 |     NULL | NULL   |      | BTREE      |         |               |
| posts |          1 | info     |            1 | info        | A         |          20 |     NULL | NULL   |      | BTREE      |         |               |
| posts |          1 | group_id |            1 | group_id    | A         |          20 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

mysql> `show index from post_likes;`
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table      | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| post_likes |          0 | PRIMARY  |            1 | id          | A         |    10276317 |     NULL | NULL   |      | BTREE      |         |               |
| post_likes |          1 | post_id  |            1 | post_id     | A         |     3425439 |     NULL | NULL   |      | BTREE      |         |               |
| post_likes |          1 | post_id  |            2 | uid         | A         |    10276317 |     NULL | NULL   |      | BTREE      |         |               |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

mysql> `show index from users;`
+-------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name    | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| users |          0 | PRIMARY     |            1 | id           | A         |      497046 |     NULL | NULL   |      | BTREE      |         |               |
| users |          0 | number      |            1 | number       | A         |      497046 |     NULL | NULL   |      | BTREE      |         |               |
| users |          1 | name        |            1 | name         | A         |       99409 |     NULL | NULL   |      | BTREE      |         |               |
| users |          1 | show_phone  |            1 | show_phone   | A         |           8 |     NULL | NULL   |      | BTREE      |         |               |
| users |          1 | show_mail   |            1 | show_mail    | A         |          12 |     NULL | NULL   |      | BTREE      |         |               |
| users |          1 | show_on_map |            1 | show_on_map  | A         |          18 |     NULL | NULL   |      | BTREE      |         |               |
| users |          1 | show_on_map |            2 | map_activity | A         |      497046 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+-------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

mysql> `show index from friends;`
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table   | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| friends |          0 | PRIMARY   |            1 | id          | A         |     1999813 |     NULL | NULL   |      | BTREE      |         |               |
| friends |          0 | user_id_2 |            1 | user_id     | A         |      666604 |     NULL | NULL   |      | BTREE      |         |               |
| friends |          0 | user_id_2 |            2 | friend_id   | A         |     1999813 |     NULL | NULL   |      | BTREE      |         |               |
| friends |          0 | user_id_2 |            3 | confirmed   | A         |     1999813 |     NULL | NULL   |      | BTREE      |         |               |
| friends |          1 | user_id   |            1 | user_id     | A         |      499953 |     NULL | NULL   |      | BTREE      |         |               |
| friends |          1 | friend_id |            1 | friend_id   | A         |      499953 |     NULL | NULL   |      | BTREE      |         |               |
| friends |          1 | confirmed |            1 | confirmed   | A         |          17 |     NULL | NULL   |      | BTREE      |         |               |
+---------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

What I've done (tried to do): Set FORCE KEY(uid) for post table - works faster approximatelly on 2 secs. Of course I've tried rewrite this query and made somthing like this:

SELECT posts.id as post_id, posts.uid, text, date, like_count, dislike_count, comments_count, post_likes.liked, image, aspect, 
       u.name as user_name, u.avatar, u.avatar_date, u.driver, u.number as user_number, 
       u.city_id as user_city_id, u.birthday as user_birthday, u.show_birthday, u.auto_model, u.auto_color, 
       u.verified , u.gps_x as user_gps_x, u.gps_y as user_gps_y, u.map_activity, u.show_on_map 
FROM posts 
LEFT JOIN post_likes ON post_likes.post_id = posts.id and post_likes.uid = '478831' 
LEFT JOIN users u ON posts.uid = u.id LEFT JOIN friends AS f ON f.friend_id=posts.uid 
WHERE posts.info = 0  AND confirmed = 2 
group by posts.id, **posts.uid** 
order by posts.id desc limit 0, 20;

不幸的是,GROUP BY posts.uid 终止了这个查询的执行,因为返回的帖子中有很多行。

替代的简化查询(如果有帮助的话):

SELECT posts.id as post_id, posts.uid
FROM posts  
WHERE posts.info = 0  AND
 ( posts.uid = 478831 OR EXISTS(SELECT friend_id 
                                FROM friends 
                                WHERE user_id = 478831 
                                  AND posts.uid = friend_id 
                                  AND confirmed = 2) 
  ) 
order by posts.id desc limit 0, 20;

我需要的 - 执行速度小于 1 秒并输出相同结果的查询。 任何帮助将不胜感激。 提前致谢。

最佳答案

类似于@StanislavL 的解决方案,您可以执行连接而不是子查询:-

SELECT posts.id as post_id, posts.uid, text, date, like_count, 
       dislike_count, comments_count, post_likes.liked, image, aspect, 
       u.name as user_name, u.avatar, u.avatar_date, u.driver, u.number as user_number, 
       u.city_id as user_city_id, u.birthday as user_birthday, u.show_birthday, u.auto_model, u.auto_color, 
       u.verified , u.gps_x as user_gps_x, u.gps_y as user_gps_y, u.map_activity, u.show_on_map 
FROM posts 
     LEFT OUTER JOIN post_likes ON post_likes.post_id = posts.id and post_likes.uid = posts.uid
     LEFT OUTER JOIN users u ON posts.uid = u.id 
     LEFT OUTER JOIN friends f ON f.user_id = 478831 AND posts.uid = f.friend_id AND confirmed = 2
WHERE posts.info = 0  
AND (posts.uid = 478831 
OR f.friend_id IS NOT NULL) 
order by posts.id desc limit 0, 20;

然而,OR 可能会阻止它有效地使用索引。为避免这种情况,您可以联合执行 2 个查询,第一个忽略 friends 表,第二个对 friends 表执行 INNER JOIN:-

SELECT posts.id as post_id, posts.uid, text, date, like_count, 
       dislike_count, comments_count, post_likes.liked, image, aspect, 
       u.name as user_name, u.avatar, u.avatar_date, u.driver, u.number as user_number, 
       u.city_id as user_city_id, u.birthday as user_birthday, u.show_birthday, u.auto_model, u.auto_color, 
       u.verified , u.gps_x as user_gps_x, u.gps_y as user_gps_y, u.map_activity, u.show_on_map 
FROM posts 
     LEFT OUTER JOIN post_likes ON post_likes.post_id = posts.id and post_likes.uid = posts.uid
     LEFT OUTER JOIN users u ON posts.uid = u.id 
WHERE posts.info = 0  
AND posts.uid = 478831
UNION
SELECT posts.id as post_id, posts.uid, text, date, like_count, 
       dislike_count, comments_count, post_likes.liked, image, aspect, 
       u.name as user_name, u.avatar, u.avatar_date, u.driver, u.number as user_number, 
       u.city_id as user_city_id, u.birthday as user_birthday, u.show_birthday, u.auto_model, u.auto_color, 
       u.verified , u.gps_x as user_gps_x, u.gps_y as user_gps_y, u.map_activity, u.show_on_map 
FROM posts 
     LEFT OUTER JOIN post_likes ON post_likes.post_id = posts.id and post_likes.uid = posts.uid
     LEFT OUTER JOIN users u ON posts.uid = u.id 
     INNER JOIN friends f ON f.user_id = 478831 AND posts.uid = f.friend_id AND confirmed = 2
WHERE posts.info = 0  
order by posts.id desc limit 0, 20;

未测试

关于MySQL慢查询请求修复,覆盖以提高速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22089147/

相关文章:

php - "Undefined Variable"尝试读取行时

MySQL 按字母顺序排序但忽略 "The"

sql - 如何将数据库列中可接受的值限制为 1 到 5?

mysql - 一起显示两个mysql表的内容

php - SELECT 语句位于另一个 SELECT 语句中

mysql : find upper case Alphabets in like clause

mysql插入当前日期,仅在插入时间

php - 优化多个 MySQL(子)查询

mysql - MY sql 查询查找 # 状态

c# - 最大函数 Linq 查询 - Nullable Int 不是 IEnumerable?