This query executes quickly:
SELECT SQL_NO_CACHE users.id, users.name
FROM users
LEFT JOIN user_statistics ON users.id = user_statistics.user_id
WHERE users.is_deleted = 0
GROUP BY users.id
ORDER BY users.updated_at
LIMIT 50
This one slowly:
SELECT SQL_NO_CACHE users.id, users.name, MAX(user_statistics.access_time) AS user_access_time
FROM users
LEFT JOIN user_statistics ON users.id = user_statistics.user_id
WHERE users.is_deleted = 0
GROUP BY users.id
ORDER BY user_access_time
LIMIT 50
Here's the explain output for the slow query:
*************************** 1. row ***************************
id: 1 select_type: SIMPLE
table: users
type: ref possible_keys: fk_users_is_deleted
key: fk_users_is_deleted
key_len: 4
ref: const
rows: 107696
Extra: Using where; Using temporary; Using filesort
*************************** 2. row ***************************
id: 1 select_type: SIMPLE
table: user_statistics
type: ref possible_keys: fk_user_statistics_user_id
key: fk_user_statistics_user_id
key_len: 4
ref: users.id
rows: 1
Extra:
I am using GROUP BY
and ORDER BY
on different tables. How can I optimize the above slow query?
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `users` ADD INDEX `users_idx_is_deleted_id` (`is_deleted`,`id`);
SELECT
SQL_NO_CACHE users.id,
users.name
FROM
users
WHERE
users.is_deleted = 0
GROUP BY
users.id
ORDER BY
users.updated_at LIMIT 50