可能重复:
Is it better to return one big query or a few smaller ones?
One big query vs. many small ones?
我有一个包含大量链接表的数据库。
像这样的东西(为了方便简化):
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查询执行此操作会更好吗?如果我这样做,那么查询的正确语法是什么?)