如何说服这个MYSQL查询使用索引?

时间:2012-02-28 14:47:06

标签: mysql indexing database-indexes

以下是有问题查询的EXPLAIN ..

    mysql> EXPLAIN SELECT orders_0.id, orders_0.created_at, orders_0.total_amount, orders_0.delivery_date, orders_0.customer_id, orders_0.items_summary, orders_0.site_id, orders_0.depot_id, orders_0.region_id, addresses_0.country, orders_0.payment_status, orders_0.created_by, orders_0.status, orders_0.delivered_by
        -> FROM production.addresses addresses_0, production.orders orders_0
        -> WHERE orders_0.delivery_address_id = addresses_0.id AND ((orders_0.status Not In ('cancelled','checkout','expired')) AND (orders_0.delivery_date>{d '2011-10-31'}));
    +----+-------------+-------------+--------+------------------------------------------+---------+---------+-----------------------------------------------+--------+-------------+
    | id | select_type | table       | type   | possible_keys                            | key     | key_len | ref                                           | rows   | Extra       |
    +----+-------------+-------------+--------+------------------------------------------+---------+---------+-----------------------------------------------+--------+-------------+
    |  1 | SIMPLE      | orders_0    | ALL    | status,delivery_date,delivery_address_id | NULL    | NULL    | NULL                                          | 929330 | Using where | 
    |  1 | SIMPLE      | addresses_0 | eq_ref | PRIMARY                                  | PRIMARY | 4       | production.orders_0.delivery_address_id |      1 |             | 
    +----+-------------+-------------+--------+------------------------------------------+---------+---------+-----------------------------------------------+--------+-------------+
    2 rows in set (0.00 sec)

    mysql>   

我在三个列上都有索引,它正在尝试做条件,但遗憾的是它并没有尝试使用索引合并。是创建多列索引的唯一选择还是有些东西会使用这些索引关闭MYSQL?

1 个答案:

答案 0 :(得分:1)

您必须创建一个多列索引。

例如,要在Phpmyadmin中执行此操作,您必须检查表的这些不同字段,然后单击表结构下方的“索引”按钮。