gpt4 book ai didi

mysql - mysql中覆盖索引是否比最左前缀索引策略优先级高?

转载 作者:行者123 更新时间:2023-11-30 00:53:44 25 4
gpt4 key购买 nike

我有一个名为 test 的表:

mysql> describe test;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| coll1 | int(11) | YES | MUL | NULL | |
| coll2 | int(11) | YES | | NULL | |
| coll3 | int(11) | YES | | NULL | |
| coll4 | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+

我在 table 上有一个索引:

mysql> show index from test;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------ +------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test | 1 | hi | 1 | coll1 | A | 0 | NULL | NULL | YES | BTREE | | |
| test | 1 | hi | 2 | coll2 | A | 0 | NULL | NULL | YES | BTREE | | |
| test | 1 | hi | 3 | coll3 | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

由于采用最左边前缀索引策略,下面的输出很容易理解。

mysql> explain select * from test where coll2=1;
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | test | ALL | NULL | NULL | NULL | NULL | 1 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+

我知道覆盖索引的东西,但是这是怎么发生的呢?这是否意味着覆盖索引的优先级高于最左边的前缀索引策略?

mysql> explain select coll1 from test where coll2=1;
+----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
| 1 | SIMPLE | test | index | NULL | hi | 15 | NULL | 1 | Using where; Using index |
+----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+

附注我使用的是mysql版本5.6.13

最佳答案

抱歉我的英语不好。

表文件由DATA文件 + INDEX文件组成。通常,一个复合索引列是所有列的子集。这意味着某些 INDEX 文件大小小于 DATA 文件大小。因此,以下查询使用 hi INDEX 扫描,其读取的磁盘数量少于 DATA 完整扫描。

SELECT coll1 FROM test WHERE coll2=1;

但是当您按如下方式检索 coll1, coll4 时,coll4 不是 hi 索引的一部分。因此,完整数据扫描速度更快。

SELECT coll1, coll4 FROM test WHERE coll2=1;

关于mysql - mysql中覆盖索引是否比最左前缀索引策略优先级高?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20758371/

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