刚来公司,被要求优化代码,基于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占用的又占用大部分,如何破?查询了很多地方都没有资料
不吐槽了,已有一个功能优化完毕,加了个索引就可以了,还有一个功能语句比较复杂,甚至我加了索引查询速度更慢了,如何破,本人对数据库不是很精通,求叫下面这句由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占用的又占用大部分,如何破?查询了很多地方都没有资料
解决方案 »
- 我使用sql语句修改了表格里的数据时忘记写条件啦,结果整个表的数据都改了,怎么挽回
- 紧急求助:MYSQL数据库表里只能看到一条不全记录
- 查询Incorrect database name??
- 提个问题,mysql下查询每个类别前2条记录
- 来交学费的
- 急急急,, group by
- AS4 MySQL RPM 安装,在init.d中的 mysql.server为啥不能在AS4重新启动的时候启动mysql?
- 有关pgaccess的问题,大家帮个忙
- mysql如何查询数据中每周的数据,有很多周,需要把每周的数据分别查询
- 关于mysql一个存储过程耗时很长的问题,求优化思路
- RedHat5.9下安装MySql 5.7 步骤
- 求帮忙!这个sql语句该咋写
基本查询时间都被那项preparing占用了