例子如下:A B 表结构一样 ,实际不止两张表,也许5张10张这样的。
1.
select (
(select count(1) from A as a where a.xxx=xxx left join C as c on a.xxx=c.xxx)+(select count(1) from A as b where b.xxx=aa left join C as c on b.xxx=c.xxx)
)as total
发现这样表多了,很耗时。
2.
select count(1)
from
(
select xxx from A as a where a.xxx=xxxx
union all
select xxx from B as b where b.xxx=xxxx
)as t
left join C as c on t.xxx=c.xxx
where
c.id=xxx
求高手,求dba.
select sum(qty) from (select count(1) as qty from a union all select count(1) as qty from b)
这个一样,也是耗时的。
@apgk: 耗时要看具体情况,你这个 where条件下的字段有索引吗?这个要排查具体问题的,不是光看sql语句
@jqw2009: 就这样吧,去除一些时间 条件,加入索引,目前12张表,1.4s能完成,感谢回复。