[英]MySQL doesn't always use a key with the same query
我有一張桌子:
CREATE TABLE `myTable` (
`a` DECIMAL(8,3) NULL DEFAULT NULL,
`b` INT(11) NULL DEFAULT NULL,
`name` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_general_ci',
`id` INT(11) NOT NULL AUTO_INCREMENT,
`plus` VARCHAR(100) NOT NULL DEFAULT 't' COLLATE 'utf8_unicode_ci',
PRIMARY KEY (`id`),
INDEX `myKey` (`a`, `b`, `name`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
AUTO_INCREMENT=2416682;
我運行查詢:
SELECT * FROM myTable
order by a, b, name
limit 10000, 8 ;
它非常慢,大約需要兩秒鍾。
如果我進行解釋,它會返回以下內容:
'id' => 1,
'select_type' => 'SIMPLE',
'table' => 'myTable ',
'partitions' => NULL,
'type' => 'ALL',
'possible_keys' => NULL,
'key' => NULL,
'key_len' => NULL,
'ref' => NULL,
'rows' => 1759263,
'filtered' => 100.00,
'Extra' => 'Using filesort',
現在,如果我將限制設置為較低的值,它將變得非常快,並解釋說它使用了myKey:
SELECT * FROM myTable
order by a, b, name
limit 1000, 8 ;
說明:
'id' => 1,
'select_type' => 'SIMPLE',
'table' => 'myTable',
'partitions' => NULL,
'type' => 'index',
'possible_keys' => NULL,
'key' => 'myKey',
'key_len' => '779',
'ref' => NULL,
'rows' => 1008,
'filtered' => 100.00,
'Extra' => NULL,
當限額很高時,我該怎么做才能加快選擇速度?
令我感到驚訝的是,鍵始終都沒有使用,與限制值無關。 這正常嗎?
MySQL版本是5.7.20-log。
謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.