我正在使用insert_batch()
向数据库中的表中大量插入10000多行。我正在进行一些测试,我注意到有时候所有10.000多行都被正确插入,但在某些情况下,我会错过表格总数中的100多行。
我所拥有的记录中的字段数据是可以的,因为我为每个测试使用相同的数据,而且大多数时候我都没有问题。例如,我尝试了20次将相同的数据插入到我的数据库中,并且19次所有行都将正确插入,但在这一次我将错过100行或更多行。
insert_batch()
的功能如下:
protected function save_sms_to_database() {
//insert_Batch
$datestring = "%Y-%m-%d %h:%m:%s";
$time = time();
$datetime = mdate($datestring, $time);
$this->date_sent = $datetime;
foreach ($this->destinations as $k => $v) {
$sms_data[$k] = array(
'campaign_id' => $this->campaign_id,
'sender_id' => $this->from,
'destination' => $v,
'token' => md5(time() . 'smstoken' . rand(1, 99999999999)),
'message' => $this->body,
'unicode' => $this->unicode,
'long' => $this->longsms,
'credit_cost' => $this->eachMsgCreditCost,
'date_sent' => $this->date_sent,
'deleted' => 0,
'status' => 1,
'scheduled' => $this->scheduled,
);
}
$this->ci->db->insert_batch('outgoingSMS', $sms_data);
if ($this->ci->db->affected_rows() > 0) {
// outgoingSMS data were successfully inserted
return TRUE;
} else {
log_message('error', $this->campaign_id.' :: Could not insert sms into database');
log_message('error', $this->ci->db->_error_message());
return FALSE; // sms was not inserted correctly
}
}
如何在这样的场合调试insert_batch()
?
我已经对DB_active_rec.php
进行了一些更改,以便在insert_batch期间进行一些日志记录,到目前为止,我无法成功重现问题以查看出现了什么问题。但是,就问题在开始时出现2-3次并且我没有对我的逻辑进行重大修改来修复它时,我不能这样做,因为我不相信codeigniter的insert_batch()
函数用于生产
我还添加了codeigniter的insert_batch()函数:
public function insert_batch($table = '', $set = NULL)
{
$countz = 0;
if ( ! is_null($set))
{
$this->set_insert_batch($set);
}
if (count($this->ar_set) == 0)
{
if ($this->db_debug)
{
//No valid data array. Folds in cases where keys and values did not match up
return $this->display_error('db_must_use_set');
}
return FALSE;
}
if ($table == '')
{
if ( ! isset($this->ar_from[0]))
{
if ($this->db_debug)
{
return $this->display_error('db_must_set_table');
}
return FALSE;
}
$table = $this->ar_from[0];
}
// Batch this baby
for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
{
$sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
//echo $sql;
$this->query($sql);
$countz = $countz + $this->affected_rows();
}
$this->_reset_write();
log_message('info', "Total inserts from batch:".$countz);
return TRUE;
}
批处理中插入总数的最后一个log_message()
也显示了问题,因为当插入的插入量少于预期时,我也得到了非预期的插入数量。
我必须考虑使用其他方法将数千行插入到我的数据库中,或者不使用codeigniter。
任何人都有这种问题的线索吗?也许它与硬盘驱动器或系统内存有关,在性能不足的情况下?这是一台装有1GB内存的旧PC。
编辑:根据要求,我在这里放置一个示例INSERT语句,其中有9行由codeigniter的insert_batch()
函数生成
INSERT INTO `outgoingSMS` (`campaign_id`, `credit_cost`, `date_sent`, `deleted`, `destination`, `long`, `message`, `scheduled`, `sender_id`, `status`, `token`, `unicode`) VALUES ('279',1,'2013-08-02 02:08:34',0,'14141415151515',0,'fd',0,'sotos',1,'4d270f6cc2fb32fb47f81e8e15412a36',0), ('279',1,'2013-08-02 02:08:34',0,'30697000000140',0,'fd',0,'sotos',1,'9d5a0572f5bb2807e33571c3cbf8bd09',0), ('279',1,'2013-08-02 02:08:34',0,'30697000000142',0,'fd',0,'sotos',1,'ab99174d88f7d19850fde010a1518854',0), ('279',1,'2013-08-02 02:08:34',0,'30697000000147',0,'fd',0,'sotos',1,'95c48b96397b21ddbe17ad8ed026221e',0), ('279',1,'2013-08-02 02:08:34',0,'306972233469',0,'fd',0,'sotos',1,'6c55bc3181be50d8a99f0ddba1e783bf',0), ('279',1,'2013-08-02 02:08:34',0,'306972233470',0,'fd',0,'sotos',1,'d9cae1cbe7eaecb9c0726dce5f872e1c',0), ('279',1,'2013-08-02 02:08:34',0,'306972233474',0,'fd',0,'sotos',1,'579c34fa7778ac2e329afe894339a43d',0), ('279',1,'2013-08-02 02:08:34',0,'306972233475',0,'fd',0,'sotos',1,'77d68c23422bb11558cf6fa9718b73d2',0), ('279',1,'2013-08-02 02:08:34',0,'30697444333',0,'fd',0,'sotos',1,'a7fd63b8b053b04bc9f83dcd4cf1df55',0)
这是一个完整的插页。
答案 0 :(得分:3)
insert_batch()
试图避开你的问题 - 试图插入大于MySQL的数据被配置为一次处理。我不确定MySQL的选项是max_allowed_packet
还是别的,但问题是它设置了一个以字节为单位的限制而不是一些行。
如果您要编辑DB_active_rec.php,mysql_driver.php或任何适当的...尝试在for()
循环中更改该100个计数。 50应该是一个更安全的选择。
除此之外,如果您通过affected_rows()
插入超过100行,FYI - insert_batch()
将不会返回正确的值,因此将其用作成功/错误检查是不可靠的。这是因为insert_batch()
一次将数据插入100条记录,而affected_rows()
只返回上次查询的数据。
答案 1 :(得分:0)
此问题的解决方案是,您需要进入目录/system/database/
并打开文件DB_query_builder.php
并更新
public function insert_batch($table, $set = NULL, $escape = NULL, $batch_size = 1000)
您可以设置要求的大小。