Home > Database > Mysql Tutorial > How to Efficiently Select One Row per Group from a Large Database Table?

How to Efficiently Select One Row per Group from a Large Database Table?

Barbara Streisand
Release: 2024-12-31 01:04:08
Original
273 people have browsed it

How to Efficiently Select One Row per Group from a Large Database Table?

Efficiently Selecting One Row from Each Group

In a large product table with millions of records, optimizing queries is crucial for performance. A common scenario is retrieving a single row from each group of a table based on a specific column, such as the maximum ID for each store.

Original Query and Performance Issue:

The initial query used for this task was:

SELECT id, product_name, store_id
FROM product
GROUP BY store_id
ORDER BY id.
Copy after login

However, this query faced performance issues due to the grouping and aggregation operations involved.

Recommended Approach:

An efficient solution is to use a subquery to determine the maximum ID for each store and join it to the main table:

SELECT  a.*
FROM    tableName a
        INNER JOIN
        (
            SELECT  store_ID, MAX(ID) max_ID
            FROM    tableName
            GROUP BY store_ID
        ) b ON a.store_ID = b.store_ID AND
                a.ID = b.max_ID
Copy after login

This query performs the grouping and aggregation in the subquery, resulting in better performance.

Additional Performance Tips:

  • Ensure that the table has indexes on the ID and store_id columns.
  • Consider using a database server with support for parallelization and optimization features.

Multiple Row Selection:

If multiple rows need to be retrieved from each group, the following query can be used:

SELECT ID, product_Name, store_ID
FROM   tableName a
WHERE
  (
     SELECT COUNT(*) 
     FROM   tableName b
     WHERE  b.store_ID = a.store_ID AND b.ID >= a.ID
  ) <= 2;
Copy after login

This query retrieves up to two rows from each group based on the ID column.

The above is the detailed content of How to Efficiently Select One Row per Group from a Large Database Table?. 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