Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

group by vs. select loop

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello,
I wanted to consult you with a little dilema I have.
I have a table in my db that looks like this -
id,zone_name
id is unique, there can be many id using the same zone_name.
I need to count how many id's I have for each zone_name.
I have a list of zone_names to be checked.
Zone_List={X,Y,.....}
The lenght of the array is changing and can be between 0 to 10000.
I have 2 ways of doing it:
one is by using a GROUP BY query as followed -

select zone_name,count(id) from <table> where zone_name in Zone_List group by zone_name

Or,the other is by peforming select queries in a loop:

for (i=0;i<Zone_List.length;i++){
select zone_name,count(id) from <table> where zone_name=Zone_List
}

Which is a better way to do it (SQL performance)?
Thanks

 
calling the database from inside a loop is almost always much slower than calling the database once and letting it return a set of results

rudy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top