explain select * from A ,B where a.userid = b.reuserid and b.userid = 13 and a.type=107 order by a.add_time desc limit 1,30;
| id | select_type | table | type | key | key_len | ref | rows | Extra
| 1 | SIMPLE | b | ref | userid | 4 | const | 6630 | Using temporary;
| 1 | SIMPLE | a | ref | userid_2 | 10 | b.reuserid,const | 5 | Using where 数据量:
A表 18w
B表 50w索引:
A表 联合索引:(userid,add_time)
B表 联合索引:(userid,reuserid) 单独索引 userid 、reuserid 表及字段含义:
A表 :用户动态
B表 :用户关注情况表
userid :用户ID
reuserid:被关注用户ID
SQL含义:
查询13号会员关注的人的最近30条动态。
explain出来的结果,索引的走向是:
1.B表的 userid
2.A表的 userid,add_time哪位牛人解释一下为什么这个SQL会慢到卡死?应该如何改进?
| id | select_type | table | type | key | key_len | ref | rows | Extra
| 1 | SIMPLE | b | ref | userid | 4 | const | 6630 | Using temporary;
| 1 | SIMPLE | a | ref | userid_2 | 10 | b.reuserid,const | 5 | Using where 数据量:
A表 18w
B表 50w索引:
A表 联合索引:(userid,add_time)
B表 联合索引:(userid,reuserid) 单独索引 userid 、reuserid 表及字段含义:
A表 :用户动态
B表 :用户关注情况表
userid :用户ID
reuserid:被关注用户ID
SQL含义:
查询13号会员关注的人的最近30条动态。
explain出来的结果,索引的走向是:
1.B表的 userid
2.A表的 userid,add_time哪位牛人解释一下为什么这个SQL会慢到卡死?应该如何改进?
'截至2011-11-30 17:07:06 用户结帖率25.00% 当您的问题得到解答后请及时结贴.
http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html8、如何给分和结贴?
http://community.csdn.net/Help/HelpCenter.htm#结帖
如果A表只有(type,add_time)一个索引的话,速度很快。 当给A表的userid也加上索引时,速度又慢下来了。userid是比较重要的查询字段,所以不能删除。
好像userid的优先级高于 (type,add_time)能具体解释一下么?PS:关于结贴 我好久没上csdn了 呵呵。
tbl_name [[AS] alias] [index_hint_list]index_hint_list:
index_hint [, index_hint] ...index_hint:
USE {INDEX|KEY}
[{FOR {JOIN|ORDER BY|GROUP BY}] ([index_list])
| IGNORE {INDEX|KEY}
[{FOR {JOIN|ORDER BY|GROUP BY}] (index_list)
| FORCE {INDEX|KEY}
[{FOR {JOIN|ORDER BY|GROUP BY}] (index_list)
SELECT * FROM table1 USE INDEX (col1_index,col2_index)
WHERE col1=1 AND col2=2 AND col3=3;SELECT * FROM table1 IGNORE INDEX (col3_index)
WHERE col1=1 AND col2=2 AND col3=3;SELECT * FROM t1 USE INDEX FOR JOIN (i1) FORCE INDEX FOR JOIN (i2);
能不能够不强制呢?因为我代码里类似的情况好像挺多的,改动挺大。 能不能只通过修改索引来优化呢?这种问题在mysql是正常的么? 还是我设计上的问题?
show index from ..以供分析。
| id | select_type | table | type | key | key_len | ref | rows | Extra
| 1 | SIMPLE | b | ref | userid | 4 | const | 6630 | Using temporary;
| 1 | SIMPLE | a | ref | userid_2 | 10 | b.reuserid,const | 5 | Using where