简体   繁体   English

PHP PDO批量更新-存在吗?

[英]PHP PDO Batch Update - Exists?

I am looking similar way to jdbc driver in Java, to perform a batch of updates in PHP. 我正在寻找与Java中的jdbc驱动程序类似的方式,以在PHP中执行一批更新。

In jdbc there is an API of PreparedStatement.executeBatch() , which executes the whole statements in one round trip to the DB. 在jdbc中,有一个PreparedStatement.executeBatch()的API,该API在与数据库的一次往返中执行整个语句。

Does PHP PDO has similar API, and if not, does starting transaction, doing the updates and then commit will do the same effect of executing all updates in one round trip to the DB or each update will round trip to the DB and immediately executing the statement (although not visible to others, since it is in transaction)? PHP PDO是否具有相似的API,如果没有,是否启动事务,进行更新然后提交,将具有与数据库一次往返执行所有更新的效果,或者每次更新将往返于数据库并立即执行更新。陈述(尽管对其他人不可见,因为它正在交易中)?

PHP PDO doesn't have batch execution of queries. PHP PDO没有批量执行查询。

Running many inserts and updates in a transaction usually improves greatly the execution speed. 在事务中运行许多插入和更新通常可以大大提高执行速度。 If you're making batch jobs on a database you should run queries in bulks within a transaction. 如果要在数据库上进行批处理作业,则应在事务内批量运行查询。

There is no such thing like "batch update" in Mysql. 在MySQL中,没有像“批处理更新”这样的东西。 There are SQL queries only. 仅存在SQL查询。 As long as you can do your updates in one query, your updates will be done in one round-trip. 只要您可以在一个查询中进行更新,更新就将在一次往返中完成。 Otherwise there would be many. 否则会有很多。 No matter what API being used. 无论使用什么API。

Speaking of single SQL queries, there are 2 possible ways 说到单个SQL查询,有两种可能的方法

  1. CASE statement in WHERE WHERE中的CASE语句
  2. A neat trick with INSERT(!) query with ON DUPLICATE UPDATE statement. 使用ON DUPLICATE UPDATE语句的INSERT(!)查询的巧妙技巧。 Which will actually update your data. 实际会更新您的数据。

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

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