简体   繁体   English

执行此UPDATE查询的最佳方法

[英]Best way to do this UPDATE query

I have to do a update query of 800k rows and looking for the best way to do this. 我必须对80万行进行更新查询,并寻找执行此操作的最佳方法。 All rows are updated with the same values excepted one field (D in my exemple). 除了一个字段(在我的示例中为D)以外,所有行均以相同的值更新。 This field can be 1 or 0. I use update() methode of Zend_Db. 该字段可以是1或0。我使用Zend_Db的update()方法。 I think about 3 methods to do this : 我认为有3种方法可以做到这一点:

  • Methode 1 : Update each row, one after one (with a foreach). 方法1:更新每一行,一个接一个地更新(带有foreach)。

  • Methode 2 : Do an IF in the update to set the value of the field 方法2:在更新中执行IF以设置字段的值

  • Methode 3 : Divide rows in two groups (one with field = 1 and another with field = 0) and make two updates (UPDATE ... WHERE id IN (...)), one for each group. 方法3:将行分为两组(一组字段= 1,另一组字段= 0)并进行两次更新(UPDATE ... WHERE ID IN(...)),每组更新一次。

Query looks like this : 查询看起来像这样:

$a_data = array(
  'A' => foo,
  'B' => 99,
  'C' => 0,
  'D' => (0 OR 1 ?)
);

$where['id IN (?)'] = $a_id;
$update = $this->_db->update($this->_name, $a_data, $where);

Witch method can be the best way to do this ? 巫婆方法可以做到这一点的最好方法吗? Thanks 谢谢

For the record, 800k rows updated on a live production server isn't a good plan. 据记录,在实时生产服务器上更新80万行不是一个好的计划。 Except being done at an actual mysql level, the chances of this update stopping your server are high. 除了在实际的mysql级别上进行之外,此更新停止服务器的几率很高。

Now, that being said, and assuming you're running MySql, 话虽如此,假设您正在运行MySql,

Method 1. isn't feasible if for nothing else than that you have 800k rows => 800k queries. 如果您仅拥有800k行=> 800k查询,那么方法1.是不可行的。 max_timeout in php.ini will not allow for the script to run that long. php.ini中的max_timeout不允许脚本运行那么长时间。 If you still want to try it, try splicing the results into batches of 50-100-200 (depending on your server configuration) and run each batch with a time difference between them. 如果仍要尝试,请尝试将结果拼接成50-100-200的批次(取决于您的服务器配置),然后运行每个批次并在它们之间存在时间差。 Do a batch, wait a second, do a batch, wait a second, and so on... 做一批,等一下,做一批,等一下,依此类推...

Method 2. i guess it pertains to your certain problem, but it will be quicker. 方法2。我认为它与您的特定问题有关,但是会更快。

Method 3. see answer for Method 1, except it's not 800k at once, but depends on the ratio between your 0 and 1's. 方法3。请参阅方法1的答案,但不是一次即可获得800k,而是取决于0与1之间的比率。 It's going to be 2 queries each pretty large. 这将是两个非常大的查询。

Usually, when there's a large batch update like this, I'd say, use mysql from a command line. 通常,当有这样的大批量更新时,我会说,从命令行使用mysql。 If this is an update php script that you're running, the best results are splicing the results and updating 50-100-whatever number at a time. 如果这是您正在运行的更新php脚本,则最好的结果是拼接结果并一次更新50-100(无论多少)。 Although it's time consuming (800.000rows / 100rows at a time = 800 runs of the script + a pause of a second after every updated batch). 尽管这很耗时(一次800.000行/ 100行=脚本运行800次+每批更新后暂停一秒钟)。

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

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