Reputation: 654
I've extended the PDO class to create a simple DB class and currently use prepare + execute for all queries run to the database, even the ones that do not have parameters (e.g. SELECT * FROM table).
The question is: is there a performance benefit to actually use PDO::query for simple queries that do not have parameters, instead of prepare/execute?
Upvotes: 10
Views: 2068
Reputation: 336
There is a measurable difference between doing any one thing two different ways in PHP. You should assess the value that each method has for you and create test cases to see if it is worth it for you to do things one way or another.
Upvotes: 2
Reputation: 59709
Yes, because when you call PDO::prepare
, the server must create a query plan and meta information for that query, then there is additional overhead to bind the specified parameters when you use PDO::execute
. So, to save this overhead and improve performance, you can use PDO::query
for queries without parameters.
However, depending on the scale and size of your application, and your server / host configuration (shared / private), you may or may not see any performance increase at all.
Upvotes: 10