Home >Database >Mysql Tutorial >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>
Desired Result:
<code>ID | tag | version -----+-----+----- 2 | A | 20 3 | B | 99 4 | C | 30 5 | F | 40</code>
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:
<code class="language-sql">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;</code>
This query works in two steps:
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.
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!