Mysql php script to replace refactor needs

Hi, I have two mysql tables, and they both need updating,

companies
unique_code

Companies take 1 million records, and I need a unique code for each of them, the problem is that my PHP script captures all the companies, and in the foreach loop it captures the unique code from the unique_code table and updates, and then the unique_code table is updated to indicate the code.

PHP code just hangs and reaches its maximum execution limit. I am really stuck and need these companies to have a unique code, can anyone think of a different approach?

Example with delayed code.

 foreach ($aCompanies as $companies){

   $query="SELECT * FROM unique_code WHERE used = 0"

   foreach(unique_code as code){
       //  UPdate companies table
       $query = "UPDATE companies SET id = $code";
       // Flag code used
       $query = "UPDATE unique_codes WHERE code = $code";
   }
}

Greetings for your time.

The code:

$query1 = "SELECT code FROM unique_codes WHERE used = 0\n";
$aUniqueCode = $oDbh->getAll($query1);

$query2 = "SELECT id FROM companies";
$aCompanies = $oDbh->getAll($query2);

foreach ($aCompanies as $companies){

    $query = "SELECT code FROM unique_codes WHERE used = '0' LIMIT 1";
    $oCode = $oDbh->getRow($query);

    $query3.= "UPDATE companies SET code = $oCode->code WHERE id = $companies->id\n";   

    $query4 = "UPDATE unique_codes SET used = '1' WHERE code = $oCode->code\n";
    $oDbh->query($query4);      
}
print print_r($query3).';';
exit;

What I do does not update the company, I export all the SQL to a file, so I can import later.

+3
3

(, , ), PHP. MySQL alter, . MySQL , PHP.

- , , , .

+3

? , , (*):

$query="SELECT * FROM unique_code WHERE used = 0"

$query="SELECT field1, field2, field3, etc FROM unique_code WHERE used = 0"
0

, uniuque- , , , WHERE, , .

, ? Xdebug , , .

You can use Memchace so that you can store the most used objects in memory and reduce database hits while reading (SELECT).

0
source

Source: https://habr.com/ru/post/1730862/


All Articles