常用索引优化

唯一索引
联合索引
索引方式:BTREE

1.order by create_create 改为 order by id
数据递增插入且有序时
主键一般都为此表的唯一性索引

2.查询条件不适用 or
若有or 则将SQL 查分成两个或多个
使用Union 连接两个SQL

3.limit 1 限制查询格式

4.放弃使用索引的情况
情况 举例 说明
设置为NULL number is null
!= 或 <> number != 3 改为 =其他情况用and连接
or a = 1 or b = 3 拆分成两个SQL,使用Union all连接
.in 和 not in number in (2,3) 连续的数字用between,用exist替代in
like like "%have%"
在where子句中使用索引 在查询条件上使用函数DATE(CREATE_TIME) <= ''
update 只更新需要变动的字段 不要全表更新


博文参考
数据库SQL优化大总结

5.日期检索使用like 后百分号,like "2017-10-29%"

猜你喜欢

转载自mingyundezuoan.iteye.com/blog/2397960