select distinct n.id,n.headLine from news n
inner join stocknewsrelation snr on snr.news_id=n.id
inner join stock s on snr.stock_id=s.id
inner join stockindexrelation sir on sir.stock_id=s.id
inner join indexinfo i on sir.index_id=i.id
where i.indexCode='HSI' and n.newsUpdateTime >= '2008-8-10'
order by n.newsUpdateTime desc limit 5;相关的表有news,stock,indexinfo,stocknewsrelation,stockindexrelation五张表.
外键都建立了索引,news的newsUpdateTime也建立了索引.
但是查询速度执行的却很慢,后辍后relation的表是连接表.
news到stock到indexinfo,都是多对多的关系.本以来是索引没建好,可是select id from news order by newsUpdateTime desc limit 5;
速度很快14ms结束.
news下大概有8W条数据.
数据库是MySQL5.0,不知道那位能帮着找一下原因?

解决方案 »

  1.   

    explain看看瓶颈在什么地方,TRY:
    select distinct n.id,n.headLine from news n
    inner join stocknewsrelation snr on (snr.news_id=n.id) and (n.newsUpdateTime >= '2008-8-10')
    inner join stock s on snr.stock_id=s.id
    inner join stockindexrelation sir on sir.stock_id=s.id
    inner join indexinfo i on (sir.index_id=i.id) and (i.indexCode='HSI')
    order by n.newsUpdateTime desc limit 5; 
      

  2.   

    看到此问题忽然想到一个问题,借用楼主的地方。如果用了别名,索引及explain检索的相关字段会不会受影响,看成无索引结果集。
      

  3.   

    索引是建立了.但是还要考虑个问题,写的where条件.具体还要看数据库是不是使用你建立的索引
      

  4.   

    在news这张表上我建立了个多列的索引newsKeyws (newsUpdateTime,keyword_id,id).
    可是explain后只使用了主键索引,并没有使用我定义的索引.
      

  5.   

    根据2楼的语句, explain的结果是:select_type|table|type|possible_keys|key|key_len|ref|rows|ExtraSIMPLE|i|ref|PRIMARY,indexCode|indexCode|768|const|1|Using where; Using index; Using temporary; Using filesortSIMPLE|sir|ref|PRIMARY,FKF3181788D5499B0,FKF318178C3CB64EC|FKF318178C3CB64EC|386|etnetchina.i.id|20|Using indexSIMPLE|s|ref|PRIMARY|PRIMARY|386|etnetchina.sir.stock_id|2|Using indexSIMPLE|snr|ref|PRIMARY,FK941DC02529C11B63,FK941DC0258D5499B0|FK941DC0258D5499B0|386|etnetchina.s.id|10|Using where; Using 
    indexSIMPLE|n|ref|PRIMARY,newskeys,newsIdkeys|PRIMARY|386|etnetchina.snr.news_id|1|Using where麻烦看一下问题出在那里,调试了好久.