索引使用规范

使索引失效
CALL proc_drop_index(“数据库名”,“表名”)

建立单值索引
CREATE INDEX idx_age_name_depId ON emp(age,NAME,depId)

删除索引
DROP INDEX idx_age_name_depId ON emp(age,NAME,depId)

关联查询
create index idx_age_deptid_name on emp (age,deptid,name)

以下是否能使用到索引,能否去掉using filesort
1、explain select SQL_NO_CACHE * from emp order by age,deptid;
2、explain select SQL_NO_CACHE * from emp order by age,deptid limit 10;
#无过滤不索引

3、explain select * from emp where age=45 order by deptid;
4、explain select * from emp where age=45 order by deptid,name;
5、explain select * from emp where age=45 order by deptid,empno;
6、explain select * from emp where age=45 order by name,deptid;
7、 explain select * from emp where deptid=45 order by age;
#顺序错,必排序

8、 explain select * from emp where age=45 order by deptid desc, name desc ;
9、 explain select * from emp where age=45 order by deptid asc, name desc ;
#方向反 必排序

结论: 当范围条件和group by 或者 order by 的字段出现二选一时 ,
优先观察条件字段的过滤数量,如果过滤的数据足够多,而需要排序的数据并不多时,
优先把索引放在范围字段上。反之,亦然。

假设index(a,b,c)
Where语句 索引是否被使用(Y or N)
where a = 3 Y,使用到a
where a = 3 and b = 5 Y,使用到a,b
where a = 3 and b = 5 and c = 4 Y,使用到a,b,c
where b = 3 或者 where b = 3 and c = 4 或者 where c = 4 N,a没用到,断层
where a = 3 and c = 5 使用到a, 但是c不可以,b中间断了
where a = 3 and b > 4 and c = 5 使用到a和b, c不能用在范围之后,b断了
where a is null and b is not null is null 支持索引 但是is not null 不支持,所以 a 可以使用索引,但是 b不可以使用
where a <> 3 不能使用索引
where abs(a) =3 不能使用 索引
where a = 3 and b like ‘kk%’ and c = 4 Y,使用到a,b,c
where a = 3 and b like ‘%kk’ and c = 4 Y,只用到a,%号不能放在最前面
where a = 3 and b like ‘%kk%’ and c = 4 Y,只用到a
where a = 3 and b like ‘k%kk%’ and c = 4 Y,使用到a,b,c

and (
b1.name like concat(’%’,#{search},’%’)
or b2.templateName like concat(’%’,#{search},’%’)
存储过程与在java程序来写效果差一千倍有可能
优点:mysql存储过程一般互联网公司不会写,但一些国企,金融银行公司会采用,因为:1.安全性高,只是暴露一个接口存储过程名字,如果sql写在程序就会出现会反推表和字段,防止sql注入 2.速度快,减少网络io

缺点:不利于系统分层设计和维护,数据迁移全部要重写存储过程(比如迁移到orlco)
时间 复杂度:一次io叫o(1),两次叫o(2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值