在一些场景,例如当查询出的一个数组列表元素过大时,再次进行查询效率会变慢
这时候需要进行分块然后分批查询
可以使用array_chunk分块
array_slice() 函数在数组中根据条件取出一段值,并返回
看一个使用案例
public function insuranceRecordOp($date){if (!empty($date)) {$start_time &#61; $date . &#39; 00:00:00&#39;;$end_time &#61; $date . &#39; 23:59:59&#39;;} else {$start_time &#61; date(&#39;Y-m-d&#39;, (time() - 24 * 60 * 60)) . &#39; 00:00:00&#39;;$end_time &#61; date(&#39;Y-m-d&#39;, (time() - 24 * 60 * 60)) . &#39; 23:59:59&#39;;}$where &#61; array(&#39;create_time&#39; &#61;> [[&#39;egt&#39;, $start_time], [&#39;elt&#39;, $end_time], &#39;and&#39;],);$od_list &#61; \Order\Model\OrderModel::odSelect($where, &#39;create_time,id&#39;, null);$od_ids &#61; array_column($od_list, &#39;id&#39;);$done_od_ids &#61; [];//$ids_chunk &#61; array_chunk($od_ids, 200);foreach (array_chunk($od_ids, 200) as $ids) {$insurance_list &#61; $this->__insurance_record_model->getInsuranceList([&#39;od_id&#39; &#61;> [&#39;in&#39;, $ids]], [&#39;od_id&#39;]);$done_od_ids &#61; array_merge($done_od_ids, array_column($insurance_list, &#39;od_id&#39;));}$od_id_list &#61; array_diff($od_ids, $done_od_ids);if (empty($od_id_list)) echo ERRNO::SUCCESS;$offset &#61; 0;$length &#61; 50;$data_count &#61; count($od_id_list);$data_list &#61; [];//分批获取数据while ($offset <$data_count) {$tmp_data &#61; array_slice($od_id_list, $offset, $length);$filters &#61; [&#39;od_id&#39; &#61;> [&#39;predicate&#39; &#61;> FILTER_PREDICATE::IN,&#39;value&#39; &#61;> array_values($tmp_data),],];$dbg &#61; [];$limit &#61; array(&#39;page_num&#39; &#61;> 0, &#39;page_size&#39; &#61;> $length);$fields &#61; ["od_id", "od_link_id", "od_basic_id", "group_id", "com_id", "order_dn", "b_tr_tr_num", "tr_num","num", "billing_date", "weight", "declared_value", "b_tr_basic_id", "order_num", "g_name","co_point", "cor_addr_info_str", "cee_addr_info_str", "od_ext_info", "entrust_num", "goods_num","b_tr_tr_num_ext", "pickup_tr_num", "od_create_time", "od_ext_info", "cor_addr_info", "cee_addr_info"];list($data_list_tmp) &#61; OrderModel::fetchOrders($fields, $filters, &#39;&#39;, $limit, null, $dbg, [], []);if (empty($data_list_tmp)) {c_log(&#39;运单od_id为:&#39; . implode(&#39;,&#39;, $data_list_tmp) . &#39;生成保单时获取昨日运单信息错误&#39;, &#39;ERROR_TRACE&#39;);//return $this->doResponse(\Basic\Cnsts\ERRNO::INVALID_ERRNO, &#39;生成保单时无法获取运单信息&#39;, $data_list_tmp);}$data_list &#61; array_merge($data_list, $data_list_tmp);$offset &#43;&#61; $length;usleep(200);}foreach ($data_list as $key &#61;> $value) {$data &#61; [&#39;info&#39; &#61;> [&#39;group_id&#39; &#61;> $value[&#39;group_id&#39;],&#39;company_id&#39; &#61;> $value[&#39;com_id&#39;]],&#39;b_info&#39; &#61;> [&#39;b_basic_id&#39; &#61;> $value[&#39;b_tr_basic_id&#39;]],&#39;tr_num&#39; &#61;> $value[&#39;b_tr_tr_num&#39;]];//$value[&#39;cor_addr_info&#39;] &#61; json_decode($value[&#39;cor_addr_info_str&#39;], true);//$value[&#39;cee_addr_info&#39;] &#61; json_decode($value[&#39;cee_addr_info_str&#39;], true);$ret_insurance &#61; $this->insurance_srv->insuranceRecordManageData($value, $data, &#39;add&#39;);if ($ret_insurance[&#39;errno&#39;] !&#61; ERRNO::SUCCESS) {c_log(&#39;运单order_num为:&#39; . $value[&#39;order_num&#39;] . &#39;生成保单错误&#39;, &#39;ERROR_TRACE&#39;);//return $this->doResponse($ret_insurance[&#39;errno&#39;], $ret_insurance[&#39;errmsg&#39;], $data_list);}}echo ERRNO::SUCCESS;}
在上诉场景中&#xff0c;都使用array_chunk分块然后循环更好