MySQL datetime index is not working

后端 未结 2 1961
悲&欢浪女
悲&欢浪女 2020-12-10 03:42

Table structure:

+-------------+----------+------+-----+---------+----------------+
| Field       | Type     | Null | Key | Default | Extra          |
+-----         


        
2条回答
  •  渐次进展
    2020-12-10 04:27

    Everything works as it is supposed to. :)

    Indexes are there to speed up retrieval. They do it using index lookups.

    In you first query the index is not used because you are retrieving ALL rows, and in this case using index is slower (lookup index, get row, lookup index, get row... x number of rows is slower then get all rows == table scan)

    In the second query you are retrieving only a portion of the data and in this case table scan is much slower.

    The job of the optimizer is to use statistics that RDBMS keeps on the index to determine the best plan. In first case index was considered, but planner (correctly) threw it away.

    EDIT
    You might want to read something like this to get some concepts and keywords regarding mysql query planner.

提交回复
热议问题