4

我一直在尝试在查询中使用索引,但无济于事。它已定义,但未使用。

这是我的索引

Table  | Non_unique | Key_name   | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment  
--------------------------------------------------------------------------------------------------------------------------------------------------------
alerts |          1 | date_index |            1 | date        | A         |        9825 |     NULL | NULL   |      | BTREE   

这是我的查询+解释:

EXPLAIN SELECT latitude, longitude, msg_id
        FROM alerts
        FORCE INDEX(date_index)
        WHERE date>1336312075;


| id | select_type | table  | type | possible_keys | key  | key_len | ref  | rows | Extra       |
--------------------------------------------------------------------------------------------------------
|  1 | SIMPLE      | alerts | ALL  | date_index    | NULL | NULL    | NULL | 9825 | Using where |

该表有 9825 行,而我选择的日期是最新的 9 行之一。所以它应该只扫描了 9 行。

有任何想法吗?谢谢

4

1 回答 1

4

只是一个猜测:你的date专栏是什么类型的?是否有可能,该值1336312075不是 type date?在这种情况下,数据库可能会将date列中的所有值转换为类型,1336312075而不是转换1336312075date可能使索引无用的列类型。

可能的解决方案:将您的值转换为date列的类型:

where date > convert_to_the_type_of_date_column(1336312075)
于 2012-06-13T10:19:01.543 回答