One of the most frustrating experiences when dealing with databases is when you've designed the perfect index, but MySQL still doesn't use it. There are several reasons why this could be the case, and in this article, we'll explore some of the most common ones. Throughout this article, we'll be working with a very simple people table that looks like this:CREATE TABLE `people` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `first_name` varchar(50) NOT NULL, `last_name` varchar(50) NOT NULL, `state` char(2) NOT NULL, PRIMARY KEY (`id`), KEY `first_name` (`first_name`), KEY `state` (`state`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
We'll be adding and dropping keys throughout to show different scenarios, but this is a good starting place. Determining what index is being used Before you can determine why your index isn't being used, you must first determine that your index isn't being used. You can run an EXPLAIN on …
[Read more]