Home > Database > Mysql Tutorial > How Can I Optimize a Query to Efficiently Find the Groupwise Maximum?

How Can I Optimize a Query to Efficiently Find the Groupwise Maximum?

Linda Hamilton
Release: 2024-12-26 10:51:10
Original
578 people have browsed it

How Can I Optimize a Query to Efficiently Find the Groupwise Maximum?

Optimized Query for Efficient Groupwise Maximum

Original Problem:

The query

select * 
from records 
where id in ( select max(id) from records group by option_id )
Copy after login

performs a sequential scan of the entire records table to determine the maximum ID for each option_id. This approach is inefficient, especially for large tables.

Solution: Lateral Join

One solution is to leverage a lateral join to fetch the maximum ID for each option_id within the subquery:

select r.*
from records r
cross join lateral (
    select max(id) as max_id
    from records
    where option_id = r.option_id
) m
where r.id = m.max_id
Copy after login

This query uses a lateral join to calculate the maximum ID in a separate subquery. The result is joined with the original records table to filter for only the rows with the maximum ID.

Creating a Specialized Index

Another optimization is to create a specialized index on the records table that stores the maximum ID for each option_id:

CREATE INDEX idx_max_id ON records (option_id, max(id))
Copy after login

This index enables a direct lookup of the maximum ID for a given option_id, eliminating the need for the original subquery:

select * 
from records r
where (option_id, id) in (
    select option_id, max(id) from records group by option_id
)
Copy after login

The index-based approach significantly reduces the number of table accesses, making the query more efficient for large tables.

The above is the detailed content of How Can I Optimize a Query to Efficiently Find the Groupwise Maximum?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template