MySQL optimization based on time query date

Source: Internet
Author: User

For example, to query the new registered users yesterday, the following two types of writing:

EXPLAINSelect *  fromChess_user uwhereDate_format (U.register_time,'%y-%m-%d')='2018-01-25'; EXPLAINSelect *  fromChess_user uwhereU.register_timebetween '2018-01-25 00:00:00'  and '2018-01-25 23:59:59';

The Register_time field is a DateTime type and is converted to date to match, and all rows need to be queried for filtering. And the second way, you can use the Register_time field to build an index, query extremely fast!

Attach Date conversion function

DECLAREYtvarchar(Ten); #昨天DECLAREYt_btvarchar( +); #昨天开始时间DECLAREYt_etvarchar( +); #昨天结束时间 #设置变量SETYt=Date_format (Date_add (now (), INTERVAL-1  Day),'%y-%m-%d'); SETYt_bt=Date_format (Date_add (now (), INTERVAL-1  Day),'%y-%m-%d 00:00:00'); SETYt_et=Date_format (Date_add (now (), INTERVAL-1  Day),'%y-%m-%d 23:59:59');

MySQL optimization based on time query date

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.