我有如下表关系:
我从 php 运行以下查询并遇到两个大问题。
1) 超出最大内存限制
2) 超出最大执行时间
SELECT DISTINCT venues.name, locations.location, events.event, types.type, foods.food, beverages.beverage, event_options.event_option, styles.style, space_requirements.space_requirement, features.feature
FROM (SELECT * FROM venues v LIMIT $offset,$limit) venues
INNER JOIN locations ON venues.location_id = locations.location_id
LEFT JOIN venue_events ON venues.venue_id = venue_events.venue_id
LEFT JOIN events ON events.event_id = venue_events.event_id
LEFT JOIN venue_types ON venues.venue_id = venue_types.venue_id
LEFT JOIN types ON types.type_id = venue_types.type_id
LEFT JOIN venue_foods ON venues.venue_id = venue_foods.venue_id
LEFT JOIN foods ON foods.food_id = venue_foods.food_id
LEFT JOIN venue_beverages ON venues.venue_id = venue_beverages.venue_id
LEFT JOIN beverages ON beverages.beverage_id = venue_beverages.beverage_id
LEFT JOIN venue_event_options ON venues.venue_id = venue_event_options.venue_id
LEFT JOIN event_options ON event_options.event_option_id = venue_event_options.event_option_id
LEFT JOIN venue_styles ON venues.venue_id = venue_styles.venue_id
LEFT JOIN styles ON styles.style_id = venue_styles.style_id
LEFT JOIN venue_space_requirements ON venues.venue_id = venue_space_requirements.venue_id
LEFT JOIN space_requirements ON space_requirements.space_requirement_id = venue_space_requirements.space_requirement_id
LEFT JOIN venue_features ON venues.venue_id = venue_features.venue_id
LEFT JOIN features ON features.feature_id = venue_features.feature_id
我正在使用此查询来检索与每个场地相关的所有配件。我需要在后端实现分页,所以我在子查询中使用 LIMIT $offset,$limit 。我找不到任何其他想法(查询),所以我可以获得与现在检索相同的结果。目前我正在使用
ini_set('memory_limit', '-1');
ini_set('max_execution_time', 60);
php 函数可以忽略这些问题,但我认为这不是最佳做法。我也在考虑可能需要找回50多个场地的所有配件的情况,这max_execution_time = 60
可能还不够。
我怎样才能摆脱这个问题?请帮我。
更新
获取的行数是(事件 * 类型 * 食物 * 饮料 * event_options * 样式 * space_requirements * features)每个场地都有。但是对我有用的行数不是乘法而是总和。
这是我使用EXPLAIN
关键字运行查询后发现的结果
Array
(
[0] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] =>
[type] => ALL
[possible_keys] =>
[key] =>
[key_len] =>
[ref] =>
[rows] => 11
[Extra] => Using temporary
)
[1] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => venue_events
[type] => ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 4
[ref] => venues.venue_id
[rows] => 3
[Extra] => Using index
)
[2] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => events
[type] => eq_ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 1
[ref] => serofero_mvb.venue_events.event_id
[rows] => 1
[Extra] =>
)
[3] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => venue_types
[type] => ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 4
[ref] => venues.venue_id
[rows] => 7
[Extra] => Using index
)
[4] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => types
[type] => eq_ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 1
[ref] => serofero_mvb.venue_types.type_id
[rows] => 1
[Extra] =>
)
[5] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => venue_foods
[type] => ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 4
[ref] => venues.venue_id
[rows] => 3
[Extra] => Using index
)
[6] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => foods
[type] => eq_ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 1
[ref] => serofero_mvb.venue_foods.food_id
[rows] => 1
[Extra] => Using index
)
[7] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => venue_beverages
[type] => ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 4
[ref] => venues.venue_id
[rows] => 3
[Extra] => Using index
)
[8] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => beverages
[type] => eq_ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 1
[ref] => serofero_mvb.venue_beverages.beverage_id
[rows] => 1
[Extra] =>
)
[9] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => venue_event_options
[type] => ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 4
[ref] => venues.venue_id
[rows] => 2
[Extra] => Using index
)
[10] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => event_options
[type] => eq_ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 1
[ref] => serofero_mvb.venue_event_options.event_option_id
[rows] => 1
[Extra] =>
)
[11] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => venue_styles
[type] => ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 4
[ref] => venues.venue_id
[rows] => 1
[Extra] => Using index
)
[12] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => styles
[type] => eq_ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 1
[ref] => serofero_mvb.venue_styles.style_id
[rows] => 1
[Extra] =>
)
[13] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => venue_space_requirements
[type] => ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 4
[ref] => venues.venue_id
[rows] => 3
[Extra] => Using index
)
[14] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => space_requirements
[type] => eq_ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 1
[ref] => serofero_mvb.venue_space_requirements.space_requirement_id
[rows] => 1
[Extra] =>
)
[15] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => locations
[type] => eq_ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 1
[ref] => venues.location_id
[rows] => 1
[Extra] =>
)
[16] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => venue_features
[type] => ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 4
[ref] => venues.venue_id
[rows] => 7
[Extra] => Using index
)
[17] => Array
(
[id] => 1
[select_type] => PRIMARY
[table] => features
[type] => eq_ref
[possible_keys] => PRIMARY
[key] => PRIMARY
[key_len] => 1
[ref] => serofero_mvb.venue_features.feature_id
[rows] => 1
[Extra] =>
)
[18] => Array
(
[id] => 2
[select_type] => DERIVED
[table] => venues
[type] => ALL
[possible_keys] =>
[key] =>
[key_len] =>
[ref] =>
[rows] => 11
[Extra] =>
)
)
谢谢