In my basic understanding of an index, the index is used on a column in a WHERE clause. Since the HAVING clause is similar to a WHERE clause applied after a GROUP BY statement, does an index have the same effect on that? For example:
SELECT * FROM table WHERE full_name = 'Bob Jones'
--> index on full_name would be beneficial here
and
SELECT * FROM table WHERE first_name = 'Bob'
GROUP BY
height HAVING height > 72
In this second query, would an index on both first_name and height improve the performance? Which index would be more important, or are they roughly the equivalent? Also, do indexes improve GROUP BY performance as well (regardless of a HAVING)?
A
HAVINGclause is essentially the last thing done to filter a query’s results before they’re sent off to the client. It’s only useful if you need to filter on the results of an aggregate function, whose value can NOT be available during the row-level filtering thatWHEREclauses do.Essentially, a
HAVINGclause can be seen as applying another query, turning your main query into a subquery.e.g.
is really no different that
If the field you’re filtering with the HAVING is NOT a derived field (aggregate value, calculated field, etc…) then you’re almost certainly better off doing the filtering at the WHERE level, which keeps unecessary rows from being loaded off disk in the first place.
Since having is applied last, rows will be loaded from disk, then possibly discarded if they don’t match the HAVING criteria.