MySQL在检查= 1时不使用索引,但在= 0时使用它 [英] MySQL not using index when checking = 1 , but using it with = 0

查看:196
本文介绍了MySQL在检查= 1时不使用索引,但在= 0时使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我遇到的一个令人困惑的问题:

Here is a perplexing issue I am having:

Query:
EXPLAIN SELECT id,hostname FROM queue_servers WHERE live=1

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra

1   SIMPLE  queue_servers   ALL     live    NULL    NULL    NULL    6   Using where







Query:
EXPLAIN SELECT id,hostname FROM queue_servers WHERE live=0

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra

1   SIMPLE  queue_servers   ref     live    live    1   const   1







SHOW INDEXES FROM queue_servers

Table Non_unique Key_name Seq_in_index Column_name Collation Cardinality Sub_part Packed Null Index_type

queue_servers   1   live    1   live    A   6   NULL    NULL        BTREE






任何i DEAS?这让我变得疯狂..如果我只是尝试选择这样一个列:


Any ideas? This is making me go crazy.. If I just try selecting a single column like this:

EXPLAIN SELECT id FROM queue_servers WHERE live=1

它工作得很好..但是如果我尝试选择列hostname,或者将它添加到选择列列表中,除非我搜索live = 0,否则它不会使用实时索引。为什么会这样?

It works just fine.. But if I try to select the column "hostname" , or add it to the select column list, it won't use the live index unless I am searching for live=0 .. Why is this?

推荐答案

为什么MySQL不使用索引?

如果行的大百分比,MySQL将不会使用索引该值。

Why doesn't MySQL not use an index?
MySQL will not use an index if a large percentage of the rows have that value.

为什么添加使用索引查询无法在此处工作 < br>
添加使用索引子句将无效,因为使用索引只会建议使用哪个索引,它不会建议是否使用索引。

Why will adding use index to the query not work here
Adding a use index clause will have no effect, because use index will only suggest which index to use, it will not suggest whether to use an index or not.

使用少量行的测试表时需要注意

这是特别的当使用几行的测试表时,由于MySQL会拒绝使用索引,因此很难看到你的查询出了什么问题。

因此请确保在测试表中添加足够的行以使其成为可能一个现实的测试。

Caveat when using test tables with few rows
This is especially vexing when using test tables with few rows as MySQL will refuse to use an index, and it's hard to see what's wrong with your query.
So make sure you add enough rows to a test table to make it a realistic test.

在低基数列上使用索引是否无用?

对布尔列的索引不是有用正如您在提出此问题之前所想的那样。

但它也不是无用

InnoDB 如果你的布尔字段有一个查询索引,MySQL会尝试使用索引检索数据:

Is using an index on low cardinality columns useless?
Indexing on boolean columns is not as useful as you thought before asking this question.
However it is also not useless either.
With InnoDB MySQL will try and retrieve data using the indexes if possible, if your boolean field has an index the query:

SELECT id, bool_field FROM table_with_many_columns_and_rows WHERE bool_field = 1

可以读取覆盖索引中 bool_field 因为InnoDB中的二级索引总是包含主索引(id)

这个更快,因为MySQL不需要读取整个表进入内存。

Can read all the data in the covering index for bool_field because secondary indexes in InnoDB always include the primary index (id) as well.
This is faster because MySQL does not have to read the entire table into memory.

在MyISAM中,这不起作用,MySQL将检查整个表。

In MyISAM this doesn't work and MySQL will examine the whole table.

但我可以使用强制索引

你可以,但是在低基数索引上,它会使您的查询更慢,而不是更快。
如果您知道MySQL用于选择索引的规则,则仅覆盖复杂查询的索引和

链接:

请参阅: http ://dev.mysql.com/doc/refman/5.1/en/mysql-indexes.html

和: http://www.xaprb.com/blog/2006/07/04/how-to-exploit -mysql-index-optimizations /

如果你想要一本关于这个主题的书:read

高性能MySQL: http://oreilly.com/catalog/9780596003067

If you want a book on this subject: read
High performance MySQL: http://oreilly.com/catalog/9780596003067

这篇关于MySQL在检查= 1时不使用索引,但在= 0时使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆