我有一个脚本,它应该通过 mysql 数据库运行并在案例上执行某个“测试”。简化后的数据库包含代表人们旅行的记录。每条记录都是一次旅行。但我只想使用往返旅行。所以我需要搜索数据库并将两次旅行相互匹配;到某个地点的旅行和从某个地点出发的旅行。
该脚本工作正常。问题是数据库包含超过 600.000 个案例。我知道如果可能的话应该避免这种情况。但是为了这个脚本的目的和以后使用数据库记录,一切都必须结合在一起。
在我的 iMac 上使用 MAMP 执行脚本时,现在执行该脚本需要几个小时。当然,我确保它可以使用大量内存等。
我的问题是如何加快速度,最好的方法是什么?
这是我现在拥有的脚本:
$table = $_GET['table'];
$output = '';
//Select all cases that has not been marked as invalid in previous test
$query = "SELECT persid, ritid, vertpc, aankpc, jaar, maand, dag FROM MON.$table WHERE reasonInvalid != '1' OR reasonInvalid IS NULL";
$result = mysql_query($query)or die($output .= mysql_error());
$totalCountValid = '';
$totalCountInvalid = '';
$totalCount = '';
//For each record:
while($row = mysql_fetch_array($result)){
$totalCount += 1;
//Do another query, get all the rows for this persons ID and that share postal codes. Postal codes revert between the two trips
$persid = $row['persid'];
$ritid = $row['ritid'];
$pcD = $row['vertpc'];
$pcA = $row['aankpc'];
$jaar = $row['jaar'];
$maand = $row['maand'];
$dag = $row['dag'];
$thecountquery = "SELECT * FROM MON.$table WHERE persid=$persid AND vertpc=$pcA AND aankpc=$pcD AND jaar = $jaar AND maand = $maand AND dag = $dag";
$thecount = mysql_num_rows(mysql_query($thecountquery));
if($thecount >= 1){
//No worries, this person ID has multiple trips attached
$totalCountValid += 1;
}else{
//Ow my, the case is invalid!
$totalCountInvalid += 1;
//Call the markInvalid from functions.php
$totalCountValid += 1;
markInvalid($table, '2', 'ritid', $ritid);
}
}
//Echo the result
$output .= 'Total cases: '.$totalCount.'<br>Valid: '.$totalCountValid.'<br>Invalid: '.$totalCountInvalid; echo $output;