View previous topic :: View next topic
|
Author |
Message |
kalaignar_S Warnings : 1 New User
Joined: 06 Jan 2006 Posts: 21
|
|
|
|
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 |
|
|
hikaps14
Active User
Joined: 02 Sep 2005 Posts: 189 Location: Noida
|
|
|
|
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 |
|
|
manisha-s Warnings : 1 New User
Joined: 22 May 2006 Posts: 13
|
|
|
|
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 |
|
|
humpty Warnings : 1 New User
Joined: 01 May 2005 Posts: 23
|
|
|
|
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 |
|
|
|