mysql优化实践总结

1.表设计

 

能使用数值型能表达的就不要使用字符串

节约存储空间

字符串在查询和关联时需要逐位对比

 

使用 varchar 代替 char

通常 varchar 存储空间更小

小字符串查询效率更高

 

每张表都应该有 create_time, update_time ,且这两个字段不能赋予额外的业务用途

每次数据更新都应更新 update_time,耦合业务用途容易产生歧义

2.数据查询

 

select 后跟随具体字段而非 *

节约网络传输耗时

避免表结构变更后影响业务

 

当仅查询1行数据时加上 limit 1

数据库引擎执行效率更高

 

Join 的两个字段应该相同类型,都被建过索引

数据库引擎触发更优化的查询机制

3.数据查询 - 索引失效专题

error: select id from t where a is null

right: select id from t where a=0

尽量减少在 where 子句中使用 != , <> , 会触发全表扫描

避免在 where 子句 中对 null 进行判断,会触发全表扫描,应该设定默认值对默认值进行判断

error: select id from t where a=10 or a=20

right: select id from t where a=10 union all select id from t where a=20

避免使用 or 来关联查询条件,会触发全表扫描,应该使用 union 来合并查询结果

error: select id from t where name like‘%c%’

right: select id from t where name like‘c%’

尽量减少模糊查询时的前后模糊,会触发全表扫描

error: select id from t where a in(1,2,3)

right: select id from t where a between 1 and 3

尽量减少 in, not in 的使用,会触发全表扫描,部分场景可以使用between 替代

error: select id from t where substring(name,1,3)='abc' select id from t where datediff(day,createdate,'2005-11-30')= 0

right: select id from t where name like‘abc%’ select id from t where createdate>='2005-11-30′ and createdate<'2005-12-1′

尽量减少在 where 子句中使用函数操作

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值