M_1
M_1

Reputation: 2155

Which SQL statement is faster? (HAVING vs. WHERE...)

SELECT NR_DZIALU, COUNT (NR_DZIALU) AS LICZ_PRAC_DZIALU
    FROM  PRACOWNICY
    GROUP BY NR_DZIALU
    HAVING NR_DZIALU = 30

or

SELECT NR_DZIALU, COUNT (NR_DZIALU) AS LICZ_PRAC_DZIALU
    FROM PRACOWNICY
    WHERE NR_DZIALU = 30
    GROUP BY NR_DZIALU

Upvotes: 54

Views: 58029

Answers (8)

Mike Woodhouse
Mike Woodhouse

Reputation: 52326

The two queries are equivalent and your DBMS query optimizer should recognise this and produce the same query plan. It may not, but the situation is fairly simple to recognise, so I'd expect any modern system - even Sybase - to deal with it.

HAVING clauses should be used to apply conditions on group functions, otherwise they can be moved into the WHERE condition. For example. if you wanted to restrict your query to groups that have COUNT(DZIALU) > 10, say, you would need to put the condition into a HAVING because it acts on the groups, not the individual rows.

Upvotes: 8

Nitish Bhardwaj
Nitish Bhardwaj

Reputation: 31

"Having" is slower if we compare with large amount of data because it works on group of records and "WHERE" works on number of rows..

"Where" restricts results before bringing all rows and 'Having" restricts results after bringing all the rows

Upvotes: 3

Marius Gri
Marius Gri

Reputation: 59

"WHERE" is faster than "HAVING"!

The more complex grouping of the query is - the slower "HAVING" will perform to compare because: "HAVING" "filter" will deal with larger amount of results and its also being additional "filter" loop

"HAVING" will also use more memory (RAM)

Altho when working with small data - the difference is minor and can absolutely be ignored

Upvotes: 3

Manoj Pandey
Manoj Pandey

Reputation: 1397

Both the statements will be having same performance as SQL Server is smart enough to parse both the same statements into a similar plan.

So, it does not matter if you use WHERE or HAVING in your query.

But, ideally you should use WHERE clause syntactically.

Upvotes: 0

programmer
programmer

Reputation: 29

Saying they would optimize is not really taking control and telling the computer what to do. I would agree that the use of having is not an alternative to a where clause. Having has a special usage of being applied to a group by where something like a sum() was used and you want to limit the result set to show only groups having a sum() > than 100 per se. Having works on groups, Where works on rows. They are apples and oranges. So really, they should not be compared as they are two very different animals.

Upvotes: 2

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340296

The theory (by theory I mean SQL Standard) says that WHERE restricts the result set before returning rows and HAVING restricts the result set after bringing all the rows. So WHERE is faster. On SQL Standard compliant DBMSs in this regard, only use HAVING where you cannot put the condition on a WHERE (like computed columns in some RDBMSs.)

You can just see the execution plan for both and check for yourself, nothing will beat that (measurement for your specific query in your specific environment with your data.)

Upvotes: 89

Eran Galperin
Eran Galperin

Reputation: 86805

It might depend on the engine. MySQL for example, applies HAVING almost last in the chain, meaning there is almost no room for optimization. From the manual:

The HAVING clause is applied nearly last, just before items are sent to the client, with no optimization. (LIMIT is applied after HAVING.)

I believe this behavior is the same in most SQL database engines, but I can't guarantee it.

Upvotes: 12

ysth
ysth

Reputation: 98398

I'd expect the WHERE clause would be faster, but it's possible they'd optimize to exactly the same.

Upvotes: 2

Related Questions