Bug #34041 MySQL doesn't use col_name(length) indexes to resolve the ORDER BY
Description:
MySQL doesn't use indexes which was created with index prefix length using col_name(length) syntax to resolve the ORDER BY
How to repeat:
DROP TABLE IF EXISTS products;
CREATE TABLE products (
id int(11) unsigned NOT NULL auto_increment,
title varchar(255) NOT NULL default '',
PRIMARY KEY (id),
INDEX title (title(10))
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO products (title) VALUES ('9 Product');
INSERT INTO products (title) VALUES ('8 Product');
INSERT INTO products (title) VALUES ('7 Product');
INSERT INTO products (title) VALUES ('6 Product');
INSERT INTO products (title) VALUES ('5 Product');
INSERT INTO products (title) VALUES ('4 Product');
INSERT INTO products (title) VALUES ('3 Product');
INSERT INTO products (title) VALUES ('2 Product');
INSERT INTO products (title) VALUES ('1 Product');
INSERT INTO products (title) VALUES ('0 Product');
mysql> EXPLAIN SELECT id, title FROM products ORDER BY title LIMIT 5;
+----+-------------+----------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+------+---------------+------+---------+------+------+----------------+
| 1 | SIMPLE | products | ALL | NULL | NULL | NULL | NULL | 10 | Using filesort |
+----+-------------+----------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)
If you use an index created from the whole field everything works fine:
ALTER TABLE products DROP KEY title;
ALTER TABLE products ADD KEY title (title);
mysql> EXPLAIN SELECT id, title FROM products ORDER BY title LIMIT 5;
+----+-------------+----------+-------+---------------+-------+---------+------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------+-------+---------------+-------+---------+------+------+-------+
| 1 | SIMPLE | products | index | NULL | title | 765 | NULL | 10 | |
+----+-------------+----------+-------+---------------+-------+---------+------+------+-------+
1 row in set (0.01 sec)
Portions of this website are copyright © 2001, 2002 The PHP Group
Page generated in 0.050 sec. using MySQL 8.4.7-u6-cloud
Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the
respective copyright holders.
It is not reviewed in advance
by Oracle and does not necessarily represent the opinion of
Oracle or any other party.
Description: MySQL doesn't use indexes which was created with index prefix length using col_name(length) syntax to resolve the ORDER BY How to repeat: DROP TABLE IF EXISTS products; CREATE TABLE products ( id int(11) unsigned NOT NULL auto_increment, title varchar(255) NOT NULL default '', PRIMARY KEY (id), INDEX title (title(10)) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; INSERT INTO products (title) VALUES ('9 Product'); INSERT INTO products (title) VALUES ('8 Product'); INSERT INTO products (title) VALUES ('7 Product'); INSERT INTO products (title) VALUES ('6 Product'); INSERT INTO products (title) VALUES ('5 Product'); INSERT INTO products (title) VALUES ('4 Product'); INSERT INTO products (title) VALUES ('3 Product'); INSERT INTO products (title) VALUES ('2 Product'); INSERT INTO products (title) VALUES ('1 Product'); INSERT INTO products (title) VALUES ('0 Product'); mysql> EXPLAIN SELECT id, title FROM products ORDER BY title LIMIT 5; +----+-------------+----------+------+---------------+------+---------+------+------+----------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+------+---------------+------+---------+------+------+----------------+ | 1 | SIMPLE | products | ALL | NULL | NULL | NULL | NULL | 10 | Using filesort | +----+-------------+----------+------+---------------+------+---------+------+------+----------------+ 1 row in set (0.00 sec) If you use an index created from the whole field everything works fine: ALTER TABLE products DROP KEY title; ALTER TABLE products ADD KEY title (title); mysql> EXPLAIN SELECT id, title FROM products ORDER BY title LIMIT 5; +----+-------------+----------+-------+---------------+-------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+----------+-------+---------------+-------+---------+------+------+-------+ | 1 | SIMPLE | products | index | NULL | title | 765 | NULL | 10 | | +----+-------------+----------+-------+---------------+-------+---------+------+------+-------+ 1 row in set (0.01 sec)