简体   繁体   中英

MySQL Multiple Subqueries vs. whole queries

I'm wondering which way to get data from a MySQL database has better performance characteristics.

Using subqueries within one main query:

SELECT
(SELECT SUM(`number`) FROM `table`) as `number_sum`,
(SELECT MAX(`number`) FROM `table`) as `number_max`,
(SELECT MIN(`number`) FROM `table`) as `number_min`

Or, 3 distinct SELECT statements retrieving the same data.

Thanks in advance!

Since these three aggregates come from the same table with the same WHERE conditions, you have no need for subselects. All three of the aggregates are operating on the same row grouping (no GROUP BY specified, so one row for the whole table), so they can all exist in the SELECT list directly.

SELECT
  SUM(number) AS number_sum,
  MAX(number) AS number_max,
  MIN(number) AS number_min
FROM `table`

If any of the aggregates needs to be based on different conditions you would filter in a WHERE clause, then you will need to either use a subselect for the differing condition, or do a cartesian join. This subselect and the following LEFT JOIN method should be equivalent, performance-wise for aggregates returning only one row:

SELECT
  /* Unique filtering condition - must be done in a subselect */
  (SELECT SUM(number) FROM `table` WHERE `somecolumn` = `somevalue`) AS number_sum,
  MAX(number) AS number_max,
  MIN(number) AS number_min
FROM `table`

Or equivalent to the query above, you can LEFT JOIN against a subquery with no ON clause . This should only be done in situations when you know the subquery will return only one row. Otherwise, you will end up with a cartesian product -- as many rows as returned by one side of the join multiplied by the number of rows returned by the other side.

This is handy if you need to return a few columns with one set of WHERE clause conditions and a few columns with a different set of WHERE conditions, but only one row from each side of the JOIN . In this case, it should be faster to JOIN than to do two subselects with the same WHERE clause.

This should be faster....

SELECT
  /* this one has two aggregates sharing a WHERE condition */
  subq.number_sum_filtered,
  subq.number_max_filtered,
  /* ...and two aggregates on the main table with no WHERE clause filtering */
  MAX(`table`.number) AS number_max,
  MIN(`table`.number) AS number_min
FROM
  `table`
  LEFT JOIN (
    SELECT 
       SUM(number) AS number_sum_filtered,
       MAX(number) AS number_max_filtered
    FROM `table`
    WHERE `somecolumn = `somevalue`
  ) subq /* No ON clause here since there's no common column to join on... */

Than this...

SELECT
  /* Two different subselects each over the same filtered set */
  (SELECT SUM(number) FROM `table` WHERE `somecolumn` = `somevalue`) AS number_sum_filtered,
  (SELECT MAX(number) FROM `table` WHERE `somecolumn` = `somevalue`) AS number_max_filtered,
  MAX(`table`.number) AS number_max,
  MIN(`table`.number) AS number_min
FROM
  `table`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM