Home > Database > Mysql Tutorial > How to Efficiently Retrieve the Maximum Value and Corresponding Data from a Large SQL Database Table?

How to Efficiently Retrieve the Maximum Value and Corresponding Data from a Large SQL Database Table?

Susan Sarandon
Release: 2025-01-07 17:21:41
Original
724 people have browsed it

How to Efficiently Retrieve the Maximum Value and Corresponding Data from a Large SQL Database Table?

Efficiently Finding Maximum Values and Associated Data in Large SQL Tables

Often, database queries require finding the maximum value in a column and retrieving the corresponding data from other columns within the same row. This is especially challenging with very large datasets. Consider a table needing to find the highest version number for each unique ID, along with its associated tag:

Sample Table:

<code>ID  |  tag  |  version
-----+-----+-----
1 |  A  |  10
2 |  A  |  20
3 |  B  |  99
4 |  C  |  30
5 |  F  |  40</code>
Copy after login

Desired Result:

<code>ID  |  tag  |  version
-----+-----+-----
2  |  A  |  20
3  |  B  |  99
4  |  C  |  30
5  |  F  |  40</code>
Copy after login

For tables with around 28 million rows, standard methods like nested SELECT statements or simple GROUP BY with MAX can be incredibly slow. A much more efficient solution uses the ROW_NUMBER() window function:

SELECT s.id, s.tag, s.version
FROM (
  SELECT t.*,
         ROW_NUMBER() OVER(PARTITION BY t.id ORDER BY t.version DESC) AS rnk
  FROM YourTable t
) s
WHERE s.rnk = 1;
Copy after login

This query works in two steps:

  1. Inner Query: It assigns a unique rank (rnk) to each row within each ID partition (group of rows with the same ID). The ranking is based on the version column in descending order, meaning the highest version gets rank 1.

  2. Outer Query: It filters the results from the inner query, selecting only the rows where rnk = 1. This effectively gives us the row with the maximum version for each ID.

This approach avoids nested queries and GROUP BY operations, making it significantly faster for large datasets. The use of ROW_NUMBER() provides a clean and efficient way to achieve the desired outcome.

The above is the detailed content of How to Efficiently Retrieve the Maximum Value and Corresponding Data from a Large SQL Database Table?. For more information, please follow other related articles on the PHP Chinese website!

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