67,549
社区成员




发下执行计划看看,给你优化下。还有结果一共多少条?
[quote=引用 11 楼 l568646976 的回复:] 就是7楼这个想法,但这样查询太慢了。
就是7楼这个想法,但这样查询太慢了。
[quote=引用 8 楼 huxiweng 的回复:] 需求我看的有点乱。你最好能举个简单的列子说清楚需求 比如 A表字段 t1,t2,t3,t4 B表字段 t1,t2,t3,t4 期望的结果是怎样?
[quote=引用 5 楼 l568646976 的回复:] 我怕大家搞混才这样写的,其实是这样的,t5其实就是t4,countAA是group by字段t1、t2、t3、t4,条件为t4是success、wait值
select a.a, aa.aa, b.b, a.t1, a.t2, a.t3, a.t4
from (select count(1) a, t1, t2, t3, t4 from A group by t1, t2, t3, t4) a
left join (select count(1) aa, t1, t2, t3, t4
from A where t4 = 'success' group by t1, t2, t3, t4) aa
on a.t1 = aa.t1
and a.t2 = aa.t2
and a.t3 = aa.t3
and a.t4 = aa.t4
left join (select count(1) b, t1, t2, t3, t4
from B group by t1, t2, t3, t4) b
on b.t1 = aa.t1
and b.t2 = aa.t2
and b.t3 = aa.t3
and b.t4 = aa.t4
[/quote]
需求我看的有点乱。你最好能举个简单的列子说清楚需求 比如 A表字段 t1,t2,t3,t4 B表字段 t1,t2,t3,t4 期望的结果是怎样?
需求我看的有点乱。你最好能举个简单的列子说清楚需求 比如 A表字段 t1,t2,t3,t4 B表字段 t1,t2,t3,t4 期望的结果是怎样?
我怕大家搞混才这样写的,其实是这样的,t5其实就是t4,countAA是group by字段t1、t2、t3、t4,条件为t4是success、wait值
select a.a, aa.aa, b.b, a.t1, a.t2, a.t3, a.t4
from (select count(1) a, t1, t2, t3, t4 from A group by t1, t2, t3, t4) a
left join (select count(1) aa, t1, t2, t3, t4
from A where t4 = 'success' group by t1, t2, t3, t4) aa
on a.t1 = aa.t1
and a.t2 = aa.t2
and a.t3 = aa.t3
and a.t4 = aa.t4
left join (select count(1) b, t1, t2, t3, t4
from B group by t1, t2, t3, t4) b
on b.t1 = aa.t1
and b.t2 = aa.t2
and b.t3 = aa.t3
and b.t4 = aa.t4
[quote=引用 楼主 l568646976 的回复:] 最后还要统计A表字段t5为success、wait状态的分组得到countAA,即:同A表一样先进行(t1、t2、t3、t4)分组统计,条件为t5字段的值为success或wait。结果集为:(countAA、t1、t2、t3、t4)。
最后还要统计A表字段t5为success、wait状态的分组得到countAA,即:同A表一样先进行(t1、t2、t3、t4)分组统计,条件为t5字段的值为success或wait。结果集为:(countAA、t1、t2、t3、t4)。
好难的样子,坐等高人
教你个笨办法,把sql从小范围向大范围查哪里慢。 如:先试select * from (select * from (select * from a) a left join (select * from b) b on a.a = b.b) c where c.c = 1
再试select * from a
再试select * from b
到哪一步突然慢特别慢,这里就是效率瓶颈,想办法优化。在经验不丰富的时候,可以使用这个简单方法能查出来大部分问题。select * from (select * from a) a left join (select * from b) b on a.a = b.b