Filtering out rows as soon as possible improves query latency because it reduces the amount of data that must be tracked through the evaluator.
g
, passes each group through the filter (b < 10)
, and then accumulates the records that remain. The input records are not filtered, and the grouping operation must operate on all input records.
RETURN Result AS SELECT SUM(a) WHERE (b < 10) AS sum_a_blt10 FROM SaleState GROUP BY g
WHERE
clause) before the records are passed to the grouping operation. Thus the grouping operation must group only those records of interest to the query. By eliminating records that are not of interest sooner, evaluation will be faster.
RETURN Results AS SELECT SUM(a) AS sum_a_blt10 FROM SaleState WHERE (b < 10) GROUP BY g
WHERE
clauses filters input records and a HAVING
clause filters output records. The first query computes the sum for all values of g
and (after performing all of that computation) throws away all results that do not meet the condition (g < 10)
.
RETURN Result AS SELECT SUM(a) AS sum_a FROM SaleState GROUP BY g HAVING g < 10
RETURN Result AS SELECT SUM(a) AS sum_a FROM SaleState WHERE g < 10 GROUP BY g