I am facing a problem with indexing. I am not getting why the indexing is not working for the same query some times.
In below case indexing is working
EXPLAIN SELECT `table_name`.* FROM `table_name`
WHERE (created_date between '2018-08-11' and '2018-08-11');
| id | select_type | table | partitions | type |
possible_keys | key | key_len | ref | rows | filtered | Extra |
| 1 | SIMPLE | table_name | NULL | ref | index_table_name_created_date | table_name_created_date | 4 | const | 11 | 1.00 | Using index condition; Using where | +----+-------------+-------------------+------------+------+--------------------------------------+--------------------------------------+---------+-------+------+----------+------------------------------------+ 1 row in set, 1 warning (0.01 sec)
Same thing if i increase the date range indexing is not working like below one
EXPLAIN SELECT `table_name`.* FROM `table_name`
WHERE (created_date between '2018-08-11' and '2018-09-11');
+----+-------------+-------------------+------------+------+--------------------------------------+------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------------------+------------+------+------------------ | 1 | SIMPLE | table_name | NULL | ALL | index_table_name_created_date | NULL | NULL | NULL | 1233 | 0.30 | Using where | +----+-------------+-------------------+------------+------+--------------------------------------+------+---------+------+------+----------+-------------+ 1 row in set, 1 warning (0.00 sec)
Can anyone help me how to handle this case? Because of this to my query is taking longer time to execute.
my query is taking longer time to execute
... Actually both queries finish almost immediately. You may want to change your example such that it highlights the two behaviors better. – Tim Biegeleisen