犯罪嫌疑人X
尝试强制使用此索引:SELECT `user_metrics`.*FROM `user_metrics` FORCE INDEX (index_user_metrics_on_user_id)WHERE (`user_metrics`.user_id IN (N,N,N,N,N,N,N,N,N,N,N,N))我刚刚检查过,它确实在完全相同的查询上使用了索引:EXPLAIN EXTENDEDSELECT * FROM tests WHERE (test IN ('test 1', 'test 2', 'test 3', 'test 4', 'test 5', 'test 6', 'test 7', 'test 8', 'test 9'))1, 'SIMPLE', 'tests', 'range', 'ix_test', 'ix_test', '602', '', 9, 100.00, 'Using where'
绝地无双
我知道我迟到了。但是希望我可以帮助其他有类似问题的人。最近,我遇到了同样的问题。然后,我决定使用自我结合来解决我的问题。问题不是MySQL。问题是我们。子查询的返回类型与我们的表不同。因此,我们必须将子查询的类型转换为选择列的类型。下面是示例代码:select `user_metrics`.* from `user_metrics` um join (select `user_metrics`.`user_id` in (N, N, N, N) ) as temp on um.`user_id` = temp.`user_id`或我自己的代码:旧:(不使用索引:〜4s)SELECT `jxm_character`.*FROM jxm_characterWHERE information_date IN (SELECT DISTINCT (information_date) FROM jxm_character WHERE information_date >= DATE_SUB('2016-12-2', INTERVAL 7 DAY)) AND `jxm_character`.`ranking_type` = 1 AND `jxm_character`.`character_id` = 3146089;新:(使用指数:〜0.02s)SELECT *FROM jxm_character jc JOIN (SELECT DISTINCT (information_date) FROM jxm_character WHERE information_date >= DATE_SUB('2016-12-2', INTERVAL 7 DAY)) AS temp ON jc.information_date = STR_TO_DATE(temp.information_date, '%Y-%m-%d') AND jc.ranking_type = 1 AND jc.character_id = 3146089;jxm_character:记录:〜350万PK:jxm_character(信息日期,ranking_type,character_id)SHOW VARIABLES LIKE '%version%';'protocol_version', '10''version', '5.1.69-log''version_comment', 'Source distribution'最后说明:确保您了解MySQL索引最左边的规则。P / s:对不起,我的英语不好。我发布了代码(当然是生产版)以清除我的解决方案:D。