mysql中联合索引的使用

在oracle,如果存在一个多列的组合索引,比如a,b,c列上存在一个组合索引,那么在查询中如果出现where b=’xx’的情况是可以使用组合索引的,但是mysql是无法使用的,见下面的测试
| big_table | CREATE TABLE big_table (
TABLE_CATALOG varchar(512) NOT NULL DEFAULT ”,
TABLE_SCHEMA varchar(64) NOT NULL DEFAULT ”,
TABLE_NAME varchar(64) NOT NULL DEFAULT ”,
COLUMN_NAME varchar(64) NOT NULL DEFAULT ”,
ORDINAL_POSITION bigint(21) unsigned NOT NULL DEFAULT ‘0’,
COLUMN_DEFAULT longtext,
IS_NULLABLE varchar(3) NOT NULL DEFAULT ”,
DATA_TYPE varchar(64) NOT NULL DEFAULT ”,
CHARACTER_MAXIMUM_LENGTH bigint(21) unsigned DEFAULT NULL,
CHARACTER_OCTET_LENGTH bigint(21) unsigned DEFAULT NULL,
NUMERIC_PRECISION bigint(21) unsigned DEFAULT NULL,
NUMERIC_SCALE bigint(21) unsigned DEFAULT NULL,
DATETIME_PRECISION bigint(21) unsigned DEFAULT NULL,
CHARACTER_SET_NAME varchar(32) DEFAULT NULL,
COLLATION_NAME varchar(32) DEFAULT NULL,
COLUMN_TYPE longtext NOT NULL,
COLUMN_KEY varchar(3) NOT NULL DEFAULT ”,
EXTRA varchar(30) NOT NULL DEFAULT ”,
PRIVILEGES varchar(80) NOT NULL DEFAULT ”,
COLUMN_COMMENT varchar(1024) NOT NULL DEFAULT ”,
id int(10) unsigned NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id),
KEY idx_TABLE_NAME (TABLE_NAME,COLUMN_KEY)
) ENGINE=InnoDB AUTO_INCREMENT=918017 DEFAULT CHARSET=utf8 |
+———–+—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————–+
1 row in set (0.01 sec)

test>explain select * from big_table where column_key=’aa’;
+—-+————-+———–+——+—————+——+———+——+——–+————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+———–+——+—————+——+———+——+——–+————-+
| 1 | SIMPLE | big_table | ALL | NULL | NULL | NULL | NULL | 902397 | Using where |
+—-+————-+———–+——+—————+——+———+——+——–+————-+
1 row in set (0.02 sec)

.test>create index idx_column_key on big_table(column_key);
Query OK, 0 rows affected (22.39 sec)
Records: 0 Duplicates: 0 Warnings: 0

.test>explain select * from big_table where column_key=’aa’;
+—-+————-+———–+——+—————-+—————-+———+——-+——+———————–+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+———–+——+—————-+—————-+———+——-+——+———————–+
| 1 | SIMPLE | big_table | ref | idx_column_key | idx_column_key | 11 | const | 1 | Using index condition |
+—-+————-+———–+——+—————-+—————-+———+——-+——+———————–+
1 row in set (0.00 sec)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值