Mysql optimization techniques for querying dates based on time

Mysql optimization techniques for querying dates based on time

For example, to query yesterday's newly registered users, there are two ways to write it:

EXPLAIN
select * from chess_user u 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';

The register_time field is of datetime type. To convert it to a date and then match it, you need to query all rows for filtering. The second way of writing can use the index created on the register_time field to make the query extremely fast!

Attach date conversion function

 DECLARE yt varchar(10); #Yesterday DECLARE yt_bt varchar(19); #Yesterday's start time DECLARE yt_et varchar(19); #Yesterday's end time #Set variables 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');

Summarize

The above is the optimization technique of Mysql querying date based on time introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Python MySQL datetime formatting as parameter operations
  • Example analysis of interval calculation of mysql date and time
  • Summary of how to format MySQL timestamp as date using thinkphp5.1 framework
  • Summary of commonly used time, date and conversion functions in Mysql
  • mysql gets yesterday's date, today's date, tomorrow's date, and the time of the previous hour and the next hour
  • Analyze the selection problem of storing time and date types in MySQL
  • MySQL gets the current date and time function
  • How to query date and time in mysql

<<:  A brief discussion on several specifications of JS front-end modularization

>>:  How to mount a new disk on a Linux cloud server

Recommend

How to uninstall and reinstall Tomcat (with pictures and text)

Uninstall tomcat9 1. Since the installation of To...

How to deploy k8s in docker

K8s k8s is a cluster. There are multiple Namespac...

Detailed explanation and summary of the URL for database connection

Detailed explanation and summary of the URL for d...

A brief introduction to Linux environment variable files

In the Linux system, environment variables can be...

Linux system command notes

This article describes the linux system commands....

Two methods to disable form controls in HTML: readonly and disabled

In the process of making web pages, we often use f...

Analysis of the use of the MySQL database show processlist command

In actual project development, if we have a lot o...

Application of Hadoop counters and data cleaning

Data cleaning (ETL) Before running the core busin...

How to solve mysql error 10061

This article shares with you the solution to the ...

Using js to implement the two-way binding function of data in Vue2.0

Object.defineProperty Understanding grammar: Obje...

echars 3D map solution for custom colors of regions

Table of contents question extend Solving the pro...

XHTML 2.0 New Features Preview

<br />Before browsers can handle the next ge...

Pure CSS drop-down menu

Achieve results Implementation Code html <div ...