Copyright: quack https://blog.csdn.net/qq_41570658/article/details/84754991
> This article introduces Mysql optimization techniques based on the time selected dates, very good, with a value for references, can refer a friend in need
Yesterday, for example, a query new registered users, the wording of the following two:
where DATE_FORMAT(u.register_time,'%Y-%m-%d')='2018-01-25'; EXPLAIN select * from chess_user u where u.register_time BETWEEN '2018-01-25 00:00:00' and '2018-01-25 23:59:59';
register_time
Datetime type field is converted to match the date and then, all the rows need, filtered. While the second wording, you can use index on register_time field, the query fast!
search result:
Attach the date of conversion functions:
DECLARE yt varchar(10); #昨天
DECLARE yt_bt varchar(19); #昨天开始时间
DECLARE yt_et varchar(19); #昨天结束时间
#设置变量
SET yt=DATE_FORMAT(DATE_ADD(now(),INTERVAL -1 day),'%Y-%m-%d');
SET yt_bt=DATE_FORMAT(DATE_ADD(now(),INTERVAL -1 day),'%Y-%m-%d 00:00:00');
SET yt_et=DATE_FORMAT(DATE_ADD(now(),INTERVAL -1 day),'%Y-%m-%d 23:59:59');