【发布时间】:2025-12-22 06:35:10
【问题描述】:
我有一个查询需要更多时间来执行。我正在尝试优化它。 GROUP BY 中有一些函数可能会增加查询时间。所以我想弄清楚如何优化那部分。
我正在使用 Mysql 5.5.17。
我不能使用强制索引,因为我正在使用 Hibernate,所以这是不可能的。
我还有其他一些选择,例如:
1) 在 group by 中使用别名代替 concat(...)。
2) 或将 concat() 替换为其中的列。
例如:按 concat(1,2,3.) 分组 => 按 1,2,3 分组
在尝试了上述两个选项后,我看不出“查询成本”有什么不同
查询看起来像这样(表名已更改):
select sum(this_.a + this_.b + this_.c + this_.d + this_.e +
this_.f + this_.g + this_.h + this_.i + this_.j) as sent,
sum(this_.a + this_.b + this_.c + this_.d + this_.e) as delivered,
d2_.name as y2_, d2_.id as y3_, concat(cast(b4_.id as char),
'.',c1_.name,'.',cs5_.name,'_',date_format(b4_.createddate,
'%Y-%b-%d %H:%i:%s')
) as allias_concat, b4_.id as y5_,
this_.year as y6_, this_.month as y7_, this_.day as y8_,
this_.weekday as y9_, this_.weekofmonth as y10_,
this_.bltid as y11_,
b4_.id as y12_, c1_.name as y13_,
cs5_.name as y14_, b4_.createddate as y15_,
cs5_.subject as y16_, d2_.name as y17_
from TABLE1 this_
inner join TABLE2 c1_ on this_.cgnid=c1_.id
inner join TABLE3 b4_ on this_.bltid=b4_.id
inner join TABLE4 csc6_ on b4_.schdlid=csc6_.id
inner join TABLE5 cs5_ on this_.constid=cs5_.id
left outer join TABLE6 f3_ on this_.fid=f3_.id
inner join TABLE7 d2_ on this_.dptid=d2_.id
where (f3_.name<>'TRASH'
or c1_.fid=0
)
and c1_.status<>'K'
and d2_.id in (1)
and ((b4_.createddate between '2015-02-01 10:30:00'
AND '2015-05-13 10:29:59'
and csc6_.isrealtime = 'N'
)
or (csc6_.isrealtime = 'Y'
and this_.bltdate between '2015-02-01 10:30:00'
AND '2015-05-13 10:29:59')
)
group by d2_.id,
concat(cast(b4_.id as char),'.',c1_.name,'.',cs5_.name,'_',
date_format(b4_.createddate,'%Y-%b-%d %H:%i:%s')
),
b4_.id,
this_.year, this_.month, this_.day,
this_.bltid
limit 20001
请建议...
提前致谢...
【问题讨论】:
标签: mysql function group-by query-optimization