简体   繁体   English

当对数据数组进行操作时,PDO是否真的比mysql_ *快?

[英]Is PDO really quicker than mysql_* when operating on arrays of data?

Ive recently upgraded my mind from mysql_* to PDO, and I have one simple question: 我最近将我的想法从mysql_ *升级到了PDO,我有一个简单的问题:

Is PDO really that much more efficient that the use of a prepared statement and an execute in a for-each loop is quicker than a single call in mysql with multiple values in it? PDO真的比在mysql中包含多个值的单个调用要快得多,比在for-each循环中使用预处理语句和execute更快吗?

For example if I have an array of 5 names, putting these in an execute command in a for loop operating on an 'insert' prepared statement - is calling this 5 times going to be quicker in computational speed that one call using the old mysql with all 5 values in a single query? 例如,如果我有一个由5个名称组成的数组,则将它们放在对“插入”准备好的语句for操作的for循环中的execute命令中-调用此5次将使计算速度比使用旧mysql调用一次更快一个查询中所有5个值? Or is it preferred due to security rather than speed alone? 还是由于安全性而不是仅仅因为速度而被首选?

The meaning and significance of native prepared statements (which you call "PDO") is overlooked and misjudged by everyone. 每个人都忽略和误判了本机准备语句 (您称为“ PDO”)的含义和重要性。

The speed benefit, everyone talking about so much, in reality can be achieved extremely rare, and often unnoticeable at all. 所有人都在谈论如此多的速度收益,实际上是可以实现的,这是极为罕见的,而且通常一点都不值得注意。 Especially in the area of web-development with PHP which PDO belongs to. 特别是在 PDO所属的使用PHP 进行Web开发的领域中
Also note that whatever speed benefit belongs to the query parsing only - no such matters like index rebuilding or time required to find a record to update ever affected by prepared statements. 还要注意,无论速度带来什么好处,都仅属于查询解析-不会受到诸如索引重建或找到要更新的记录所需的时间之类的问题的影响,而这些都受准备语句的影响。

So, speaking of numbers like five, don't bother yourself with this "once-prepare-multiple execute" thing. 因此,谈到5之类的数字时,不要为“一次准备多次执行”而烦恼自己。 It is not what PDO is about. 这与PDO无关。 PDO does two essential things, which makes it preferred over two other possible extensions: PDO做两项重要的事情,这使其比其他两个可能的扩展更受欢迎:

  • it supports prepared statements in general, allowing data in the query not directly but via placeholder. 它通常支持准备好的语句,从而允许查询中的数据不直接而是通过占位符。 This is the only reason why you should use PDO or similar lib (although you can easily make even old mysql ext to support prepared statements, but PDO offers it out of the box) 这是为什么您应该使用PDO或类似的lib的唯一原因(尽管您可以轻松地制作甚至旧的mysql ext来支持准备好的语句,但是PDO提供了开箱即用的功能)
  • it makes such support not as painful as mysqli 它使这种支持不像mysqli那样痛苦

Turning back to your question: 回到您的问题:

  1. You can use either way you like. 您可以使用任意一种方式。 Just remember that multiple inserts are better to be wrapped in a transaction, due to default settings of the modern DB engines 请记住,由于现代数据库引擎的默认设置,多个插入最好包裹在一个事务中
  2. No matter which way you choose, any dynamical value should be added into query via placeholders only . 无论选择哪种方式, 任何动态值都应仅通过占位符添加到查询中。 If you still not convinced, you are welcome to read an article I wrote on the matter (which is still incomplete, but have a through explanation on the real meaning of prepared statements) . 如果您仍然不确定,欢迎您阅读我写的有关此问题文章(该文章仍不完整,但对准备好的陈述的真实含义有透彻的解释)

PS. PS。 There is also one minor benefit of native prepared statements, often forgotten (becaulse seldom demanded) - if native prepared statement were used (and backed by msqlnd driver), the data returned is already formatted according to its type. 本机准备语句还有一个小好处,通常会被遗忘(因为很少需要)-如果使用本机准备语句(并由msqlnd驱动程序支持),则返回的数据已经根据其类型进行了格式化。

One query that fetches 5 rows will probably be quicker than 5 separate calls, so you are comparing apples and oranges. 一个提取5行的查询可能比5个单独的调用要快,因此您要比较苹果和橙子。

When executing the same query, the performance will be similar too. 当执行相同的查询时,性能也将相似。 The (small) performance advantage that PDO has, is that queries with parameters are supposed to be better cachable. PDO所具有的(较小)性能优势是,具有参数的查询应该可以更好地实现。 When querying customer 3 and customer 5 , the query will be cached as two different queries, while only the id is different. 当查询客户3和客户5 ,该查询将被缓存为两个不同的查询,而唯一的id是不同的。 By using parameters, the database might cache the query in a smarter way, so a second call with a different input doesn't need to go through the query optimizer and such. 通过使用参数,数据库可以以更智能的方式缓存查询,因此,第二次调用具有不同的输入就无需通过查询优化器等。

That said, apart from the performance advantage, PDO is also safer (when actually using paramteres), and in the end easier. 也就是说,除了性能优势外,PDO也更安全(当实际使用参数时),最终更容易实现。 It may look more complex at first, but it is easier to do right, because without using parameters, you will need to do all the escaping yourself, risking dangerous bugs. 乍一看,它看起来可能更复杂,但是正确操作起来更容易,因为如果不使用参数,您将需要自己进行所有转义操作,冒着危险的错误的风险。

By the way, you can also build a query with a variable number of parameters, and bind a value to each of them in a loop, so with PDO you could still perform the single insert query for 5 rows, although it will need a bit puzzling and a bit of extra code. 顺便说一句,您还可以使用可变数量的参数构建查询,并在循环中将其绑定到每个参数,因此使用PDO,您仍然可以对5行执行单个插入查询,尽管这需要一点点令人费解和一些额外的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM