I'm sure this has been discussed many times before, but for whatever reason I didn't find anything (could be no coffee).
As a design question here's the idea.
One (remote) database looks something like this
id|timestamp|log_entry|task_id
These are fetched to a PHP/Codeigniter portal and mapped to a local database
task_id|name|...
Now, parsing through the remote data I need to (among other things) get the name associated with each row. The simple way would be to loop through the result, and in each iteration do a lookup (id -> name). But this will generate a lot of DB calls. The other way I see it is to pre-fetch all id-name pairs into an array, and then use that for lookup.
For this specific project I'm not expecting performance to be an issue either way. But which would be the most efficient way to do this?
EDIT: Pseudo code
<?php
// ---------- Multiple queries example ------
$log_result = mysql_query('SELECT id, task_id, log_entry, timestamp FROM remote_db WHERE date=X');
foreach ($log_result as $log_row)
{
// Get task name for row
$task_name = mysql_query("SELECT name FROM local_db WHERE id={$log_row['task_id']}");
// Process this row ...
echo "Proccesed {$task_name} which occured at {$log_row['timestamp']}";
}
// ---------- Array example -----------------
$task_lookup = mysql_query('SELECT id, name FROM local_db');
$log_result = mysql_query('SELECT id, task_id, log_entry, timestamp FROM remote_db WHERE date=X');
foreach ($log_result as $log_row)
{
// Get task name for row
// assume task_lookup[] = array(id => name)
$task_name = $task_lookup[$log_row['task_id']];
// Process this row ...
echo "Proccesed {$task_name} which occured at {$log_row['timestamp']}";
}
?>