Mysql query the most recent record of the sql statement (optimization)

Mysql query the most recent record of the sql statement (optimization)

The worst option is to sort the results by time and take the first one.

select * from a 
where create_time<="2017-03-29 19:30:36" 
order by create_time desc
limit 1

Although this method can retrieve the most recent record at the current time, it requires traversing the table once for each query, which will be time-consuming for queries with more than one million data points; limit retrieves all results first and then the first one, which is equivalent to taking up unnecessary time and space in the query; and if you need to retrieve the most recent record in batches, for example: "an order table has users, order times, and amounts, and you need to query the most recent order records of all users at one time", then each user's query will require traversing the entire table once. When the data is large, the time will increase exponentially and cannot be put into practical use.

Middle strategy - query sorted and group by

select * from (
  select * from a
  where create_time<="2017-03-29 19:30:36" 
  order by create_time desc
) group by user_id

Later I found that using group by can group by the parameter column of group by, but only one result is returned. After careful observation, I found that group by returns the first record after grouping. By default, the time is sorted in order after querying, so you need to sort the time in reverse order first to retrieve the one closest to the current one.

This query actually performs two queries. Although the time is much faster than the first method, it can be further optimized.

The best strategy is to use the max() method in combination with group by

select *,max(create_time) from a
where create_time<="2017-03-29 19:30:36" 
group by user_id

This sentence can be understood as grouping the result set according to user_id, and taking the record with the maximum time in each group. This makes it possible to query the latest records in batches, and only requires traversing the table once, so that the results can be found in a very short time even when the amount of data is huge.

Extensions:

Now there is an asset equipment table: base_assets_turn

Query the latest custodian of assets

Note: Assume that the asset number ASSETS_ID=254

The next best option:

select * from base_assets_turn 
where ASSETS_ID = 254
order by create_time desc
limit 1

Middle policy:

select * from ( select * from base_assets_turn 
where ASSETS_ID = 254
order by create_time desc) tt GROUP BY tt.ASSETS_ID;

Best strategy:

So how should the best strategy be written? Welcome to leave a message!

The above is the MySQL SQL statement for querying the most recent record (optimization) brought to you by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message!

You may also be interested in:
  • A brief discussion of 30 common methods for optimizing SQL query in MySQL
  • 10 SQL statement optimization techniques to improve MYSQL query efficiency
  • 10 tips for optimizing MySQL SQL statements
  • MySQL SQL statement analysis and query optimization detailed explanation
  • Analyze the sql statement efficiency optimization issues of Mysql table reading, writing, indexing and other operations
  • Tips for optimizing MySQL SQL statements
  • MySQL optimization: how to write high-quality SQL statements
  • 19 common and effective methods for MySQL optimization (recommended!)

<<:  Installation, configuration and use of process daemon supervisor in Linux

>>:  Sample code for a simple seamless scrolling carousel implemented with native Js

Recommend

How familiar are you with pure HTML tags?

The following HTML tags basically include all exis...

Several ways to schedule backup of MySQL database (comprehensive)

Table of contents 1. mysqldump command to back up...

How to customize at and cron scheduled tasks in Linux

There are two types of scheduled tasks in Linux s...

How to implement input checkbox to expand the click range

XML/HTML CodeCopy content to clipboard < div s...

jQuery achieves breathing carousel effect

This article shares the specific code of jQuery t...

How to install common components (mysql, redis) in Docker

Docker installs mysql docker search mysql Search ...

Linux Dig command usage

Dig Introduction: Dig is a tool that queries DNS ...

12 Javascript table controls (DataGrid) are sorted out

When the DataSource property of a DataGrid control...

Rules for registration form design

I finished reading "Patterns for Sign Up &...

MySQL learning record: bloody incident caused by KEY partition

Demand background Part of the data in the busines...

Detailed explanation of tinyMCE usage and experience

Detailed explanation of tinyMCE usage initializat...