
1 eric_zyh 2013-01-28 20:28:53 +08:00 (user_id,status,is_friends) 或 (user_id,status,is_friends,act_pid,cat) |
2 plprapper 2013-01-28 20:41:34 +08:00 中间的OR逻辑 括号部分 从sql中拆出去吧,别放在sql里做。建索引除了sql外还和你的数据分布情况有关系。 |
3 xing393939 OP |
4 techzhou 2013-01-29 09:43:42 +08:00 这个你不把explain贴出来么 |
5 keakon 2013-01-29 11:09:31 +08:00 把equal关系放前面啊,我估计执行时先查了is_friends,而不是status |
6 Cadina 2013-01-29 11:11:36 +08:00 索引只看equal最大前缀,只有索引最后一个field用大于或小于才能完整利用索引 |
7 xing393939 OP @Cadina 那如何优化这个查询? |
8 ipconfiger 2013-01-29 16:34:02 +08:00 都不用看Explain的结果 or 直接就导致索引失效了 (SELECT * FROM feeds WHERE user_id=249229547 AND status < 10000 AND act_pid = 0 AND cat = 3 and is_friends = 0) union all (SELECT * FROM feeds WHERE user_id=249229547 AND status < 10000 AND act_pid > 0 and is_friends = 0) 这样子拆成2个子查询再union all 加起来就不会丢索引了 |
9 ipconfiger 2013-01-29 16:36:43 +08:00 另外,mysql在一个查询里头只能match一个索引,所以你需要根据使用的顺序 将 user_id,status,act_pid和is_friends合起来建一个联合索引才能在4个条件里都match上索引 自己explain试试就知道了 |