问题
EXPLAIN EXTENDED
SELECT * FROM table_name
-- FORCE INDEX(dummy_date)
USE INDEX(dummy_date)
where dummy_date
between '2020-12-01' AND '2020-12-31'
AND name='something';
ALTER TABLE `table_name` ADD INDEX `dummy_date_index`(`dummy_date`)
When I execute this query then indexing is not working but if I use force index
then its working with index. Please let me know the correct approach to use the index for millions of rows.
回答1:
Even better than MySQL not using indexes with WHERE IN clause? --
Use a better index and don't bother with USE or FORCE:
INDEX(name, dummy_date)
Anyway, if most of the table is in that month, then using INDEX(dummy_date)
is less efficient than scanning the table.
来源:https://stackoverflow.com/questions/66007286/in-mysql-use-index-is-not-working-but-force-index-seems-working-fine