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

Things to note when writing self-closing XHTML tags

The img tag in XHTML should be written like this:...

Vue custom component implements two-way binding

Scenario: The interaction methods between parent ...

Native JS to achieve draggable login box

This article shares a draggable login box impleme...

Docker uses a single image to map to multiple ports

need: The official website's resource server ...

React native realizes the monitoring gesture up and down pull effect

React native implements the monitoring gesture to...

Let's talk about bitwise operations in React source code in detail

Table of contents Preface Several common bit oper...

HTML Tutorial: Collection of commonly used HTML tags (4)

These introduced HTML tags do not necessarily ful...

Introduction to Semantic XHTML Tags

The first point to make is that people can judge t...

How to design and optimize MySQL indexes

Table of contents What is an index? Leftmost pref...

Xhtml special characters collection

nbsp &#160; no-break space = non-breaking spa...

MySQL joint table query basic operation left-join common pitfalls

Overview For small and medium-sized projects, joi...

Vue implements three-level navigation display and hiding

This article example shares the specific code of ...