简体   繁体   English

为什么这个查询使用where而不是index?

[英]Why is this query using where instead of index?

EXPLAIN EXTENDED SELECT  `board` . * 
FROM  `board` 
WHERE  `board`.`category_id` =  '5'
AND  `board`.`board_id` =  '0'
AND  `board`.`display` =  '1'
ORDER BY  `board`.`order` ASC

The output of the above query is 上述查询的输出是

id  select_type table   type    possible_keys   key key_len ref rows    filtered    Extra
1   SIMPLE  board   ref category_id_2   category_id_2   9   const,const,const   4   100.00  Using where

I'm a little confused by this because I have an index that contains the columns that I'm using in the same order they're used in the query...: 我有点困惑,因为我有一个索引,其中包含我使用的列,它们在查询中使用的顺序相同......:

category_id_2   BTREE   No  No 
category_id 33  A       
    board_id    33  A   
    display 33  A   
    order   66  A   

The output of EXPLAIN can sometimes be misleading. EXPLAIN的输出有时会产生误导。

For instance, filesort has nothing to do with files, using where does not mean you are using a WHERE clause, and using index can show up on the tables without a single index defined. 例如, filesort与文件无关, using where并不意味着你正在使用WHERE子句,并且using index可以在没有定义单个索引的情况下显示在表上。

Using where just means there is some restricting clause on the table ( WHERE or ON ), and not all record will be returned. Using where只表示表上有一些限制条款( WHEREON ),并不会返回所有记录。 Note that LIMIT does not count as a restricting clause (though it can be). 请注意, LIMIT不算作限制条款(尽管可以)。

Using index means that all information is returned from the index, without seeking the records in the table. Using index意味着从索引返回所有信息,而不寻求表中的记录。 This is only possible if all fields required by the query are covered by the index. 只有在索引覆盖查询所需的所有字段时,才可以执行此操作。

Since you are selecting * , this is impossible. 由于您选择* ,这是不可能的。 Fields other than category_id , board_id , display and order are not covered by the index and should be looked up. category_idboard_iddisplayorder以外的字段不在索引中,应该查找。

它实际上使用索引category_id_2

It's using the index category_id_2 properly, as shown by the key field of the EXPLAIN . 它正确使用索引category_id_2 ,如EXPLAINkey段所示。

Using where just means that you're selecting only some rows by using the WHERE statement, so you won't get the entire table back ;) Using where只意味着你只使用WHERE语句选择了一些行,所以你不会得到整个表;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM