In general, which is more expensive? A double-nested for loop and one call to a database or a call to a database for each of N items in only one for loop?
Not looking for an answer down to microseconds, just a general idea of which direction I should take.
TIA.
The most common causes of expensive queries are: A lack of relevant indexes, causing slow lookups on large tables. Unused indexes, causing slow INSERT , UPDATE , and DELETE operations. An inefficient schema leading to bad queries.
The query takes 20 to 500 ms (or sometimes more) depending on the system and the amount of data. The performance of the database or the database server has a significant influence on the speed.
In SQL Server, there is no FOR LOOP. However, you simulate the FOR LOOP using the WHILE LOOP.
In general, the fewer times you hit the database, the better. There are a number of reasons for this, including:
In general, anything done in memory (for loop) is faster than the same thing done over a network (database call). However:
for i = 1 to num_users get user from database end
will be slower than
get users 1 to num_users from database (in one query)
because it's the number of times you ask the database for something that really matters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With