Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the least expensive aggregate function in the absence of ANY()

Tags:

sql

tsql

mysql

I usally use MAX() or MIN() if a DBMS hasn't an ANY() aggregate function.

Is there something less expensive in mySQL and MS-SQL?

like image 679
Luis Siquot Avatar asked May 19 '11 14:05

Luis Siquot


People also ask

Is AVG () an aggregate function?

AVG() function is an aggregate function that calculates the average value of a numerical dataset that returns from the SELECT statement.

Is MIN () an aggregate function?

The MIN() aggregate function returns the lowest value (minimum) in a set of non-NULL values.

What is the purpose of sum () and Count () aggregate functions?

COUNT: It is used to count the number of rows in a set. the COUNT function includes rows with NULL values. SUM: This is used to calculate the sum of all non-NULL values in a group.

What aggregate function returns the value with the lowest value?

The aggregate function MIN() is used to find the minimum value or lowest value of a certain column or expression or a set of values. It is applicable to all the data types.


3 Answers

As of MySQL 5.7 (released October 2015), there is actually a function for this! any_value(col) explicitly meets this need - see the documentation for details.

However, it's important to note that it still appears to not guarantee a short-circuit and may still perform a full scan, so the goal of higher efficiency may not be met. If MySQL is intelligent about it, there is some chance, though testing would be wise. There are two circumstances in which it may be helpful anyway:

  • If the value to be aggregated is particularly large and unindexed, such that any comparisons would be expensive.
  • If the intention in the code would be made less clear by leveraging max or another placeholder aggregate. For example, a max may imply there was some reason for its existence rather than getting "anything" from the grouping, but any_value would explicitly define the intention without additional commenting.

Toy sample:

select
col, 
any_value(a.val) as any_val
from (
    select 'blue' as col, 3 as val
    union all
    select 'blue' as col, 2 as val
    union all
    select 'green' as col, 1 as val
) as a
group by col
like image 94
bsplosion Avatar answered Sep 27 '22 16:09

bsplosion


MIN and MAX are equally (in)expensive.

like image 31
Jonathan Leffler Avatar answered Sep 27 '22 16:09

Jonathan Leffler


MySQL does not need an ANY() aggregate.

if I do a

SELECT field1, field2, SUM(field3) FROM table1 
GROUP BY field2

Microsofts T-SQL will complain but

MySQL will just silently execute

SELECT whatever(field1), field2, SUM(.... 

Which of course is way faster than SELECT max(field1), field2, SUM(.... GROUP BY field2

MySQL supports ANY, but SELECT ANY(field1) ... doesn't work because ANY is an operator similar to IN and ALL.
see: http://dev.mysql.com/doc/refman/5.0/en/any-in-some-subqueries.html

I love MySQL

like image 24
Johan Avatar answered Sep 27 '22 17:09

Johan