我有一个名为t_owecoupon
的表,它有一个名为index_cpn_endvalidity
的索引。
+-------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------------+------------+--------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| t_owecoupon | 1 | index_cpn_endval | 1 | ENDVALIDITY | A | 4 | NULL | NULL | | BTREE | | |
(抱歉,我无法更好地查看我的查询结果) MySQL从不使用这个索引...... 例如,以下是对简单查询的解释结果
explain select * from t_owecoupon where endvalidity > date('2015-01-01');
+----+-------------+-------------+------+------------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------+------+------------------+------+---------+------+--------+-------------+
| 1 | SIMPLE | t_owecoupon | ALL | index_cpn_endval | NULL | NULL | NULL | 307017 | Using where |
+----+-------------+-------------+------+------------------+------+---------+------+--------+-------------+
输出总是一样的,它说我可以在“可能的键”中使用我想要的索引...上面的查询是一个例子,我有一个更复杂的查询,它也没有使用索引
我试过:使用索引,按索引列分组,按索引列排序,但解释的输出始终相同。
如何让MySQL使用我的索引?
感谢您的帮助。
真正的查询 -
SELECT
date_format(endvalidity,'%Y-%m') as mois, cpn.ABONNE_id, cpn.statut, regulation, cpn.id
FROM powecpn.t_owecoupon as cpn, t_oweabonne as abo
WHERE cpn.ABONNE_id = abo.ID
and abo.usi != '0000000000'
and date_format(DATE_ADD(endvalidity, INTERVAL 6 MONTH),'%Y-%m') > @DATE_ANNEE_MOIS
and endvalidity < DATE_ADD(LAST_DAY(str_to_date(CONCAT(@DATE_ANNEE_MOIS,'-1'),"%Y-%m-%d")),INTERVAL 1 DAY)
group by mois, cpn.ABONNE_id, cpn.ID