php - SQL - 查询运行非常慢

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


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

ini_set('内存限制'​​, '-1');
ini_set('max_execution_time', 60);

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

我怎样才能摆脱这个问题? 请帮助我。

已更新
获取的行数是(事件 * 类型 * 食品 * 饮料 * 事件选项 * 样式 * 空间要求 * 功能)每个 field 都有。但是对我有用的行数不是乘法而是求和。


这是我使用 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] => 
        )

)


谢谢

最佳答案

为您选择的列创建索引。

关于php - SQL - 查询运行非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17090631/

相关文章:

php - 需要在不使用 PHP 的情况下从 Windows .cmd 脚本中找到 PHP 安装路径

mysql - 第 1 列包含名称,第 2 列包含值

php - PHP 中的 sprintf 长度说明符

php - 添加http ://if submitted URL doesn't begin with it

php - 检查两个用户在 php/mysql 中是否是 friend

php - 多个数据存储和获取的最佳方法是什么?

mysql - 选择 '' -''是什么意思

jquery - 有条件地加入mysql

php - 使用 PHP Doctrine ORM 的复杂 WHERE 子句

php - 如果PHP脚本中断,则显示错误消息