批量插入原理以及案例
public function insertALl() { $length = 100; $sql = "select count(*) SALE,PRODUCT_ID from order_product group by PRODUCT_ID"; $data = Db::query($sql); $data_length = count($data); $step_length = ceil($data_length / $length); for ($i=0; $i < $step_length ; $i++) { $sql_insert = "insert into order_product_sales(SALES, PRODUCT_ID) values"; $start_index = $i * $length; $end_index = ($i+1) * $length; if($end_index > $data_length) $end_index = $data_length; for ($j = $start_index; $j < $end_index ; $j++) { $sql_insert = $sql_insert. "(".$data[$j]['SALE'] . ',' . $data[$j]['PRODUCT_ID'] . '),'; } $sql_insert = substr($sql_insert, 0, -1); Db::execute($sql_insert); } }
先设置步长

浙公网安备 33010602011771号