Question

I have the following complex query that is taking a while to run:

SELECT 
    `User`.`id`, 
    `User`.`username`, 
    `User`.`password`, 
    `User`.`role`, 
    `User`.`created`, 
    `User`.`modified`, 
    `User`.`email`, 
    `User`.`other_user_id`, 
    `User`.`first_name`, 
    `User`.`last_name`, 
    `User`.`school_id`, 
    `Resume`.`id`, 
    `Resume`.`user_id`, 
    `Resume`.`other_resume_id`, 
    `Resume`.`other_user_id`, 
    `Resume`.`file_extension`, 
    `Resume`.`created`, 
    `Resume`.`modified`, 
    `Resume`.`is_deleted`, 
    `Resume`.`has_file`, 
    `Resume`.`is_stamped`, 
    `Resume`.`is_active` 
FROM 
    `dataplace`.`users` AS `User` 
    LEFT JOIN `dataplace`.`attempts` AS `Attempt` 
        ON (`Attempt`.`user_id` = `User`.`id` AND `Attempt`.`test_id` != 5) 
    LEFT JOIN `dataplace`.`resumes` AS `Resume` 
        ON (`Resume`.`user_id` = `User`.`id`) 
WHERE 
    `Resume`.`has_file` = 1 
GROUP BY `User`.`id` 
ORDER BY `Attempt`.`score` DESC;

This query generates the following explain:

+----+-------------+---------+--------+---------------+---------------+---------+------------------------------+-------+----------------------------------------------+
| id | select_type | table   | type   | possible_keys | key           | key_len | ref                          | rows  | Extra                                        |
+----+-------------+---------+--------+---------------+---------------+---------+------------------------------+-------+----------------------------------------------+
|  1 | SIMPLE      | Resume  | ALL    | user_id_index | NULL          | NULL    | NULL                         | 18818 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | User    | eq_ref | PRIMARY       | PRIMARY       | 4       | dataplace.Resume.user_id     |     1 | Using where                                  |
|  1 | SIMPLE      | Attempt | ref    | user_id_index | user_id_index | 5       | dataplace.User.id            |     1 |                                              |
+----+-------------+---------+--------+---------------+---------------+---------+------------------------------+-------+----------------------------------------------+

The resume table has 4 separate indexes that are as followed:

PRIMARY id
user_id_index
other_resume_id_index
other_user_id_index

Based on this, I would expect the user_id index from the resumes table to be used with the query in question, but it is not. Could this be an issue with ordering? Is there some other reason why this index is not in use? Would a different index serve me better. I am not sure? Thank you to anyone that can help.

Était-ce utile?

La solution

You've basically got no useful WHERE clause, because the condition you have there applies to the last table joined and could be moved into the join condition of the last join.

The Users table is the first table accessed (it is named first in the FROM clause) and there are no conditions for users, so all rows must be accessed - no index could help there.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top