+ -
当前位置:首页 → 问答吧 → 【求助】关于MYSQL关联索引的走向问题及优化方案

【求助】关于MYSQL关联索引的走向问题及优化方案

时间:2011-11-30

来源:互联网

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会慢到卡死?应该如何改进?

作者: scorpio1   发布时间: 2011-11-30

create index x on A (type,add_time);

作者: ACMAIN_CHM   发布时间: 2011-11-30

scorpio1 
  '截至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.html

8、如何给分和结贴?
http://community.csdn.net/Help/HelpCenter.htm#结帖


作者: ACMAIN_CHM   发布时间: 2011-11-30