I've got the request in high-load application:
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;
Execution time between 6-7 secs. - absolutelly bad.
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;
Unfortunatelly GROUP BY posts.uid kills this query execution, because of a lot rows from posts returning.
Alternative simplified query (if it may help):
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;
What I need - the query that executes faster less then 1 sec and outputs the same result. Any help will be appreciated. Thx in advance.