高性能mysql(2)分析几种sql

准备工作,建立一张表
CREATE TABLE t_books2 (
id bigint(20) NOT NULL AUTO_INCREMENT,
uuid varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT ‘案件编号’,
book_name varchar(40) COLLATE utf8mb4_unicode_ci NOT NULL,
class_name varchar(23) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (id),
)
首先在uuid上建立索引。
explain select * from t_books2 where uuid in(‘2’,’3’,’4’) 利用到索引 ;
explain select * from t_books2 where uuid not in(‘2’,’3’,’4’) 没有;
explain select * from t_books2 where uuid<>‘2’;没有
explain select * from t_books2 where uuid =1 没有;
explain select * from t_books2 where uuid like ‘1%’ 有;
explain select * from t_books2 where uuid like ‘%1’ 没有;
接着在book_name建立索引

explain select * from t_books2 where uuid like ‘1’ or book_name=’2’;利用到了
explain select * from t_books2 where uuid = ‘1’ or class_name=’2’ 没有;
explain select * from t_books2 where uuid = ‘1’ union
select * from t_books2 where class_name=’3’;部分使用

3删除之前建立的索引,建立组合索引:
(uuid,book_name,class_name)
select * from t_books where uuid=’1’ and class_name=’2’;只利用到了uuid的索引
select * from t_books where uuid like ‘1’%’ and class_name=’2’;只利用到了uuid的索引

select * from t_books where uuid = ‘1’ and class_name=’2’;只利用到了uuid的索引
select * from t_books where uuid = ‘1’ and book_name=’2’ and class_name=’33’;三列索引都用上

select * from t_books where uuid = ‘1’ and class_name=’2’ order(group) by book_name;
三列索引都用上
select * from t_books where uuid = ‘1’ gourp by class_name,book_name;只用上uuid

select * from t_books where uuid = ‘1’ gourp by book_name,book_name; 三列索引都用上

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值