1

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?)

Community
  • 1
  • 1
Andrew Willis
  • 2,289
  • 3
  • 26
  • 53
  • If you read the question, you'd know that the queries are normally written via the CodeIgniter database class, not 'mysql_', I just typed this as an example. Secondly, using PDO is not the only option, you can also, for instance, use 'mysqli_' so do not impose your preferences upon others, especially when you can't be bothered to read a question before passing judgement on it. – Andrew Willis May 20 '12 at 18:10
  • I didn't vote to close it... i didn't put an effort on reading it because i found 5 other questions with the same title and thought to myself : "What could be different?" – eric.itzhak May 20 '12 at 18:12
  • 1
    so why bother even commenting on the question? I have read the other questions with similar titles and I felt they didn't answer my question sufficiently. – Andrew Willis May 20 '12 at 18:16
  • It was my way to notify you it has been asked before, in case you were too lazy to google. – eric.itzhak May 20 '12 at 18:17
  • Ok, I guess that some people wouldn't bother Googling so it would be a fair assumption. I had actually just spent 2 hours in the MySQL documentation and looking for an answer on here so I apologise for being a bit snappy. – Andrew Willis May 20 '12 at 18:19
  • It's ok :) Sorry i can't help and good luck! – eric.itzhak May 20 '12 at 18:22

0 Answers0