PDO :: query与PDOStatement :: execute(PHP和MySQL) [英] PDO::query vs. PDOStatement::execute (PHP and MySQL)

查看:79
本文介绍了PDO :: query与PDOStatement :: execute(PHP和MySQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经扩展了PDO类以创建一个简单的DB类,并且当前对所有运行到数据库的查询(甚至是那些没有参数(例如SELECT * FROM table)的查询)都使用prepare + execute.

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 :: query有性能上的好处吗?

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?

推荐答案

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

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.

这篇关于PDO :: query与PDOStatement :: execute(PHP和MySQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆