1

重複の可能性:
1 つの大きなクエリを返すのと、いくつかの小さなクエリを返すのとではどちらがよいでしょうか?
1 つの大きなクエリと多数の小さなクエリ?

リンクされたテーブルがたくさんあるデータベースがあります。

このようなもの(簡単にするために簡略化):

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|

ユーザーがログインすると、ダッシュボードに移動し、出席しているイベント (users_eventsテーブルから) と場所でのイベント (events_locations テーブルを使用)、および好きなバンドごとのイベントが表示されます。

ユーザーがイベントに参加していない可能性があり、イベントが時々その場所に表示されない可能性があることを考慮すると、このデータを取得するための最良の (最も効率的な) 方法は何でしょうか?

(クエリが実行されると、ユーザーが何度でもダッシュボードに戻る可能性があるため、クエリは 15 分間キャッシュされます。イベントに「参加」するとキャッシュ ファイルが更新されますが、その場所のイベントは 15 分ごとに更新されます。 )

私は現在、次のようなことを行っています (ただし、CodeIgniter のデータベース クラスを使用しているため、クエリはここに書かれているほど安全ではありません):

$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
}

これは小さな例です。さまざまなことに同様の設定がされたテーブルが他にもあります。ユーザー ダッシュボードを生成するために使用される 24 個のテーブルがあります。

大規模な JOIN クエリでこれを行う方がよいでしょうか? また、それを行う場合、クエリの正しい構文は何でしょうか?)

4

0 に答える 0