在Mongodb中插入几个数据后插入速度变慢,我使用batch_insert进行插入。插入时我还需要检查一些条件。
要插入20k数据本身需要1个多小时。
在temporary_table中我有1L数据
$batchSize = 20;
$documents = array();
$count = count($pending_contacts_data);
$count =1;
$temporary_data = array();
for($i=0;$i<$count;$i++){
$multiple_temporary_data = $this->mongo_db->select('*')->where(array('contact_id'=>(int)4,'status'=>1))->limit(10000)->get('temporary_table');
$temporary_data = array_merge($temporary_data,$multiple_temporary_data);
}
$count1 = count($temporary_data);
$documents = array();
for ($i=0; $i<$count1; $i++)
{
$obj_id[] = $temporary_data[$i]->_id;
$test_email = $this->mongo_db->select('*')->where(array('encrypted_email'=>$temporary_data[$i]->encrypted_email))->get('email_table');
if(empty($test_email)){
$document = array('email_id'=>$temporary_data[$i]->email,
'encrypted_email'=>$temporary_data[$i]->encrypted_email,
'encrypted_key'=>$temporary_data[$i]->encrypted_key,
'encrypted_iv'=>$temporary_data[$i]->encrypted_iv,
'status'=>(int)1,
'opend_supression_status'=>''
);
array_push($documents, $document);
if ((($i % $batchSize) === 0)) {
$insert = $this->mongo_db->batch_insert('opend_contacts_email_new1',$documents);
$update_temporary =$this->mongo_db->where_in('_id',$obj_id)->set(array('status'=>13))->update_all('temporary_data');
$documents = array();
}
}
}
答案 0 :(得分:0)
您的代码出了什么问题,在第500个元素之后,每个新项目都作为单个批处理插入。 $ documents只是循环其余部分的一个元素的数组。你应该使用模数运算符%
$count = 20000;
$batchSize = 50;
for ($i=1; $i<=$count; ++$i){
$test_email = $this->mongo_db->select('*')->where(array('email_id'=>$temporary_data[$i]->email_id))->get('email_table');
if(empty($test_email)){
$document = array('email_id'=>$temporary_data[$i]->email,
'encrypted_email'=>$temporary_data[$i]->encrypted_email,
'encrypted_key'=>$temporary_data[$i]->encrypted_key,
'encrypted_iv'=>$temporary_data[$i]->encrypted_iv,
'status'=>(int)1,
'suppression_status'=>''
);
array_push($documents, $document);
if (($i % $batchSize) === 0) {
$insert = $this->mongo_db->batch_insert('email_table',$documents);
$documents = array();
}
}
}