1

我有如下表关系:

表关系通过 phpmyadmin

我从 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] => 
        )

)


谢谢

4

3 回答 3

2

为您选择的列创建索引。

于 2013-06-13T15:10:35.520 回答
0

根据 EXPLAIN 输出,查询似乎已经使用 PRIMARY KEY 作为索引。你为什么不尝试拆分查询。

首先,获取要显示(分页)的场所的 ID。

SELECT id FROM venues v LIMIT $offset,$limit

$venue_ids = implode(',', $results);

接下来,运行此查询。

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

WHERE venues.id IN ( $venue_ids )

那至少应该摆脱派生表。

于 2013-06-13T19:00:26.623 回答
0

您需要将查询更改为以下内容;

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`

请记住,这只是代码的第一行。您需要在查询中的任何地方更改它。

因为你得到这个错误的原因是索引丢失,这样索引将以正确的方式定义,并且很容易让 php 区分。

于 2013-06-13T16:16:54.703 回答