我有两个表,每个表都有 160k+ 行。两者之间共享一些 UUID。我在"new"表上使用 foreach 循环,并使用嵌入式 foreach 搜索“旧”表。当 UUID 匹配失败时,“旧”表将使用"new"表中的数据进行更新。
两个表都有一个ID的索引。
我的问题是这个操作非常耗时;有谁知道更有效的方法来搜索匹配的 UUID?旁注:我们正在使用 PHP 5.3 的 MySQLi 扩展
实验代码:
$oldCounter = 0;
$newCounter = 1;
//loop
foreach( $accounts as $accKey=>$accValue )
{
echo( "New ID - " . $oldCounter++ . ": " . $accValue['id'] . "\n" );
foreach( $accountContactData as $acdKey=>$acdValue )
{
echo( "Old ID - " $newCounter++ . ": " . $acdValue['id'] . " \n" );
if( $accValue['id'] == $acdValue['id'] && (
$accValue['phone_office'] == "" || $accValue['phone_office'] == NULL || $accValue['phone_office'] == 0 )
){
echo("ID match found\n");
//when match found update accounts with accountsContact info
$query = '
UPDATE `accounts`
SET
`phone_fax` = "' . $acdValue['fax'] . '",
`phone_office` = "' . $acdValue['telephone1'] . '",
`phone_alternate` = "' . $acdValue['telephone2'] . '"
WHERE
`id` = "' . $acdValue['id'] . '"
';
echo "" . $query . "\n\n";
$DB->query($query);
break 1;
}
}
}
unset($oldCounter);
unset($newCounter);
提前谢谢你。
最佳答案
这一切都在 SQL 中完成。
我在您的代码中没有看到任何需要 PHP 的内容。
UPDATE
允许JOIN
。 JOIN
新 和旧 表并使您的WHERE
条件与您的描述相匹配。应该非常简单并且速度明显更快。
关于php - 查找两个表之间的匹配项,每个表有 160k+ 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12270636/