PDO ::查询与PDOStatement :: execute(PHP和MySQL)

时间:2022-06-05 01:16:45

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).

我已经扩展了PDO类来创建一个简单的DB类,并且当前对运行到数据库的所有查询使用prepare + execute,甚至是那些没有参数的查询(例如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?

问题是:对于没有参数的简单查询而不是prepare / execute,实际使用PDO :: query是否有性能优势?

2 个解决方案

#1


8  

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.

是的,因为当您调用PDO :: prepare时,服务器必须为该查询创建查询计划和元信息,然后在使用PDO :: execute时绑定指定的参数会产生额外的开销。因此,为了节省此开销并提高性能,您可以将PDO :: query用于不带参数的查询。

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.

但是,根据应用程序的规模和大小以及服务器/主机配置(共享/私有),您可能会或可能看不到任何性能提升。

#2


1  

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.

在PHP中以两种不同的方式做任何一件事都有一个可衡量的区别。您应该评估每种方法对您的价值,并创建测试用例,以确定您是否有必要以某种方式做事。

#1


8  

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.

是的,因为当您调用PDO :: prepare时,服务器必须为该查询创建查询计划和元信息,然后在使用PDO :: execute时绑定指定的参数会产生额外的开销。因此,为了节省此开销并提高性能,您可以将PDO :: query用于不带参数的查询。

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.

但是,根据应用程序的规模和大小以及服务器/主机配置(共享/私有),您可能会或可能看不到任何性能提升。

#2


1  

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.

在PHP中以两种不同的方式做任何一件事都有一个可衡量的区别。您应该评估每种方法对您的价值,并创建测试用例,以确定您是否有必要以某种方式做事。