• <small id='6AOia'></small><noframes id='6AOia'>

    <tfoot id='6AOia'></tfoot>
    <i id='6AOia'><tr id='6AOia'><dt id='6AOia'><q id='6AOia'><span id='6AOia'><b id='6AOia'><form id='6AOia'><ins id='6AOia'></ins><ul id='6AOia'></ul><sub id='6AOia'></sub></form><legend id='6AOia'></legend><bdo id='6AOia'><pre id='6AOia'><center id='6AOia'></center></pre></bdo></b><th id='6AOia'></th></span></q></dt></tr></i><div id='6AOia'><tfoot id='6AOia'></tfoot><dl id='6AOia'><fieldset id='6AOia'></fieldset></dl></div>

        • <bdo id='6AOia'></bdo><ul id='6AOia'></ul>
        <legend id='6AOia'><style id='6AOia'><dir id='6AOia'><q id='6AOia'></q></dir></style></legend>

        MySQL 日期时间索引不起作用

        MySQL datetime index is not working(MySQL 日期时间索引不起作用)
        <legend id='KXvxQ'><style id='KXvxQ'><dir id='KXvxQ'><q id='KXvxQ'></q></dir></style></legend>

          <i id='KXvxQ'><tr id='KXvxQ'><dt id='KXvxQ'><q id='KXvxQ'><span id='KXvxQ'><b id='KXvxQ'><form id='KXvxQ'><ins id='KXvxQ'></ins><ul id='KXvxQ'></ul><sub id='KXvxQ'></sub></form><legend id='KXvxQ'></legend><bdo id='KXvxQ'><pre id='KXvxQ'><center id='KXvxQ'></center></pre></bdo></b><th id='KXvxQ'></th></span></q></dt></tr></i><div id='KXvxQ'><tfoot id='KXvxQ'></tfoot><dl id='KXvxQ'><fieldset id='KXvxQ'></fieldset></dl></div>

              <tbody id='KXvxQ'></tbody>
            <tfoot id='KXvxQ'></tfoot>
            • <bdo id='KXvxQ'></bdo><ul id='KXvxQ'></ul>
              • <small id='KXvxQ'></small><noframes id='KXvxQ'>

                1. 本文介绍了MySQL 日期时间索引不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  表结构:

                  +-------------+----------+------+-----+---------+----------------+
                  | Field       | Type     | Null | Key | Default | Extra          |
                  +-------------+----------+------+-----+---------+----------------+
                  | id          | int(11)  | NO   | PRI | NULL    | auto_increment |
                  | total       | int(11)  | YES  |     | NULL    |                |
                  | thedatetime | datetime | YES  | MUL | NULL    |                |
                  +-------------+----------+------+-----+---------+----------------+
                  

                  总行数:137967

                  mysql> explain select * from out where thedatetime <= NOW();
                  +----+-------------+-------------+------+---------------+------+---------+------+--------+-------------+
                  | id | select_type | table       | type | possible_keys | key  | key_len | ref  | rows   | Extra       |
                  +----+-------------+-------------+------+---------------+------+---------+------+--------+-------------+
                  |  1 | SIMPLE      | out         | ALL  | thedatetime   | NULL | NULL    | NULL | 137967 | Using where |
                  +----+-------------+-------------+------+---------------+------+---------+------+--------+-------------+
                  

                  实际查询的时间更长,表连接数更多,关键是,我无法让表使用 datetime 索引.如果我想选择特定日期之前的所有数据,这对我来说会很困难.但是,我注意到如果我选择较小的数据子集,我可以让 MySQL 使用索引.

                  The real query is much more longer with more table joins, the point is, I can't get the table to use the datetime index. This is going to be hard for me if I want to select all data until certain date. However, I noticed that I can get MySQL to use the index if I select a smaller subset of data.

                  mysql> explain select * from out where thedatetime <= '2008-01-01';
                  +----+-------------+-------------+-------+---------------+-------------+---------+------+-------+-------------+
                  | id | select_type | table       | type  | possible_keys | key         | key_len | ref  | rows  | Extra       |
                  +----+-------------+-------------+-------+---------------+-------------+---------+------+-------+-------------+
                  |  1 | SIMPLE      | out         | range | thedatetime   | thedatetime | 9       | NULL | 15826 | Using where |
                  +----+-------------+-------------+-------+---------------+-------------+---------+------+-------+-------------+
                  
                  mysql> select count(*) from out where thedatetime <= '2008-01-01';
                  +----------+
                  | count(*) |
                  +----------+
                  |    15990 |
                  +----------+
                  

                  那么,我该怎么做才能确保无论我放置什么日期,MySQL 都会使用该索引?

                  So, what can I do to make sure MySQL will use the index no matter what date that I put?

                  推荐答案

                  一切正常.:)

                  索引是为了加快检索速度.他们使用索引查找来完成.

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

                  在您第一次查询时未使用索引,因为您正在检索所有行,并且在这种情况下使用索引较慢(lookup indexget rowlookup index, get row... x 行数比 get all rows == table scan) 慢)

                  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.

                  优化器的工作是使用 RDBMS 保留在索引上的统计信息来确定最佳计划.在第一种情况下考虑了索引,但计划者(正确地)将其丢弃了.

                  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.

                  编辑
                  您可能想阅读this之类的内容,以了解有关mysql 查询计划器.

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

                  这篇关于MySQL 日期时间索引不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                  MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)
                  MySQL cumulative sum grouped by date(按日期分组的 MySQL 累计总和)

                  <small id='OJB6s'></small><noframes id='OJB6s'>

                  <legend id='OJB6s'><style id='OJB6s'><dir id='OJB6s'><q id='OJB6s'></q></dir></style></legend>
                      <bdo id='OJB6s'></bdo><ul id='OJB6s'></ul>
                        <tbody id='OJB6s'></tbody>
                        <i id='OJB6s'><tr id='OJB6s'><dt id='OJB6s'><q id='OJB6s'><span id='OJB6s'><b id='OJB6s'><form id='OJB6s'><ins id='OJB6s'></ins><ul id='OJB6s'></ul><sub id='OJB6s'></sub></form><legend id='OJB6s'></legend><bdo id='OJB6s'><pre id='OJB6s'><center id='OJB6s'></center></pre></bdo></b><th id='OJB6s'></th></span></q></dt></tr></i><div id='OJB6s'><tfoot id='OJB6s'></tfoot><dl id='OJB6s'><fieldset id='OJB6s'></fieldset></dl></div>

                          <tfoot id='OJB6s'></tfoot>