简体   繁体   中英

Simple ordered MySQL query running very slow on a large table

I have a table with ~ 1.500.000 records:

CREATE TABLE `item_locale` (
  `item_id` bigint(20) NOT NULL,
  `language` int(11) NOT NULL,
  `name` varchar(256) COLLATE utf8_czech_ci NOT NULL,
  `text` text COLLATE utf8_czech_ci)
  PRIMARY KEY (`item_id`,`language`),
  KEY `name` (`name`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;

With item_id , language as primary keys and index on name with size 255.

With following query:

select item_id, name from item_locale order by name limit 50;

The select takes around 3 seconds event though only 50 rows were required.

What can I do to speed up such query?

EDIT: Some of you suggested adding an INDEX. I mentioned above, that the name column is indexed with size 255.

I runned explain on the command:

+----+-------------+---------------+------+---------------+------+---------+------+---------+----------------+
| id | select_type | table         | type | possible_keys | key  | key_len | ref  | rows    | Extra          |
+----+-------------+---------------+------+---------------+------+---------+------+---------+----------------+
|  1 | SIMPLE      | item_locale | ALL  | NULL          | NULL | NULL    | NULL | 1558653 | Using filesort |
+----+-------------+---------------+------+---------------+------+---------+------+---------+----------------+

Strange thing is that it is seems not to use any index...

Retrieving 50 Records is heavier too. Limit them to 10 Since you are using Order by also..

Try to use query hint:

select item_id, name 
 from item_locale USE INDEX FOR ORDER BY (name) 
  order by name limit 50;

also try to use

select item_id, name 
 from item_locale FORCE INDEX (name) 
  order by name limit 50;

In the end, there was some kind of problem with indexes - I dropped them all and recreated them again. And it finally works. Thanks.

在名称字段上应用索引,这可能会加快索引速度。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM