一个联合索引的问题

Longerandlonger 2014-04-21 07:44:54
我创建了一个唯一索引,其中包含 a、b、c、d 四个列。

那么当执行
select * from table_1
where a = xxx
and b = xxx
and c in (1, 2, 3, 4)
and d = xxx

这个语句性能是怎么样的啊?
...全文
174 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Longerandlonger 2014-05-06
  • 打赏
  • 举报
回复
的确是的!!! 请问这是mysql的一个什么feature啊?
Longerandlonger 2014-05-06
  • 打赏
  • 举报
回复
我发现,当select索引中包含的列,或者主键时,会使用 到索引。但当查询“非索引列且非主键列”时,explain显示会进行全表扫描。

CREATE TABLE `index_test` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `fn` varchar(16) CHARACTER SET latin1 NOT NULL,
  `ln` varchar(16) CHARACTER SET latin1 NOT NULL,
  `bd` date NOT NULL,
  `fn_crc` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `fn_ln_bd` (`fn`,`ln`,`bd`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
-------------------------------------------------------------------------------------------------------------------
mysql> explain select id from index_test where fn = 'xx' and ln in ('aa', 'bb', 'cc') and bd = '2012-12-23';
+----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| id | select_type | table      | type | possible_keys | key      | key_len | ref   | rows | Extra                    |
+----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
|  1 | SIMPLE      | index_test | ref  | fn_ln_bd      | fn_ln_bd | 18      | const |    3 | Using where; Using index |
+----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)

mysql> explain select * from index_test where fn = 'xx' and ln in ('aa', 'bb', 'cc') and bd = '2012-12-23';
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | index_test | ALL  | fn_ln_bd      | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select id, fn, ln, bd from index_test where fn = 'xx' and ln in ('aa', 'bb', 'cc') and bd = '2012-12-23';
+----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| id | select_type | table      | type | possible_keys | key      | key_len | ref   | rows | Extra                    |
+----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
|  1 | SIMPLE      | index_test | ref  | fn_ln_bd      | fn_ln_bd | 18      | const |    3 | Using where; Using index |
+----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)

mysql> explain select fn_crc from index_test where fn = 'xx' and ln in ('aa', 'bb', 'cc') and bd = '2012-12-23';
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
| 
ACMAIN_CHM 2014-05-06
  • 打赏
  • 举报
回复
贴出你的 show index from index_test; 怀疑你的表中可能只有几条数据。
引用 9 楼 Longerandlonger 的回复:
我发现,当select索引中包含的列,或者主键时,会使用 到索引。但当查询“非索引列且非主键列”时,explain显示会进行全表扫描。

CREATE TABLE `index_test` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `fn` varchar(16) CHARACTER SET latin1 NOT NULL,
  `ln` varchar(16) CHARACTER SET latin1 NOT NULL,
  `bd` date NOT NULL,
  `fn_crc` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `fn_ln_bd` (`fn`,`ln`,`bd`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
-------------------------------------------------------------------------------------------------------------------
mysql> explain select id from index_test where fn = 'xx' and ln in ('aa', 'bb', 'cc') and bd = '2012-12-23';
+----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| id | select_type | table      | type | possible_keys | key      | key_len | ref   | rows | Extra                    |
+----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
|  1 | SIMPLE      | index_test | ref  | fn_ln_bd      | fn_ln_bd | 18      | const |    3 | Using where; Using index |
+----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)

mysql> explain select * from index_test where fn = 'xx' and ln in ('aa', 'bb', 'cc') and bd = '2012-12-23';
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | index_test | ALL  | fn_ln_bd      | NULL | NULL    | NULL |    1 | Using where |
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

mysql> explain select id, fn, ln, bd from index_test where fn = 'xx' and ln in ('aa', 'bb', 'cc') and bd = '2012-12-23';
+----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
| id | select_type | table      | type | possible_keys | key      | key_len | ref   | rows | Extra                    |
+----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
|  1 | SIMPLE      | index_test | ref  | fn_ln_bd      | fn_ln_bd | 18      | const |    3 | Using where; Using index |
+----+-------------+------------+------+---------------+----------+---------+-------+------+--------------------------+
1 row in set (0.00 sec)

mysql> explain select fn_crc from index_test where fn = 'xx' and ln in ('aa', 'bb', 'cc') and bd = '2012-12-23';
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+------------+------+---------------+------+---------+------+------+-------------+
| 
Rotel-刘志东 2014-04-29
  • 打赏
  • 举报
回复
性能还是可以的。
ACMAIN_CHM 2014-04-23
  • 打赏
  • 举报
回复
贴出 show index from ... explain ... 以供分析。
Longerandlonger 2014-04-23
  • 打赏
  • 举报
回复
引用 1 楼 benluobobo 的回复:
这个效率是很好的,你也可以explain看看查询计划 如果没问题,type应该是const
type 不是 const,而是 range。貌似 range 性能不是最好的。
Longerandlonger 2014-04-23
  • 打赏
  • 举报
回复
explain,的确用到了这个索引,但是用索引的方式是“range”
码无边 2014-04-23
  • 打赏
  • 举报
回复
desc或者explain分析下语句
ACMAIN_CHM 2014-04-22
  • 打赏
  • 举报
回复
没有问题。 .
benluobo 2014-04-22
  • 打赏
  • 举报
回复
这个效率是很好的,你也可以explain看看查询计划 如果没问题,type应该是const
rucypli 2014-04-22
  • 打赏
  • 举报
回复
explain

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧