刚来公司,被要求优化代码,基于SSH框架的系统,我一看Action里面全部写的是SQL语句,然后就是各种拼SQL的操作,最后还使用了Hibernate....
不吐槽了,已有一个功能优化完毕,加了个索引就可以了,还有一个功能语句比较复杂,甚至我加了索引查询速度更慢了,如何破,本人对数据库不是很精通,求叫下面这句由Hibernate生成的代码该如何优化
 select
        SUM(score0_.score) as col_0_0_,
        score0_.belong_month as col_1_0_ 
    from
        kpi.score score0_ 
    where
        score0_.ass_status=2 
        and score0_.belong_year=2016 
        and score0_.score_belong=8553 
        and score0_.link_type<>1 
        or score0_.link_type=1 
        and score0_.belong_year=2016 
        and score0_.score_belong=8553 
        or (
            score0_.score_belong in (
                select
                    user1_.uid 
                from
                    kpi.user user1_ 
                where
                    (
                        score0_.link_type in (
                            4 , 5 , 7 , 8
                        )
                    ) 
                    and (
                        score0_.dkid>0 
                        and user1_.dkid=-1 
                        and user1_.addid=8553 
                        or score0_.dkid=0 
                        and user1_.dkid=0 
                        and user1_.uid=8553
                    )

            )
        ) 
        and score0_.ass_status=2 
        and score0_.belong_year=2016 
    group by
        score0_.belong_month
我执行了EXPILAN ,结果为
id     select_type                                  table            type                            possible_keys  key            ken_len    ref        rows            Extra
1 PRIMARY                                 score0_     ALL                        score_belong                                      147818       Using where; Using temporary; Using filesort
2 DEPENDENT SUBQUERY user1_     unique_subquery PRIMARY     PRIMARY  4          func      1          Using where
然后执行PROFILE 发现
show profile cpu for query 132;
结果显示的查询时间都在preparing上,其中被CPU_user占用的又占用大部分,如何破?查询了很多地方都没有资料