gpt4 book ai didi

mysql - 为什么mysql解释说 'using index',而使用的索引不包含必填字段

转载 作者:行者123 更新时间:2023-11-29 11:29:14 24 4
gpt4 key购买 nike

这是 mysql(innodb) 中解释命令的输出:

explain select * from multi_index_test_tbl_1 force index(`query_index_1`) where `text_field1`='0' order by `numeric_field2` desc limit 1000000;
+----+-------------+------------------------+------+---------------+---------------+---------+-------+----------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------------+------+---------------+---------------+---------+-------+----------+--------------------------+
| 1 | SIMPLE | multi_index_test_tbl_1 | ref | query_index_1 | query_index_1 | 386 | const | 53547628 | Using where; Using index |
+----+-------------+------------------------+------+---------------+---------------+---------+-------+----------+--------------------------+

multi_index_test_tbl_1的架构如下:

CREATE TABLE IF NOT EXISTS `multi_index_test_tbl_1` 
(
`text_field1` varchar(128) NOT NULL,
`numeric_field1` float NOT NULL,
`numeric_field2` float NOT NULL,
`text_field2` varchar(128) NOT NULL,
PRIMARY KEY (`text_field1`,`numeric_field1`,`text_field2`),
KEY `query_index_1` (`text_field1`,`numeric_field2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

似乎使用了query_index_1。并且“使用索引”出现在 Extra 中,而索引 query_index_1 不包含表 multi_index_test_tbl_1 中的所有字段。

因为 mysql 文档说:

Using index (JSON property: using_index)

The column information is retrieved from the table using only information in the index tree without having to do an additional seek to read the actual row. This strategy can be used when the query uses only columns that are part of a single index.

我很困惑这里到底发生了什么。

最佳答案

啊,但它确实“包含必填字段”。详细说明...

InnoDB 在每个“辅助”索引中包含PRIMARY KEY 的所有列。所以,

KEY `query_index_1` (`text_field1`,`numeric_field2`)

实际上更像

KEY `query_index_1` (`text_field1`,`numeric_field2`,
`numeric_field1`,`text_field2`)

在您的示例中,这包括所有列。因此,SELECT 中的所有内容(包括 *)都可以在该二级索引中找到。因此,“使用索引”是“正确的”。

这是 InnoDB 有时比 MyISAM 更好的一种方式。

尝试 EXPLAIN FORMAT=JSON SELECT ... 以获取更多详细信息。

关于mysql - 为什么mysql解释说 'using index',而使用的索引不包含必填字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37692737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com