Php 在两个表之间查找匹配项,每个表都有160k+;排

Php 在两个表之间查找匹配项,每个表都有160k+;排,php,mysql,performance,refactoring,Php,Mysql,Performance,Refactoring,我有两个表,每个表有160k+行。在这两者之间,一些UUID是共享的。我正在对“新”表使用foreach循环,并使用嵌入式foreach搜索“旧”表。当UUID匹配失效时,“旧”表将用“新”表中的数据更新 两个表的ID上都有一个索引 我的问题是这个操作非常耗时;有人知道一种更有效的方法来搜索匹配的UUID吗?旁注:我们正在为PHP5.3使用MySQLi扩展 Exp代码: $oldCounter = 0; $newCounter = 1; //loop foreach( $accounts as

我有两个表,每个表有160k+行。在这两者之间,一些UUID是共享的。我正在对“新”表使用foreach循环,并使用嵌入式foreach搜索“旧”表。当UUID匹配失效时,“旧”表将用“新”表中的数据更新

两个表的ID上都有一个索引

我的问题是这个操作非常耗时;有人知道一种更有效的方法来搜索匹配的UUID吗?旁注:我们正在为PHP5.3使用MySQLi扩展

Exp代码:

$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的东西

更新
允许
加入
<代码>加入新表和旧表,并使您的
WHERE
条件与您的描述相匹配。应该非常简单,而且速度要快得多。

在SQL中完成这一切

我在你的代码中没有看到需要PHP的东西


更新
允许
加入
<代码>加入新表和旧表,并使您的
WHERE
条件与您的描述相匹配。应该非常简单而且速度更快。

几个月前我写了一个函数
您可以根据需要修改它

这可以提高搜索速度

public static function search($query)
{
    $result = array();
    $all = custom_query::getNumRows("bar");
    $quarter = floor(0.25 * $all) + 1;
    $all = 0;
    for($i = 0;$i<4;$i++)
    {
        custom_query::condition("", "limit $all, $quarter");
        $data = custom_query::FetchAll("bar");
        foreach ($data as $v)
            foreach($v as $_v)
                if (count(explode($query, $_v)) > 1)
                    $result[] = $v["bar_id"];
        $all += $quarter;
    }
    return $result;
}
公共静态函数搜索($query)
{
$result=array();
$all=custom_查询::getNumRows(“bar”);
$quarter=楼层(0.25*$all)+1;
$all=0;
对于($i=0;$i 1)
$result[]=$v[“bar_id”];
$all+=$quarter;
}
返回$result;
}
它返回与搜索匹配的记录的ID

此方法将表分为4部分,每次迭代只得到表的四分之一…
您可以将此数字更改为例如10或20,速度为…

有些方法在类中,您可以轻松编写主题

几个月前我写了一个函数
您可以根据需要修改它

这可以提高搜索速度

public static function search($query)
{
    $result = array();
    $all = custom_query::getNumRows("bar");
    $quarter = floor(0.25 * $all) + 1;
    $all = 0;
    for($i = 0;$i<4;$i++)
    {
        custom_query::condition("", "limit $all, $quarter");
        $data = custom_query::FetchAll("bar");
        foreach ($data as $v)
            foreach($v as $_v)
                if (count(explode($query, $_v)) > 1)
                    $result[] = $v["bar_id"];
        $all += $quarter;
    }
    return $result;
}
公共静态函数搜索($query)
{
$result=array();
$all=custom_查询::getNumRows(“bar”);
$quarter=楼层(0.25*$all)+1;
$all=0;
对于($i=0;$i 1)
$result[]=$v[“bar_id”];
$all+=$quarter;
}
返回$result;
}
它返回与搜索匹配的记录的ID

此方法将表分为4部分,每次迭代只得到表的四分之一…
您可以将此数字更改为例如10或20,速度为…

有些方法在类中,您可以轻松编写主题

更新
账户
设置
账户
电话传真=
账户旧版
传真
账户
电话办公室=
账户旧版
电话1
accounts
phone\u alternate
=
accounts\u old
telephone2
LEFT JOIN
accounts\u old
id
id
其中
accounts
id
id
是这样吗你指的是什么想法*阿吉耶!!!!!stackoverflow.com,请允许您的评论有更好的格式…或多或少。我不知道您的表结构,但我保证您可以单独使用SQL来实现这一点。试着重写它,做一些测试,然后发回。是的,基本上做到了。而且比循环脚本快得多。更新
账户
设置
账户
电话传真
=
账户
传真,
账户
电话办公室
=
账户
电话1,
accounts
phone\u alternate
=
accounts\u old
telephone2
LEFT JOIN
accounts\u old
id
id
其中
accounts
id
id
是这样吗你指的是什么想法*阿吉耶!!!!!stackoverflow.com,请允许您的评论有更好的格式…或多或少。我不知道您的表结构,但我保证您可以单独使用SQL来实现这一点。试着重写它,做一些测试,然后发回。是的,基本上做到了。而且比循环脚本快得多。快了8倍。