Multiple DB lookups VS fetch all + array search, what's more efficient
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_na开发者_如何转开发me} which occured at {$log_row['timestamp']}";
}
?>
If you need all the information anyway then certainly selecting it once and looping over what you need (particularly since the database is remote and the latency of many calls will add up).
Edit: Looking at the pseudo code: You could use the array of ids from the remote DB to narrow the results grabbed from the local db. Something like:
$log_result = mysql_query('SELECT id, task_id, log_entry, timestamp FROM remote_db WHERE date=X');
$task_lookup = mysql_query('SELECT id, name FROM local_db WHERE id IN taskIdsFromLogQuery');
精彩评论