简体   繁体   English

如何使用Codeigniter使用批处理更新/插入SQL?

[英]How to use batch update/insert sql using codeigniter?

in this example.. how can i simplified and fasten up the update and insert of data in the database? 在这个例子中..如何简化和加快数据库中数据的更新和插入?

//if count($arr_list['sample_element1'] is 500+++ or more records

$i =0;
while ($i < count($arr_list['sample_element1'])) {

$update_db= array('column1' => $arr_list['sample_element1'][$i],'column2' => $arr_list['sample_element2'][$i]);

$this->db->update('sample_table',$update_db);

$i++;
}

if the record to be save is over 1000 data...it will take about sometime to finish the process.. thank in advance.. =) 如果要保存的记录超过1000条数据...大约需要一些时间才能完成该过程..预先感谢.. =)

use batch_update 使用batch_update

$this->db->update(table_name,array,where_key_word);

here table_name = your table name 这里table_name =您的表名

and array = an array containing multiple associative array array =包含多个关联数组的数组

and where_key_word = the column which should be used in where condition where_key_word =在where条件中应使用的列

also read this question thread for more information 另请阅读此问题主题以获取更多信息

batch updating thousands of element together may create performance issue.Better split them up and update by smaller batch.Also use transactions so that if any of the update fails the whole update procedure can be rolled back 批量更新数千个元素可能会导致性能问题。更好地将它们拆分并以较小的批次进行更新。还使用事务处理,以便如果任何更新失败,则可以回滚整个更新过程

Taking @AL-zami's answer you're looking for this 采取@ AL-zami的答案,您正在寻找这个

$data = [];
$this->db->trans_start();
foreach($arr_list as $item)
{
    $data[] = [
        'column1' => $item['element1'],
        'column2' => $item['element2'],
        'column3' => $item['element3']
    ];
}
$this->db->update_batch('mytable', $data, 'column1');
$this->db->trans_complete();

Where 'column1' is the column used for the "WHERE" condition. 其中“ column1”是用于“ WHERE”条件的列。

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

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