Possible Duplicate:
Is it better to return one big query or a few smaller ones?
One big query vs. many small ones?
I have a database with a lot of linked tables.
Something like this (simplified for ease):
USERS
|user_id|username|location_id|
EVENTS
|event_id|location_id|description|
BANDS
|band_id|band_name|description|
LOCATIONS
|location_id|location_name|
USERS_EVENTS
|user_id|event_id|
BANDS_EVENTS
|user_id|event_id|
USERS_BANDS
|user_id|band_id|
USERS_LOCATIONS
|user_id|location_id|
EVENTS_LOCATIONS
|event_id|location_id|
When a user logs in, they are brought to their dashboard which shows events they're attending (from the users_events
table) and events in their location (using the events_locations table) and events by bands they like.
Considering that users may not be attending any events and that events might not appear in their location from time to time, what would be the best (read: most efficient) way for me to retrieve this data?
(once the query has been executed it is cached for 15 minutes as the user might return to their dashboard any number of times, the cache file gets updated if they "attend" an event but the events in their location will update every 15 minutes.)
I am currently doing something like this (although I use CodeIgniter's database class so the queries aren't as unsecure as written here):
$query = mysql_query("
SELECT *
FROM users
WHERE username = {$username}
LIMIT 1");
$result = array('events_attending' => array(), 'events_in_area' => array(), 'events_by_bands' => array());
if (mysql_num_rows($query) > 0) {
while ($row = mysql_fetch_assoc($query)) {
$query_2 = mysql_query("
SELECT events.event_id, locations.location_name, events.description
FROM users_events, events, locations
WHERE users_events.user_id = {$row['user_id']}
AND events.event_id = users_events.event_id
AND locations.location_id = events.location_id
LIMIT 5");
$i = 0;
while ($row_2 = mysql_fetch_assoc($query_2)){
$query_3 = mysql_query("
SELECT bands.band_name
FROM bands_events, bands
WHERE bands_events.event_id = {$row_2['event_id']}
AND bands.band_id = bands_events.event_id
");
$result['events_attending'][$i] = $row_2;
while ($row_3 = mysql_fetch_assoc($query_3)){
$result['events_attending'][$i]['bands'][] = $row_3['band_name'];
}
$i++;
}
$query_4 = mysql_query("
SELECT events.description, locations.location_name
FROM events, locations
WHERE events.location_id = {$row['location_id']}
AND locations.location_id = {$row['location_id']}
LIMIT 5");
$i = 0;
while ($row_4 = mysql_fetch_assoc($query_4)){
$query_5 = mysql_query("
SELECT bands.band_name
FROM bands_events, bands
WHERE bands_events.event_id = {$row_4['event_id']}
AND bands.band_id = bands_events.event_id");
$result['events_in_area'][$i] = $row_4;
while ($row_5 = mysql_fetch_assoc($query_5)){
$result['events_in_area'][$i]['bands'][] = $row_2['band_name'];
}
$i++;
}
$query_6 = mysql_query("
SELECT bands.band_id, bands.band_name
FROM users_bands, bands
WHERE users_bands.user_id = {$row['user_id']}
AND bands.band_id = users_bands.band_id");
while ($row_6 = mysql_fetch_assoc($query_6)) {
$query_7 = mysql_query("
SELECT events.event_id, locations.location_name, events.description
FROM bands_events, events, locations
WHERE bands_events.band_id = {$row_6['band_id']}
AND events.event_id = bands_events.event_id
AND locations.location_id = events.location_id
LIMIT 5");
$i = 0;
while ($row_7 = mysql_fetch_assoc($query_7)){
$result['events_by_bands'][$row_6['band_name']][$i] = $row_7;
$i++;
}
}
}
// save return data as json object in cache file,
// return json object
} else {
// error handling - user not in database
}
This is a small example, there are other tables with a similar set-up for different things, all together there are 24 tables used to generate the user dashboard.
Would it be better to do this with a massive JOIN query? also if I was to do that, what would the correct syntax for the query be?)