我有 2 张桌子。表格具有相同的列,我需要比较它们。简单的联接查询无法解决我的问题,因为 table2 可以包含许多适合表 1 的行,但我需要选择最合适的行。例如 :
表格1
期间; 价格; 数字;
1个;3;5个;
表 2
期间; 价格; 数字;
1个;3.1;5个;
1个;3.01; 5个;
我需要将 table1 中的 row1 与表 2 中的 row1 和 row2 进行比较,然后选择最合适的(例如 row2 是最合适的)并将 row2 标记为已比较,下次不要进行比较。我正在使用 FIREBIRD 数据库和 ADODB php 库。我写了一些代码,但是当我在表中有很多记录时,它可以工作很长时间。如何优化我的代码以更快地完成这项任务?
代码:
$this->connect->BeginTrans();
$sourceResult = $this->connect->Execute( "SELECT SC_PHONE_NUMBER, SC_CALL_START, SC_DURATION, SC_RATE, SC_ID FROM ". $this->sourceTableName . " WHERE sc_comparing_id = " . $this->insertedId );
if ( $sourceResult ) {
while ( !$sourceResult->EOF ) {
$result = array();
$comparationResult = $this->connect->Execute(
"SELECT CC_PHONE_NUMBER, CC_CALL_START, CC_DURATION, CC_RATE, CC_ID FROM " . $this->comparableTableName . " WHERE cc_comparing_id = " . $this->insertedId
. " AND cc_is_compared = 0"
. " AND cc_phone_number = " . $sourceResult->fields['SC_PHONE_NUMBER']
. " AND " . $sourceResult->fields['SC_CALL_START'] . " BETWEEN cc_call_start - " . TIME_RANGE . " AND " . " cc_call_start + " . TIME_RANGE
);
if ( $comparationResult ) {
while ( !$comparationResult->EOF ) {
$callStartRating = TIME_RANGE / ( TIME_RANGE + abs( $sourceResult->fields['SC_CALL_START'] - $comparationResult->fields['CC_CALL_START'] ) );
$durationRating = 0;
$rateRating = 0;
if ( $sourceResult->fields['SC_DURATION'] > $comparationResult->fields['CC_DURATION'] ) {
$durationRating = $comparationResult->fields['CC_DURATION'] / $sourceResult->fields['SC_DURATION'];
} else {
$durationRating = $sourceResult->fields['SC_DURATION'] / $comparationResult->fields['CC_DURATION'];
}
if ( $sourceResult->fields['SC_RATE'] > $comparationResult->fields['CC_RATE'] ) {
$rateRating = $comparationResult->fields['CC_RATE'] / $sourceResult->fields['SC_RATE'];
} else {
$rateRating = $sourceResult->fields['SC_RATE'] / $comparationResult->fields['CC_RATE'];
}
$totalRating = $rateRating + $durationRating + $callStartRating;
$result[] = array(
'sc_id' => $sourceResult->fields['SC_ID'],
'cc_id' => $comparationResult->fields['CC_ID'],
'rating' => $totalRating
);
$comparationResult->MoveNext();
}
$resArray = null;
if ( count( $result ) >= 1 ) {
$resArray = $result[0];
foreach ( $result as $row ) {
if ( $resArray['rating'] < $row['rating'] ) {
$resArray = $row;
}
}
$query = "UPDATE source_cdr SET sc_cc_key = " . $row['cc_id'] . " WHERE sc_id = " . $row['sc_id'];
$this->connect->_Execute( $query );
$this->connect->_Execute( "UPDATE comparable_cdr SET cc_is_compared = 1 WHERE cc_id = " . $resArray['cc_id'] );
}
}
$this->connect->CommitTrans();
$sourceResult->MoveNext();
}