I have a table user that looks like this
id | first_name | last_name | org_id
This table has few million entries.
I want to run the below query with an exact match and an order by clause
select * from user
where org_id = "some id"
ORDER BY first_name asc, last_name asc
limit 100;
I also have the following indexes:
- org_id
- org_id, first_name, last_name
When I run an explain on this query, mysql uses org_id index instead of the composite index on org_id, first_name, last_name.
This is the output of the explain query
I can see in the possible keys sections where mysql evaluates the composite index but still does not uses it.
I have read several answers like this one which says that composite index should be used here.
This query is really slow in case the match is really. Any idea
- why mysql is not using the composite index?
- How can I speed up this query?
Edit 1: Here is the table DDL
CREATE TABLE `user` (
`organisation_id` bigint(20) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`id` bigint(20) NOT NULL,
`first_name` varchar(255) DEFAULT NULL,
`middle_name` varchar(255) DEFAULT NULL,
`last_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `organisation_id` (`organisation_id`,`email`),
KEY `idx_first_name_last_name` (`first_name`(32),`last_name`(32)),
KEY `idx_organisation_id_first_name_last_name` (`organisation_id`,`first_name`(32),`last_name`(32)),
CONSTRAINT `user_org_fkey` FOREIGN KEY (`organisation_id`) REFERENCES `organisation` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
TIA
Update: Updating the index as mentioned by Liki solved the issue for me
