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