MySQL索引长时间运行的查询 - 是否使用了密钥?

时间:2016-10-24 02:21:26

标签: mysql

我正在处理我的数据库(mysql)的大表。我的一些查询运行时间超过5分钟。以下是我的查询运行缓慢的示例:

select b.DESCRIPTION collateral_type, a.description brand, a.year,
       a.model, a.plate_number, d.description fuel, a.chassis_number,
       a.engine_number, c.description as color, insurance_name 
from lms_loan_application_collateral a inner join
     lms_collateral_type b
     on a.COLLATERAL_TYPE_ID = b.id left join
     lms_color c
     on a.color_id = c.id left join
     lms_fuel_type d
     on a.fuel_type_id = d.id inner join
     lms_loan_application e
     on e.id = a.loan_application_id inner join
     lms_dlr_dtl f
     on e.code = f.lano inner join
     lms_loanapp_dtl g
     on f.lano = g.lano
 where b.description in ('Motorcycle', 'Automotive', 'Heavy Equipment');

这是查询的mysql解释

    +----+-------------+-------+-------+--------------------------------------------------------+-------------------------+---------+------------+-------+----------------------------------------------------+
    | id | select_type | table | type  | possible_keys                                          | key                     | key_len | ref        | rows  | Extra                                              |
    +----+-------------+-------+-------+--------------------------------------------------------+-------------------------+---------+------------+-------+----------------------------------------------------+
    |  1 | SIMPLE      | g     | index | lms_loanapp_dtl_lano                                   | lms_loanapp_dtl_lano    | 23      | NULL       | 23432 | Using where; Using index                           |
    |  1 | SIMPLE      | f     | ref   | lano                                                   | lano                    | 23      | new.g.LANO |     1 | Using index                                        |
    |  1 | SIMPLE      | b     | ALL   | lms_collateral_type_description,lms_collateral_type_id | NULL                    | NULL    | NULL       |    11 | Using where; Using join buffer (Block Nested Loop) |
    |  1 | SIMPLE      | a     | ref   | collateral_type_id                                     | collateral_type_id      | 5       | new.b.ID   |  4067 | Using index condition                              |
    |  1 | SIMPLE      | e     | ref   | lms_loan_application_id                                | lms_loan_application_id | 153     | func       |     1 | Using index condition; Using where                 |
    |  1 | SIMPLE      | c     | ALL   | LMS_color_id                                           | NULL                    | NULL    | NULL       |    20 | Range checked for each record (index map: 0x1)     |
    |  1 | SIMPLE      | d     | ALL   | LMS_fuel_type_id                                       | NULL                    | NULL    | NULL       |     4 | Using where; Using join buffer (Block Nested Loop) |
    +----+-------------+-------+-------+--------------------------------------------------------+-------------------------+---------+------------+-------+----------------------------------------------------+
键列下的

,有NULL值。我不确定mysql是否使用了我创建的索引键。

下面是我为相关表创建的索引列表:

显示来自lms_loan_application_collat​​eral FROM IND的索引;

    +---------------------------------+------------+---------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table                           | Non_unique | Key_name            | Seq_in_index | Column_name         | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +---------------------------------+------------+---------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | lms_loan_application_collateral |          1 | color_id            |            1 | color_id            | A         |          36 |     NULL | NULL   | YES  | BTREE      |         |               |
    | lms_loan_application_collateral |          1 | fuel_type_id        |            1 | fuel_type_id        | A         |           6 |     NULL | NULL   | YES  | BTREE      |         |               |
    | lms_loan_application_collateral |          1 | loan_application_id |            1 | loan_application_id | A         |       89493 |     NULL | NULL   | YES  | BTREE      |         |               |
    | lms_loan_application_collateral |          1 | collateral_type_id  |            1 | collateral_type_id  | A         |          22 |     NULL | NULL   | YES  | BTREE      |         |               |
    +---------------------------------+------------+---------------------+--------------+---------------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

显示来自lms_collat​​eral_type FROM IND的索引;

    +---------------------+------------+---------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table               | Non_unique | Key_name                        | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +---------------------+------------+---------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | lms_collateral_type |          1 | lms_collateral_type_description |            1 | DESCRIPTION | A         |          11 |     NULL | NULL   | YES  | BTREE      |         |               |
    | lms_collateral_type |          1 | lms_collateral_type_id          |            1 | ID          | A         |          11 |     NULL | NULL   | YES  | BTREE      |         |               |
    +---------------------+------------+---------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

显示来自lms_color FROM IND的索引;

    +-----------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table     | Non_unique | Key_name     | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +-----------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | lms_color |          1 | LMS_color_id |            1 | id          | A         |          20 |     NULL | NULL   |      | BTREE      |         |               |
    +-----------+------------+--------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

显示来自lms_fuel_type FROM IND的索引;

    +---------------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table         | Non_unique | Key_name         | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +---------------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | lms_fuel_type |          1 | LMS_fuel_type_id |            1 | id          | A         |           4 |     NULL | NULL   | YES  | BTREE      |         |               |
    +---------------+------------+------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

显示来自lms_loan_application FROM NEW的索引;

    +----------------------+------------+-------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table                | Non_unique | Key_name                | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +----------------------+------------+-------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | lms_loan_application |          1 | lmspis_id               |            1 | PIS_ID       | A         |        1878 |     NULL | NULL   | YES  | BTREE      |         |               |
    | lms_loan_application |          1 | loan_type_id            |            1 | LOAN_TYPE_ID | A         |           6 |     NULL | NULL   | YES  | BTREE      |         |               |
    | lms_loan_application |          1 | lms_loan_application_id |            1 | ID           | A         |        1878 |     NULL | NULL   | YES  | BTREE      |         |               |
    +----------------------+------------+-------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

显示来自lms_dlr_dtl FROM NEW的索引;

    +-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table       | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | lms_dlr_dtl |          0 | PRIMARY  |            1 | LDDID       | A         |       90066 |     NULL | NULL   |      | BTREE      |         |               |
    | lms_dlr_dtl |          1 | lano     |            1 | LANO        | A         |       90066 |     NULL | NULL   | YES  | BTREE      |         |               |
    +-------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

显示来自lms_loanapp_dtl FROM IND;

的索引
    +-----------------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table           | Non_unique | Key_name             | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +-----------------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | lms_loanapp_dtl |          0 | PRIMARY              |            1 | LLADID      | A         |       23432 |     NULL | NULL   |      | BTREE      |         |               |
    | lms_loanapp_dtl |          1 | lms_loanapp_dtl_lano |            1 | LANO        | A         |       23432 |     NULL | NULL   | YES  | BTREE      |         |               |
    +-----------------+------------+----------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

2 个答案:

答案 0 :(得分:0)

表示类型为ALL的表不使用索引。在表上的id列上创建索引在c和d中表示,在b上的描述和id列上都有一个索引。

create index ix_id on lms_color(id);
create index ix_id on lms_fuel_type(id); 
create index ix_description_id on lms_collateral_type(description,id); 

答案 1 :(得分:0)

由于基数较低,MySQL正在为您的WHERE使用ALL。因此,MySQL认为仅使用11个条目进行表扫描比使用带索引的二进制搜索更便宜。

真正的问题是您不使用索引具有更高基数(lms_loanapp_dtl_lanocollateral_type_id)的列进行过滤。