I am looking to iterate through a USERS table, and for each user selected based on my where clauses, perform a bunch of mysql queries within the LOOP, including temp table creates, using prepare statements, etc
What is the best way in terms of performance should this be done. Using while, for each, for, repeat ?
eg.(Pseudo code)
foreach user_name in (select user_name from users where user_type = 'SP' and active = 'Y')
do
set @l_query1 = concat("create temp_table_t1 select * from ...")
PREPARE ..
EXECUTE ...
set @l_query2 = ...
....
.... etc ..
done
Ok, i have a table users, where pK = user_name.
So,this query will be fast to execute,
I just need to LOOP through this resultSet, and execute the remaining of the mySQL queries within the LOOP.
How can i LOOP through the resultSet of a simple select ?
thanks
There’s no difference in performance between while, for-each, repeat, etc.
If you’re looking for performance gains, consider where you might reduce the number of queries the code executes on the database. For example, it would be a huge help to run a query once for all users, rather than running it once each for every user.