SELECTuser.nickname,visitors.week_likesFROMuserJOINvisitorsON(user.id=visitors.user_id)WHERE(user.role>=0ANDvisitors.week_num=10ANDvisitors.week_likes>0AND(user.show_index!=-1ORuser.show_indexISNULL))ORDERBYvisitors.week_likesDESC,user.last_seenDESCLIMIT100user.id和visitors.id是主键,user.id与visitors.user_id对应。对visitors.user_id建立了索引,没什么效果,在SQL语句和索引方面能怎样优化?由于week_num和week_likes的区分度很低(<0.001),所以我觉得建立索引没有太大的效果,但是,索引user_id换成(week_num,week_likes)后visitors的rows由9000+变为1700+了,至少行数来说还是很明显的(虽然查询时间上没有太大差别)那先筛选user和visitor后再join会不会更好呢?SELECTa.nickname,b.week_likesFROM(SELECT*FROMuserWHERE(user.role>=0AND(user.show_index!=-1ORuser.show_indexISNULL))aJOIN(SELECTvisitors.user_id,visitors.week_likesFROMvisitorsWHERE(visitors.week_num=10ANDvisitors.week_likes>0))bON(a.id=b.user_id)ORDERBYb.week_likesDESC,a.last_seenDESCLIMIT100;这句报错,不知道原因在哪?还有其他什么方面可以优化的吗?
蛊毒传说
相关分类