0

我正在尝试在我的数据库中运行以下查询:

SELECT * FROM ts_cards WHERE ( cardstatus= 2 OR cardstatus= 3 ) AND ( cardtype= 1 OR cardtype= 2 ) ORDER BY cardserial DESC LIMIT 10;

所有三个字段(cardstatus、cardtype 和carderial)都被索引:

    mysql> SHOW INDEX FROM ts_cards;
    +----------+------------+----------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+
    | Table    | Non_unique | Key_name       | Seq_in_index | Column_name       | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
    +----------+------------+----------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+
    | ts_cards |          0 | PRIMARY        |            1 | card_id           | A         |    15000134 |     NULL | NULL   |      | BTREE      |         |
    | ts_cards |          1 | CardID         |            1 | cardserial        | A         |    15000134 |     NULL | NULL   |      | BTREE      |         |
    | ts_cards |          1 | CardType       |            1 | cardtype          | A         |          17 |     NULL | NULL   |      | BTREE      |         |
    | ts_cards |          1 | CardHolder     |            1 | cardstatusholder  | A         |          17 |     NULL | NULL   |      | BTREE      |         |
    | ts_cards |          1 | CardExpiration |            1 | cardexpiredstatus | A         |          17 |     NULL | NULL   |      | BTREE      |         |
    | ts_cards |          1 | CardStatus     |            1 | cardstatus        | A         |          17 |     NULL | NULL   |      | BTREE      |         |
    +----------+------------+----------------+--------------+-------------------+-----------+-------------+----------+--------+------+------------+---------+
    6 rows in set (0.22 sec)

(是的,我知道索引的名字很烂)

然而,默认情况下,MySQL 只使用 cardstatus 的索引:

    mysql> EXPLAIN SELECT * FROM `ts_cards` WHERE ( cardstatus= 2 OR cardstatus= 3 ) AND ( cardtype= 1 OR cardtype= 2 ) ORDER BY cardserial DESC LIMIT 10;
    +----+-------------+----------+-------+---------------------+------------+---------+------+---------+-----------------------------+
    | id | select_type | table    | type  | possible_keys       | key        | key_len | ref  | rows    | Extra                       |
    +----+-------------+----------+-------+---------------------+------------+---------+------+---------+-----------------------------+
    |  1 | SIMPLE      | ts_cards | range | CardType,CardStatus | CardStatus | 1       | NULL | 3215967 | Using where; Using filesort |
    +----+-------------+----------+-------+---------------------+------------+---------+------+---------+-----------------------------+
    1 row in set (0.00 sec)

(它甚至不考虑卡片上的索引,但我想这是另一个问题。)

使用 "USE KEY" 或 "FORCE KEY" 可以使它使用 cardtype 的索引,但不能同时使用 cardtype 和 cardstatus:

    mysql> EXPLAIN SELECT * FROM `ts_cards` FORCE KEY (CardType) WHERE ( cardstatus= 2 OR cardstatus= 3 ) AND ( cardtype= 1 OR cardtype= 2 ) ORDER BY cardserial DESC LIMIT 10;
    +----+-------------+----------+-------+---------------+----------+---------+------+---------+-----------------------------+
    | id | select_type | table    | type  | possible_keys | key      | key_len | ref  | rows    | Extra                       |
    +----+-------------+----------+-------+---------------+----------+---------+------+---------+-----------------------------+
    |  1 | SIMPLE      | ts_cards | range | CardType      | CardType | 1       | NULL | 6084861 | Using where; Using filesort |
    +----+-------------+----------+-------+---------------+----------+---------+------+---------+-----------------------------+
    1 row in set (0.00 sec)

    mysql> EXPLAIN SELECT * FROM `ts_cards` FORCE KEY (CardType,CardStatus) WHERE ( cardstatus= 2 OR cardstatus= 3 ) AND ( cardtype= 1 OR cardtype= 2 ) ORDER BY cardserial DESC LIMIT 10;
    +----+-------------+----------+-------+---------------------+------------+---------+------+---------+-----------------------------+
    | id | select_type | table    | type  | possible_keys       | key        | key_len | ref  | rows    | Extra                       |
    +----+-------------+----------+-------+---------------------+------------+---------+------+---------+-----------------------------+
    |  1 | SIMPLE      | ts_cards | range | CardType,CardStatus | CardStatus | 1       | NULL | 3215967 | Using where; Using filesort |
    +----+-------------+----------+-------+---------------------+------------+---------+------+---------+-----------------------------+
    1 row in set (0.00 sec)

如何强制 MySQL 使用 BOTH 索引来加快查询速度?cardtype 和 cardstatus 索引似乎都以相同的方式定义,但 cardstatus 似乎优先于 cardtype。

4

1 回答 1

0

IIRC,MySQL 不能在同一个查询中使用两个不同的索引。要使用这两个索引,MySQL 需要将它们合并为一个(链接到手册)。以下是此类合并的示例(单击“查看执行计划”)。注意第一个的“index_merge” SELECT

免责声明:我不确定以上信息。

在您的情况下,尽管有您的提示,优化器仍然认为直接扫描第二个表比合并索引更快(您的表可能有非常多的行,因此是一个非常大、操作成本高的索引)。

我建议:

ALTER TABLE ADD INDEX CardTypeStatus (cardtype, cardstatus);

这会在两列上创建一个索引。您的查询可能会使用此索引。之后您可能希望删除CardType索引:查询仍然可以使用两列索引,即使它们cardtype仅搜索列(但如果它们cardstatus仅搜索则不能)。

有关多列索引的更多信息:http: //dev.mysql.com/doc/refman/5.5/en/multiple-column-indexes.html

于 2012-06-22T13:08:46.247 回答