tags:

views:

41

answers:

2

I made a few tests, first I tested mysql prepared statement with $pdo->prepare() and $insert_sth->execute() for 10k inserts (with named parameters if it matters), and it took 301s.

After that I made simple insert queries and inserting each time for the same 10k inserts too and it took 303s.

So I would like to know: does prepared statements really give performance benefits? Because my tests didn't show it , or I have to optimize my prepared statements version in order for them to be they faster?

I can give my source code if it's needed.

+1  A: 

INSERTs are most likely IO-bound, since they're generally not very complex in terms of SQL - just a list of columns and data to put in them. Thus, what you use to perform the queries isn't as significant in the run time as the amount of data that you're stuffing into the database, how fast you can get the data to the DB server, and how fast the DB server can store it.

Amber
Yes but in documentation it's told that the query gets cached on the sql server , and you are sending just parameters for the inserts, so the server don't have to check for errors and other repetitive actions, so theoretically ,it should be some performance benefits.
Centurion
Yes, but what I'm saying is that the query in this case is generally pretty tiny relative to the *data*, so you don't see any significant difference because any actual difference is small relative to the time taken by sending the data, which is constant and large.
Amber
Thanks, I will try to test more complicated statements and will see the results.
Centurion
+4  A: 

I prefer prepared statements in terms of security rather than performance (not sure if it is faster) for example to avoid sql injection.

Mike Johnson
I agree with you in terms of security..
Centurion