加速 SQL 更新循环通过 Select 语句的语句
Speed Up SQL Update Statement That Is Looped Through A Select Statement
发现代码中的 SQL 查询需要 31 分钟(1900 秒)才能完成。首先 select 语句获取 1955 行,然后代码循环遍历这些行以 运行 根据该记录集中的数字进行更新。它 运行 通过的 table 有 14,000 行。我怎样才能加快速度?
$sql = "select id, did, customer_id from dids";
$rs = $db->PDOquery($sql, $qry_arr);
//Loop through all DIDs and attach to cdrs select id, did, customer_id from dids
while($row=$rs->fetch()){
$qry_arr = array(':did_id' => $row['id'],
':customer_id' => $row['customer_id'],
':did' => $row['did']);
$sql = "update ".$billing_table." c ";
$sql .= "set c.did_id = :did_id, c.customer_id = :customer_id ";
$sql .= "where c.customer_id = 0 and c.telcom_num = :did ";
$result=$db->PDOquery($sql, $qry_arr);
set_time_limit(30); //Reset time limit after each query
if (!$result) {
error_log(date("Y/m/d h:i:sa").": "."\nError In Sql.\n".$sql, 3, $cron_log);
}
}
尝试使用以下方法,但收到错误消息:错误代码:1054。'where clause'
中的未知列 'dids.did'
UPDATE ".billing_table." SET ".billing_table.".did_id = dids.id, ".billing_table.".customer_id = dids.customer_id WHERE dids.did = ".billing_table.".telcom_num
序列化 SQL 查询通常会导致性能不佳。您可以在一条语句中完成所有操作:
$sql = "update ".$billing_table." c ".
"inner join dids d on d.did=c.telcom_num ".
"set c.did_id = d.id, c.customer_id = d.customer_id ".
"where c.customer_id = 0;";
发现代码中的 SQL 查询需要 31 分钟(1900 秒)才能完成。首先 select 语句获取 1955 行,然后代码循环遍历这些行以 运行 根据该记录集中的数字进行更新。它 运行 通过的 table 有 14,000 行。我怎样才能加快速度?
$sql = "select id, did, customer_id from dids";
$rs = $db->PDOquery($sql, $qry_arr);
//Loop through all DIDs and attach to cdrs select id, did, customer_id from dids
while($row=$rs->fetch()){
$qry_arr = array(':did_id' => $row['id'],
':customer_id' => $row['customer_id'],
':did' => $row['did']);
$sql = "update ".$billing_table." c ";
$sql .= "set c.did_id = :did_id, c.customer_id = :customer_id ";
$sql .= "where c.customer_id = 0 and c.telcom_num = :did ";
$result=$db->PDOquery($sql, $qry_arr);
set_time_limit(30); //Reset time limit after each query
if (!$result) {
error_log(date("Y/m/d h:i:sa").": "."\nError In Sql.\n".$sql, 3, $cron_log);
}
}
尝试使用以下方法,但收到错误消息:错误代码:1054。'where clause'
中的未知列 'dids.did' UPDATE ".billing_table." SET ".billing_table.".did_id = dids.id, ".billing_table.".customer_id = dids.customer_id WHERE dids.did = ".billing_table.".telcom_num
序列化 SQL 查询通常会导致性能不佳。您可以在一条语句中完成所有操作:
$sql = "update ".$billing_table." c ".
"inner join dids d on d.did=c.telcom_num ".
"set c.did_id = d.id, c.customer_id = d.customer_id ".
"where c.customer_id = 0;";