I am looping through a number of values (1 to 100 for example) and executing a prepared statement inside the loop.
Is there and advantage to using a transaction – committing after the loop ends – compared to a direct execution inside the loop?
The values are not dependant on each other so a transaction is not needed from that point of view.
If your queries are INSERTs, the page 7.2.19. Speed of INSERT Statements of the MySQL manual gives two interesting informations, depending on whether your are using a transactionnal engine or not :
When using a non-transactionnal engine :
And, with a transactionnal engine :
So I am guessing using transactions might be a good idea — but I suppose that could depend on the load on your server, and whether there are multiple uses using the same table at the same moment, and all that…
There are more informations on the page I linked to, so don’t hesitate to read it 😉
And, if you are doing update statements :
So, I’m guessing the same can be said than for inserts.
BTW : to be sure, you can try both solutions, benchmarking them with
microtime, on the PHP side, for instance 😉