RDBMS-MS SQL
如何将这两个SQL选择查询合并为一个并快速执行它们:
select
VVO.VV_CODE,
V.Vessel_name,
VVO.Arrival_date,
isnull(IGM.VIR_NO,'NULL') as VIR_NO,
isnull(VVO.TERMINAL_CODE,'NULL') as TERMINAL_CODE
from Vessel_voyage VVO, Vessel V, IGM
where V.Vessel_code = substring(VVO.VV_CODE,1,3) and VVO.VV_CODE = IGM.VV_CODE
select
BLD.BL_NO,
isnull(BLD.Parent_BL,'NULL') as Parent_BL,
BLD.Consignee_Description,
DO.DO_Issue_Date,
CA.CAgent_Name,
BLC.Container_No ,
CS.Container_Size_Description
from BL_DATA BLD, CAgent CA, Delivery_Order DO, BL_Container BLC, Container_Size CS
where BLD.BL_NO = DO.BL_NO and DO.CAgent_Code = CA.CAgent_Code and BLD.BL_NO = BLC.BL_NO and BLC.Container_Size_Code = CS.Container_Size_Code
分别执行这些选择查询,它们将在几秒钟内被执行。
但是将它们变成单个选择查询,它们大约需要30到40分钟才能被执行。
这是我尝试过的:
select
VVO.VV_CODE,
V.Vessel_name,
VVO.Arrival_date,
isnull(IGM.VIR_NO,'NULL') as VIR_NO,
isnull(VVO.TERMINAL_CODE,'NULL') as TERMINAL_CODE,
BLD.BL_NO,
isnull(BLD.Parent_BL,'NULL') as Parent_BL,
BLD.Consignee_Description,
DO.DO_Issue_Date,
CA.CAgent_Name,
BLC.Container_No ,
CS.Container_Size_Description
from Vessel_voyage VVO, Vessel V, IGM ,BL_DATA BLD, CAgent CA, Delivery_Order DO, BL_Container BLC, Container_Size CS
where V.Vessel_code = substring(VVO.VV_CODE,1,3) and VVO.VV_CODE = IGM.VV_CODE and BLD.BL_NO = DO.BL_NO and DO.CAgent_Code = CA.CAgent_Code and BLD.BL_NO = BLC.BL_NO and BLC.Container_Size_Code = CS.Container_Size_Code
答案 0 :(得分:2)
使用ANSI样式联接编写查询将使其更易于阅读-因此我已经做到了。它还有助于发现联接逻辑中存在问题的地方。
重新编写我得到的联接:
from Vessel_voyage VVO
inner join Vessel V on V.Vessel_code = substring(VVO.VV_CODE,1,3)
inner join IGM on VVO.VV_CODE = IGM.VV_CODE
inner join BL_DATA BLD on BLD.BL_NO = DO.BL_NO
inner join CAgent CA
inner join Delivery_Order DO on DO.CAgent_Code = CA.CAgent_Code
inner join BL_Container BLC on BLD.BL_NO = BLC.BL_NO
inner join Container_Size CS on BLC.Container_Size_Code = CS.Container_Size_Code
我可以绕着DO到CA联接谓词移动,但最终我有8个表联接,但是只有6个谓词联接它们-最终结果是,其中一个表是笛卡尔运算,可能会给出错误的结果,但肯定会导致性能下降。
如果您使用这种连接方式,我怀疑您将能够很轻松地对其进行修复。