准备
1.创建如下表结构

表结构图
2.添加百万条测试数据,注意数据的分散性

测试数据截图
测试
1.创建 account 单列索引
CREATE INDEX account_index ON user_info(account);
1.1 首先测试哪些操作会用到索引
select * from user_info order by account limit 10;

排序使用索引测试
select * from user_info where account = '10701445';

查询使用索引测试
- 结论
ORDER BY和WHERE操作都会使用到索引,且数据越分散作用越大。
1.2 如何使用索引
select * from user_info order by account limit 10; (使用索引)
select * from user_info order by age, account limit 10; (不使用索引)
select * from user_info where age > 25 and account = '15182615'; (使用索引)
select * from user_info where account = '15182615'; (使用索引)
2.创建 组合索引 account, email
create index account_email_index on user_info(account, email);
2.1 同单列索引一样,ORDER BY 和 SELECT 会使用到组合索引
2.2 如何使用组合索引
select * from user_info order by account, email limit 10; (使用索引)
select * from user_info order by account limit 10; (使用索引)
select * from user_info order by email limit 10; (不使用索引)
select * from user_info where name = '是稍' and account = '15182615'; (使用索引)
select * from user_info where name = '是稍' and email = '2x63dC7y@yahoo.com'; (不使用索引)
网友评论