将索引放在主要用于查询的日期字段上对我有什么真正的好处吗?
dateField < 'var'
和
'var' BETWEEN dateField1 AND dateField2
搜索已经完成了很多,但我从来没有对它们进行直接比较“=”。
最佳答案
当然可以。范围搜索将受益于索引,就像相等搜索一样。
引自MySQL Reference Manual :: How MySQL Uses Indexes :
B-Tree Index Characteristics
A B-tree index can be used for column comparisons in expressions that use the =, >, >=, <, <=, or BETWEEN operators. The index also can be used for LIKE comparisons if the argument to LIKE is a constant string that does not start with a wildcard character.
在某些情况下,如果范围最终太大,优化器可能会决定不使用索引,因为表扫描实际上可能更快。使用 EXPLAIN
查看您的查询将使用哪些(如果有)索引。
关于mysql - 将索引放在 MySQL 中的日期字段上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3840348/