更新查询没有充分利用MySQL 8中的索引

时间:2018-06-01 01:00:42

标签: mysql sql

鉴于此表:

CREATE TABLE `queue` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `type` int(10) unsigned NOT NULL,
  `posted_on` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
  `status` enum('pending','complete','error') NOT NULL DEFAULT 'pending',
  `body` blob NOT NULL,
  `process_id` int(10) unsigned DEFAULT NULL,
  `acquired_on` datetime(6) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `acquiredon` (`acquired_on`),
  KEY `type_status_processid_postedon` (`type`,`status`,`process_id`,`posted_on`) USING BTREE
);

当我对此表进行选择时,它会正确/完全使用索引:

EXPLAIN SELECT *
FROM `queue`
FORCE INDEX (`type_status_processid_postedon`)
WHERE type = 1
AND `status` = 'pending'
AND `process_id` IS NULL
ORDER BY `posted_on` ASC
LIMIT 1;

+----+-------------+-------+------------+------+--------------------------------+--------------------------------+---------+-------------------+------+----------+-----------------------+
| id | select_type | table | partitions | type | possible_keys                  | key                            | key_len | ref               | rows | filtered | Extra                 |
+----+-------------+-------+------------+------+--------------------------------+--------------------------------+---------+-------------------+------+----------+-----------------------+
|  1 | SIMPLE      | queue | NULL       | ref  | type_status_processid_postedon | type_status_processid_postedon | 10      | const,const,const |    1 |   100.00 | Using index condition |
+----+-------------+-------+------------+------+--------------------------------+--------------------------------+---------+-------------------+------+----------+-----------------------+

然而,当我执行与UPDATE相同的查询时,索引未完全使用。

EXPLAIN UPDATE `queue`
FORCE INDEX(`type_status_processid_postedon`)
SET `process_id` = 1
WHERE `type` = 1
AND `status` = 'pending'
AND `process_id` IS NULL
ORDER BY `posted_on` ASC
LIMIT 1;

+----+-------------+-------+------------+-------+--------------------------------+--------------------------------+---------+-------------------+------+----------+-----------------------------+
| id | select_type | table | partitions | type  | possible_keys                  | key                            | key_len | ref               | rows | filtered | Extra                       |
+----+-------------+-------+------------+-------+--------------------------------+--------------------------------+---------+-------------------+------+----------+-----------------------------+
|  1 | UPDATE      | queue | NULL       | range | type_status_processid_postedon | type_status_processid_postedon | 10      | const,const,const |    1 |   100.00 | Using where; Using filesort |
+----+-------------+-------+------------+-------+--------------------------------+--------------------------------+---------+-------------------+------+----------+-----------------------------+

更新执行文件排序。这里发生了什么?

0 个答案:

没有答案