加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

MySQL表中具有相同字段的两个索引

发布时间:2020-12-11 23:51:31 所属栏目:MySql教程 来源:网络整理
导读:例如,我们有表: CREATE TABLE `my_tbl` ( `id` int(11) NOT NULL AUTO_INCREMENT,`id_type` int(11) NOT NULL,`date` date NOT NULL,`other_fields` varchar(200) CHARACTER SET latin1 NOT NULL,PRIMARY KEY (`id`),KEY `id_type` (`id_type`),KEY `type_d

例如,我们有表:

CREATE TABLE `my_tbl` (
 `id` int(11) NOT NULL AUTO_INCREMENT,`id_type` int(11) NOT NULL,`date` date NOT NULL,`other_fields` varchar(200) CHARACTER SET latin1 NOT NULL,PRIMARY KEY (`id`),KEY `id_type` (`id_type`),KEY `type_date` (`id_type`,`date`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Ther是两个索引:id_type和id_type,date.

据我所知,如果我们有两个字段的索引,我们可以将它用作第一个字段的单个索引.

我可以删除索引id_type而不会丢失性能吗?

更新:问这个问题引起注意,有时不同索引中的同一个字段具有不同的基数.

最佳答案 MySQL 5.7.9 – 删除id_type索引并没有什么区别.多列索引(type_date)适用于两个查询.

解释查询输出:

mysql> explain SELECT id_type,date FROM my_tbl WHERE id_type='some';
+----+-------------+--------+------------+------+---------------+-----------+---------+-------+------+----------+-------------+
| id | select_type | table  | partitions | type | possible_keys | key       | key_len | ref   | rows | filtered | Extra       |
+----+-------------+--------+------------+------+---------------+-----------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | my_tbl | NULL       | ref  | type_date     | type_date | 4       | const |    1 |   100.00 | Using index |
+----+-------------+--------+------------+------+---------------+-----------+-----



mysql> explain  SELECT id_type FROM my_tbl WHERE id_type='some';
+----+-------------+--------+------------+------+---------------+-----------+---------+-------+------+----------+-------------+
| id | select_type | table  | partitions | type | possible_keys | key       | key_len | ref   | rows | filtered | Extra       |
        +----+-------------+--------+------------+------+---------------+-----------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | my_tbl | NULL       | ref  | type_date     | type_date | 4       | const |    1 |   100.00 | Using index |
        +----+-------------+--------+------------+------+---------------+---------


mysql> show indexes from my_tbl;
+--------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table  | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+--------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| my_tbl |          0 | PRIMARY   |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| my_tbl |          1 | type_date |            1 | id_type     | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| my_tbl |          1 | type_date |            2 | date        | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
+--------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+-------------

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读