SQL - 查询运行速度很慢

时间:2013-06-13 15:09:32

标签: php mysql

我有如下表格关系:

Table Relations Via 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


我正在使用此查询来检索与每个场地相关的所有配件。我需要在后端实现分页,所以我在Sub Query中使用LIMIT $ offset,$ limit。我找不到任何其他想法(查询),所以我可以达到我现在检索的相同结果。目前我正在使用

ini_set('memory_limit', '-1');
ini_set('max_execution_time', 60);

php函数忽略了这些问题,但我认为这不是最佳实践。此外,我正在考虑我可能需要检索50多个场地的所有配件的情况,max_execution_time = 60可能还不够。

如何摆脱这个问题? 请帮助我。

已更新
获取的行数是(events * types * foods * beverages * event_options * styles * 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] => 
        )

)


三江源

3 个答案:

答案 0 :(得分:2)

为您选择的列创建索引。

答案 1 :(得分: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区别开来。

答案 2 :(得分: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 )

这应该至少摆脱派生表。