首页 > 代码库 > mysql优化

mysql优化

DATEDIFF是日期相减得到天数

DATE_FORMAT对日期格式化

DATEDIFF(DATE_FORMAT(NOW(),‘%Y-%m-%d‘), DATE_FORMAT(should_repay_date,‘%Y-%m-%d‘))  AS should_repay_day

 

统计记录数使用count(1),不要用count(*)

select count(1) from user

 

查询字段不要用* 代替所有的,要查那些字段就查那些字段

select username, password from user

 

实现先排序后分组,不要用下面这个语句,外表查询每一条记录都会从内表遍历一遍,速度非常慢

select id, username, password, age from 

(

select id, username, password, age from user order by age

) temp group by age

 

优化后

select t1.id, t1.usernam, t1.password, t1.age from user t1

inner join 

(select id, max(age) as maxAge from user group by age ) b on t1.id = b.id and t1.age = b.maxAge


mysql优化