MySQL的性能优化

1.尽量把子段设置为NOT NULL.索引子段设置为NOT NULL

2.定义子段长度,不要设置过大,根据子段来调整子段大小。

3.使用外键

4.使用索引,复合索引,索引覆盖

5.sql语句的where查询条件,对字段进行表达式操作或函数操作,会导致mysql引擎放弃使用索引而进行全表查询

6.尽量避免使用or来连接条件查询数据(全表查询),使用union all 来代替

例如
select id from user where user name='a' or name='b'
改为以下
select id from user where name='a'
union all
select id from user where name='b'

7.在连续数值的查询中,能使用between的情况下,尽量使用between,而不使用in。in和not in 都会导致全表查询

select id from aa where num in (1,2,3)
改为
select id from aa where num between i and 3

8.在使用like进行数据表的查询时,能用单%的情况下,不建议使用双%,双%查询会导致mysql引擎放弃使用索引而进行全表查询

select * from t where name like '%de%';
/**上面语句可改为下面语句,提高查询效率****/
select * from t where name like 'de%';

9.应尽量避免在 where 子句中对字段进行 null 值判断

select id from t where num is null

10.多张数据表查询数据,使用inner join,left/right join来代替子查询,可以提高查询的效率。

select a.id,b.name,b.id,b.name
from a left join b
on a.id=b.id;

索引失效几种情况
(1)使用or来连接条件查询数据

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值