在项目中需要删除表里的过期数据,表设置了key为gmt_created, 但是在执行"delete from table_name where to_days(gmt_created) < to_days(now()) -7"这个语句的时候并没有走索引。
研究后发现是因为在gmt_created上使用了to_days()这个函数后, 就不会走索引了,修改sql语句为:
delete from table_name where gmt_created < now() -interval 7 day;
可以发现现在开始走索引了。
结论:使用函数会导致索引无法时候。
使用函数导致索引无法使用
使用函数导致索引无法使用