Why are no keys used in this EXPLAIN?

后端 未结 2 588

I was expecting this query to use a key.

mysql> DESCRIBE TABLE Foo;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Nu         


        
相关标签:
2条回答
  • 2021-01-26 09:23

    It could be because that the said table Foo very less volume of data. In such case optimizer will choose to do table scan rather than looking through index.

    As MySQL Documentation clearly says

    Indexes are less important for queries on small tables, or big tables where report queries process most or all of the rows. When a query needs to access most of the rows, reading sequentially is faster than working through an index. Sequential reads minimize disk seeks, even if not all the rows are needed for the query.

    0 讨论(0)
  • 2021-01-26 09:31

    From the MySQL Manual page entitled EXPLAIN Output Format:

    Impossible WHERE noticed after reading const tables (JSON property: message)

    MySQL has read all const (and system) tables and notice that the WHERE clause is always false.

    and the definition of const tables, from the Page entitled Constants and Constant Tables:

    A MySQL constant is something more than a mere literal in the query. It can also be the contents of a constant table, which is defined as follows:

    A table with zero rows, or with only one row

    A table expression that is restricted with a WHERE condition, containing expressions of the form column = constant, for all the columns of the table's primary key, or for all the columns of any of the table's unique keys (provided that the unique columns are also defined as NOT NULL).

    The second reference is a page and half long. Please refer to it.

    const

    const

    The table has at most one matching row, which is read at the start of the query. Because there is only one row, values from the column in this row can be regarded as constants by the rest of the optimizer. const tables are very fast because they are read only once.

    const is used when you compare all parts of a PRIMARY KEY or UNIQUE index to constant values. In the following queries, tbl_name can be used as a const table:

    SELECT * FROM tbl_name WHERE primary_key=1;

    SELECT * FROM tbl_name WHERE primary_key_part1=1 AND primary_key_part2=2;

    0 讨论(0)
提交回复
热议问题