首页 新闻 会员 周边

mssql 涉及多张表名不同其余一样的表条件查询,如何快速获取过滤后的总数?

0
悬赏园豆:30 [已解决问题] 解决于 2019-09-05 08:54

例子如下: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.

< >
分享
最佳答案
0

select sum(qty) from (select count(1) as qty from a union all select count(1) as qty from b)

收获园豆:30
jqw2009 | 老鸟四级 |园豆:2439 | 2019-09-04 15:20

这个一样,也是耗时的。

apgk | 园豆:47 (初学一级) | 2019-09-04 15:21

@apgk: 耗时要看具体情况,你这个 where条件下的字段有索引吗?这个要排查具体问题的,不是光看sql语句

jqw2009 | 园豆:2439 (老鸟四级) | 2019-09-04 16:34

@jqw2009: 就这样吧,去除一些时间 条件,加入索引,目前12张表,1.4s能完成,感谢回复。

apgk | 园豆:47 (初学一级) | 2019-09-05 08:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册