Php 有没有办法更新12000+;在不到2分钟的时间内从txt文件中删除行?

Php 有没有办法更新12000+;在不到2分钟的时间内从txt文件中删除行?,php,mysql,codeigniter,mysqli,Php,Mysql,Codeigniter,Mysqli,我需要使用php Codeigniter和txt文件更新一个超过12000行的表。。读取文件和foreach循环都很好,但是逐行更新需要30分钟,我想问题是我正在按名称搜索,因为我在txt文件中没有id。。。 这是我的密码: 控制器: 型号: 公共函数更新3s($product){ 如果($产品){ $product[2]=修剪(str_替换(“,”,“,$product[2]); $product[1]=修剪($product[1]); $product[6]=修剪($product[6]);

我需要使用php Codeigniter和txt文件更新一个超过12000行的表。。读取文件和foreach循环都很好,但是逐行更新需要30分钟,我想问题是我正在按名称搜索,因为我在txt文件中没有id。。。 这是我的密码:

控制器:

型号:

公共函数更新3s($product){
如果($产品){
$product[2]=修剪(str_替换(“,”,“,$product[2]);
$product[1]=修剪($product[1]);
$product[6]=修剪($product[6]);
$product[3]=修剪($product[3]);
$sql=“更新产品集数量=$product[3],价格=$product[6],其中(名称=“$product[2]”);
回显$sql。“
”; $update=$query=$this->db->query($sql); 返回$update; } 返回false; }
您可以使用事务并为数据库表中的列
名称添加索引

$fn = fopen($this->upload->data('full_path'),"r");

$update = true;

$updatedCount = 0;

while(! feof($fn) && $update)  {
    $pieces = explode("|", fgets($fn));
    if(sizeof($pieces) == 9 && is_numeric(trim($pieces[1]))) {
      if ($updatedCount == 0) {
         $databaseInstance->beginTransaction();
      }
      $update = $this->model_products->update3s($pieces);
      ++$updatedCount;

      if ($updatedCount > 500) { //in one transaction update 500 rows
         $databaseInstance->commit();
         $updatedCount = 0;
      }
    }
} 

if ($updatedCount > 0) { // if we have not commited transaction 
   $databaseInstance->commit();
}

fclose($fn);

您可以使用事务并为数据库表中的列
name
添加索引

$fn = fopen($this->upload->data('full_path'),"r");

$update = true;

$updatedCount = 0;

while(! feof($fn) && $update)  {
    $pieces = explode("|", fgets($fn));
    if(sizeof($pieces) == 9 && is_numeric(trim($pieces[1]))) {
      if ($updatedCount == 0) {
         $databaseInstance->beginTransaction();
      }
      $update = $this->model_products->update3s($pieces);
      ++$updatedCount;

      if ($updatedCount > 500) { //in one transaction update 500 rows
         $databaseInstance->commit();
         $updatedCount = 0;
      }
    }
} 

if ($updatedCount > 0) { // if we have not commited transaction 
   $databaseInstance->commit();
}

fclose($fn);
一些提示

  • 将索引添加到字段
    name
  • 使用事先准备好的陈述
  • 禁用MySQL伪造密钥检查
一些提示

  • 将索引添加到字段
    name
  • 使用事先准备好的陈述
  • 禁用MySQL伪造密钥检查

    • 编写sql函数可以在更短的时间内完成这项工作。 使用以下功能:

      在mysql用户定义的

      如果它是CI 3,那么只需通过

      $this->db->call_function('update3s', file_get_contents($this->upload->data('full_path')));
      
      否则


      编写sql函数可以在更短的时间内完成这项工作。 使用以下功能:

      在mysql用户定义的

      如果它是CI 3,那么只需通过

      $this->db->call_function('update3s', file_get_contents($this->upload->data('full_path')));
      
      否则


      为表编制索引可以简单地减少time@AkshitAhuja我有一个主键(ai int),但在txt文件中唯一唯一的是名称..在
      名称中添加唯一索引
      ,使用参数化请求,在循环前准备语句一些提示:**如果您更新一个表,例如
      updatetable SET qty=(当名称为'product_1'时,则为1 ELSE NULL END),则可以将多个更新语句与一个CASE END cluase合并,其中name IN(…)
      。。。请记住在“脱机”数据上写入/测试此项。。。此外,使用事务还可以加快多个更新查询的速度,并且不要忘记为更新的WHERE子句中使用的列编制索引。为表编制索引只需减少time@AkshitAhuja我有一个主键(ai int),但在txt文件中唯一唯一唯一的是名称..将唯一索引添加到
      名称
      ,使用参数化请求,在循环提示之前准备语句:**如果您更新一个表,例如
      updatetable SET qty=(当name='product\u 1'时,CASE-ELSE-NULL-END),则可以将多个update语句与一个CASE-END cluase合并,其中name位于(…)
      。。。请记住在“脱机”数据上写入/测试此项。。。此外,使用事务还可以加快多个更新查询的速度,并且不要忘记为更新的WHERE子句中使用的列编制索引。
      $this->db->call_function('update3s', file_get_contents($this->upload->data('full_path')));
      
      $this->db->query("select update3s(".file_get_contents($this->upload->data('full_path')).")");