Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

The advice to not perform SQL statements in loops is given often. However, it seems to me that prepared statements are in fact designed for use in loops. That is, the statement should be prepared before the loop, and the execution with differing parameters should be done inside the loop. I base this on the fact that with prepared statements much resources are devoted to actually preparing the statement, but then the actual execution seems to be very swift.

In researching this, I am finding conflicting opinions online. Where might I find a definite resource which examines and answers this issue? I have only the facilities to benchmark with PHP and MySQL on a LAMP stack, so I would not extrapolate those results to other platforms. I would also like to hear DBAs' personal experience here, but if so then please mention why it is adviced to never use SQL queries in loops, and why that advice does not apply to prepared statements (opinion is not allowed on SO sites).

For my specific use case I am looking at MySQL 5.5 with many (tens) of INSERT statements. However I am interested in the general case as well, for other types of statements and also for other databases. Thank you!

share|improve this question
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.