I've added an index (IDX_D34A04AD46C53D4C41FA5CD2) to my product table in order to speed-up searching for enabled products, ordered by price ascending:
CREATE TABLE `product` (
`id` varchar(16) COLLATE utf8_unicode_ci NOT NULL,
`unit_price` decimal(13,4) NOT NULL,
`stock_qty` int(11) DEFAULT NULL,
`is_enabled` tinyint(1) NOT NULL,
`min_sale_qty` int(11) DEFAULT NULL,
`max_sale_qty` int(11) DEFAULT NULL,
`package_qty` int(11) DEFAULT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`is_new` tinyint(1) NOT NULL,
`created_at` date NOT NULL,
`package_type` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_D34A04ADBF396750` (`id`),
KEY `IDX_D34A04AD41FA5CD2` (`unit_price`),
KEY `IDX_D34A04AD46C53D4C` (`is_enabled`),
KEY `IDX_D34A04AD46C53D4C41FA5CD2` (`is_enabled`,`unit_price`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Searching for active products, order by price, showing 50 items per page:
EXPLAIN SELECT * FROM product WHERE is_enabled > 0 ORDER BY unit_price ASC LIMIT 0, 50;
Output:
1 SIMPLE product index IDX_D34A04AD46C53D4C,IDX_D34A04AD46C53D4C41FA5CD2 IDX_D34A04AD41FA5CD2 6 100 Using where
Can you simple explain what I'm doing wrong and why I can't achieve "Using Index Condition" in my example?
EDIT: from MySQL documentation:
The following queries use the index to resolve the ORDER BY part:
SELECT * FROM t1 WHERE key_part1 = constant ORDER BY key_part2;
It seems exactly the same example as mine.