So I have a “notes” field that is getting one row (assigned_user_id) updated to new id’s. Notes is approx. 160k in length. The users table is around 300.
For each notes row I compare the assigned_user_id to the users table, if a match is found, get the new user ID and update the notes table. Rinse repeat:
//dis-allow timeout. Was timing out consistently under 100 rows changes.
set_time_limit(0);
ignore_user_abort(1);
//foreach note
$counter = 0;
foreach ( $msCRMAnnotations as $key=>$value)
{
//foreach user, check for match between assigned_user_id and id_mscrm
foreach( $sugarCRMUsers as $key2=>$value2 )
{
if($value['assigned_user_id'] == $value2['id_mscrm'] )
{
//give some output as to what is changing...
echo("Note #" . $counter++ . " - Note id " . $value['id'] . " is assigned to user " . $value2['user_name'] . ".<br />" );
$query = "
UPDATE `notes`
SET `assigned_user_id` = '" . $value2['id'] . "'
WHERE `id` = '" . $value['id'] . "' AND
`assigned_user_id` = '" . $value['assigned_user_id'] . "'
";
$DB->query($query);
//unset($query);
}
}
}
Specifically we are migrating select data from a MsCRM to SugarCRM. This being the accounts/notes/users phase of the project.
So now the question….how can I best re-factor the above code to be more performant? Any and all help is greatly appreciated.
Assuming from comments:
Try to set indexes for these columns, for faster search.