我有多个 sql 查询,我想将它们捆绑到一个查询中,这样我就可以避免从我的应用程序向数据库发送多个请求(我想一次性接收所有这些数据):
1) select pin, officeNum, isVeteran from table18 where pin = 123;
2) select streetAddress, apartmentAddress, cityAddress, stateAddress from table1 where case = (select case from table18 where pin = 123);
3) select unitAddress, cityAddress, streetAddress, apartmentAddress from table5 where pin = 123;
4) select unitAddress, cityAddress, streetAddress, apartmentAddress from table55 where seqNum = 0 and rfa = (select rfa from table18 where pin = 123);
5) select unitAddress, cityAddress, streetAddress, apartmentAddress from table103 where histCode = 0 and case = (select case from table18 where pin = 123);
6) select phone, email from table715 where histSeqNum in (select max(histSeqNum from table715))
and histCode in (select max(histCode) from table715)
and case = (select case from table18 where pin = 123);
Run Code Online (Sandbox Code Playgroud)
这是我的架构:
(请原谅不好的设计,它来自20年前创建的数据库,没有外键)
-Table18(引脚(PK)、案例、officeNum、isVeteran)
-Table1(案例(PK)、案例官员、街道地址、公寓地址、城市地址、州地址)
-Table5(引脚(PK)、街道地址、公寓地址、城市地址、州地址)
-Table55(rfa(复合键),seqNum(复合键),rfa地址,街道地址,公寓地址,城市地址,州地址)
-Table103(案例(CompositeKey),histCode(CompositeKey))
-Table715(案例(CompositeKey)、histSeqNum(CompositeKey)、histCode(CompositeKey)、电话、电子邮件)
这是一个可以合并的集合... (3,4,5)
select unitAddress, cityAddress, streetAddress, apartmentAddress
from table5 where pin = 123
union
select unitAddress, cityAddress, streetAddress, apartmentAddress
from table55 where seqNum = 0 and rfa = (select rfa from table18 where pin = 123)
union
select unitAddress, cityAddress, streetAddress, apartmentAddress
from table103 where histCode = 0 and case = (select case from table18 where pin = 123);
Run Code Online (Sandbox Code Playgroud)
如果你不介意这些上的空白状态,你也可以把(2)放在那里......
例如,在 2 中写下如下内容:
select null unitAddress, streetAddress, apartmentAddress, cityAddress, stateAddress
from table1
where case = (select case from table18 where pin = 123);
union
select unitAddress, cityAddress, streetAddress, apartmentAddress, null
from table5 where pin = 123
union
select unitAddress, cityAddress, streetAddress, apartmentAddress, null
from table55 where seqNum = 0 and rfa = (select rfa from table18 where pin = 123)
union
select unitAddress, cityAddress, streetAddress, apartmentAddress, null
from table103 where histCode = 0 and case = (select case from table18 where pin = 123);
Run Code Online (Sandbox Code Playgroud)
还考虑将嵌套选择重组为连接 - 类似于此:(我打赌优化这些查询将是您正在寻找的性能差异)
select streetAddress, apartmentAddress, cityAddress, stateAddress
from table1 t1, table18 t18
where t1.case = t18.case
and t18.123;
Run Code Online (Sandbox Code Playgroud)
然后确保 t18 在引脚上有索引,并且 t1 在外壳上有索引。
| 归档时间: |
|
| 查看次数: |
54509 次 |
| 最近记录: |