This topic created in 4878 days ago, the information mentioned may be changed or developed.
SELECT * FROM feeds WHERE user_id=249229547 AND status < 10000 AND ((act_pid = 0 AND cat = 3) OR act_pid > 0) and is_friends = 0
这个查询怎么建索引,测试(user_id,status,act_pid,cat,is_friends)无效
9 replies • 1970-01-01 08:00:00 +08:00
 |
|
1
eric_zyh Jan 28, 2013
(user_id,status,is_friends) 或 (user_id,status,is_friends,act_pid,cat)
|
 |
|
2
plprapper Jan 28, 2013
中间的OR逻辑 括号部分 从sql中拆出去吧,别放在sql里做。建索引除了sql外还和你的数据分布情况有关系。
|
 |
|
4
techzhou Jan 29, 2013
这个你不把explain贴出来么
|
 |
|
5
keakon Jan 29, 2013
把equal关系放前面啊,我估计执行时先查了is_friends,而不是status
|
 |
|
6
Cadina Jan 29, 2013
索引只看equal最大前缀,只有索引最后一个field用大于或小于才能完整利用索引
|
 |
|
8
ipconfiger Jan 29, 2013
都不用看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 Jan 29, 2013
另外,mysql在一个查询里头只能match一个索引,所以你需要根据使用的顺序 将 user_id,status,act_pid和is_friends合起来建一个联合索引才能在4个条件里都match上索引
自己explain试试就知道了
|