Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 8th, 2008, 02:25 PM
Jack
Guest
 
Posts: n/a
Default HAVING and WHERE clauses: Performance considerations in SQL

Hi,

Performance-wise, is there a difference between these 2 clauses?
I assume a large database...

1) With the WHERE clause:

Select FLD1, FLD2, FLD3
From myFile
Where FLD1 = 1 And FLD2 = 1
Group By FLD1, FLD2, FLD3

2) With the HAVING clause:

Select FLD1, FLD2, FLD3
From myFile
Group By FLD1, FLD2, FLD3
Having FLD1 = 1 And FLD2 = 1

thx

Jack
  #2  
Old October 8th, 2008, 05:45 PM
--CELKO--
Guest
 
Posts: n/a
Default Re: HAVING and WHERE clauses: Performance considerations in SQL

First of all, I hope that you know that fields are not anything like
columns and tables are not anything like files. Please tell me that
this was just an unfortunate bunch of data element names and not how
you think of data.

1) With the WHERE clause:

SELECT fld1, fld2, fld3
FROM MyFile
WHERE fld1 = 1
AND fld2 = 1
GROUP BY fld1, fld2, fld3;

The query is to act as if the WHERE clause is done first on the table
named (ugh!) MyFile. Then the resulting work set is grouped --
subsets of rows are reduced to a single row.

That is the model, but the optimizer is free to do this any way it
wants to do it. But since you violated the basic SQL Newsgroup
netiquette, and did not show us any DDL, we have no idea if there is
UNIQUE constraint on any of the columns, what the indexing is, etc.

But a smart optimizer will see the GROUP BY and that fld1 and fld2 are
constants, so we need "buckets" for the fld3 values. If fld3 is
indexed, then we know those values! That means looking at just the
index. Should be fast!

2) With the HAVING clause:

SELECT fld1, fld2, fld3
FROM MyFile
GROUP BY fld1, fld2, fld3
HAVING fld1 = 1
AND fld2 = 1;

This query is to act as if we make all possible groupings of (fld1,
fld2, fld3) in a working table, then applied the having clause.

That is the model, but the optimizer is free to do this any way it
wants to do it. But since you violated the basic SQL Newsgroup
netiquette, and did not show us any DDL, we have no idea if there is
UNIQUE constraint on any of the columns, what the indexing is, etc.

But a smart optimizer will see constants in the HAVING and convert
them into a WHERE clause. The two queries should work out about the
same.

  #3  
Old October 9th, 2008, 02:15 AM
Mark A
Guest
 
Posts: n/a
Default Re: HAVING and WHERE clauses: Performance considerations in SQL

"Jack" <jacquestardif@gmail.comwrote in message
news:3b4b3050-0836-4ee1-9958-ac0382cfb043@i20g2000prf.googlegroups.com...
Quote:
Hi,
>
Performance-wise, is there a difference between these 2 clauses?
I assume a large database...
>
1) With the WHERE clause:
>
Select FLD1, FLD2, FLD3
From myFile
Where FLD1 = 1 And FLD2 = 1
Group By FLD1, FLD2, FLD3
>
2) With the HAVING clause:
>
Select FLD1, FLD2, FLD3
From myFile
Group By FLD1, FLD2, FLD3
Having FLD1 = 1 And FLD2 = 1
>
thx
>
Jack
Generally speaking, the sooner you filter the rows, the less work DB2 will
need to do. That means filtering out the rows in where clause will generally
be faster, or at least not slower.


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles