我正在使用 PHP 比较一些数据来访问 MySQL 表。该脚本需要一个多小时才能运行。我们正在处理源数据中的大约 100k 行和每个目标表(即两个表)中的 300k。
每个表总共不超过 10 列。
这是脚本:
$query = "SELECT bbva_check_deductions.empno, bbva_check_deductions.idr, bbva_check_deductions.current1, bbva_job.paygroup
FROM bbva_check_deductions
LEFT JOIN bbva_job
ON bbva_check_deductions.empno = bbva_job.file_nbr
ORDER BY current1 DESC, EmpNo DESC, IDR DESC";
$result_set = mysql_query($query,$connection);
while ($result = mysql_fetch_assoc($result_set)) {
$emplid = compare_filter($result['empno']);
$oldCode = compare_filter($result['idr']);
$origCode = $oldCode;
$payGroup = compare_filter($result['paygroup']);
$codes = array('DC6', 'HTH', 'DM4', '4UP', 'DC1', 'DC3', 'DC4', 'DEN', 'DCR', 'MDR', 'PRK', 'VIS', '41K');
//CALCULATE WEEK NO
if ($payGroup == "J2C") {$week = 24;}
elseif ($payGroup == "J2E") {$week = 26;}
else {$week = 22;}
//QUERY TRANSLATION TABLE FOR TRANSLATED VALUE
$translate_query = "SELECT * FROM translation WHERE sourceValue = '{$oldCode}'
AND sourceField LIKE '%ded_code%' LIMIT 1";
$translate_result_set = mysql_query($translate_query, $connection);
if (mysql_num_rows($translate_result_set) > 0) {
$translate_result = mysql_fetch_assoc($translate_result_set);
$oldCode = compare_filter($translate_result['translatedValue']);
}
//DETERMINE IF MEMO
if ($oldCode == "GTL" || $oldCode == "BLI" || $oldCode == "BLT" || $oldCode == "HIC" || $oldCode == "DM4") {
$queryField = "memo_code"; $queryTable = "pf_check_memo";
} else {
$queryField = "deduction_code"; $queryTable = "pf_check_deductions";
}
$compare_query = "SELECT {$queryField}, amount_field_for_payroll
FROM {$queryTable}
WHERE empl_id = {$emplid}
AND {$queryField} LIKE '%{$oldCode}%'
AND week_number = {$week}
LIMIT 1";
$compare_result_set = mysql_query($compare_query, $connection);
if (mysql_num_rows($compare_result_set) != 0) {
$compare_result = mysql_fetch_assoc($compare_result_set);
//PREP COMPARE FIELDS
$amount = compare_filter($result['current1']);
$compare_amount = compare_filter($compare_result['amount_field_for_payroll']);
$negCodes = array('DC6', 'HTH', 'DM4', '4UP', 'DC1', 'DC3', 'DC4', 'DEN', 'DCR', 'MDR', 'PRK', 'VIS', '41K');
if (in_array($oldCode,$negCodes)) {$compare_amount = $compare_amount * -1;}
//AMOUNT COMPARE
direct_compare_basic($amount,$compare_amount,"amount","bbva_check_deductions","Y",$origCode,$oldCode);
} else {$content .= $emplid . $separator . "" . $separator . "Emplid + Deduction Code + Pay Group" . $separator . 'bbva_check_deductions' .
$separator . field_prep($emplid . " - " . $oldCode . " - " . $payGroup) . $separator . "No Record Found in PF for week {$week}" . $separator . "Y" . $separator . "Y" .
$separator . $origCode . $separator . $oldCode . $break;}
然后它只是写入一个 .CSV 文件。
我的函数不调用任何额外的查询,只是格式化东西。我正在运行 InnoDB 表并为所有正在使用的列提供索引。我的电脑正在运行 WAMP,它有 16G 的 RAM 和一个四核处理器。硬盘是 7200 转。
对我来说似乎花费的时间太长了。有任何想法吗?