IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

select empname from emp where city


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
kalaignar_S
Warnings : 1

New User


Joined: 06 Jan 2006
Posts: 21

PostPosted: Thu May 25, 2006 11:18 am
Reply with quote

HI,

i have two queries

1. select empname from emp where city = 'CHENNAI' order by city
2. select empname from emp group by city having city in 'CHENNAI'.


Which one will run fast..


Thanks
Kalaignar
Back to top
View user's profile Send private message
hikaps14

Active User


Joined: 02 Sep 2005
Posts: 189
Location: Noida

PostPosted: Thu May 25, 2006 11:41 am
Reply with quote

Hi ,

i guess most probably the first querry will run faster .

its because it will staright away scan those rows where city = chennai .

where as in 2nd querry

it will first group by for all different cities .
so it will scan more rows n then it will imply the having condition .

i haven't tried these querry out .

it will be better if some one has practical results .

PLZ do correct me if u find me wrong .

Thanks ,
-Kapil .
Back to top
View user's profile Send private message
manisha-s
Warnings : 1

New User


Joined: 22 May 2006
Posts: 13

PostPosted: Thu May 25, 2006 12:16 pm
Reply with quote

Using the WHERE clause appropriately can eliminate unnecessary rows before they get to the GROUP BY and HAVING clause, saving some unnecessary work, and boosting performance.

For example, in a SELECT statement with WHERE, GROUP BY, and HAVING clauses, here's what happens. First, the WHERE clause is used to select the appropriate rows that need to be grouped. Next, the GROUP BY clause divides the rows into sets of grouped rows, and then aggregates their values. And last, the HAVING clause then eliminates undesired aggregated groups. If the WHERE clause is used to eliminate as many of the undesired rows as possible, this means the GROUP BY and the HAVING clauses will have less work to do, boosting the overall performance of the query.


so i think where clause with order by run faster as compared to group by with having clause .
Back to top
View user's profile Send private message
humpty
Warnings : 1

New User


Joined: 01 May 2005
Posts: 23

PostPosted: Thu May 25, 2006 5:03 pm
Reply with quote

right manisha,

The WHER clause will extract all the cities with CHENNAI and then sort the intermediate table based on the ORDER BY clause.

but in the case of GROUP BY, first the sorting will take place and then the selection, therefore there will be wastage of time in sorting and then making selection as in case 2, as compare to case first where the selection will be made in advance and then there will be sorting on very less number of selected records.

thanks.[/b]
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Dynamically pass table name to a sele... DB2 2
No new posts SELECT from data change table DB2 5
No new posts Select two different counts from SQL... DB2 6
No new posts Select a DB2 value in a specific deci... DB2 4
No new posts How can I select certain file dependi... JCL & VSAM 12
Search our Forums:

Back to Top