SQL 优化,2个数据库,几个表之间查询数据关联查询速度比较慢,如何优化?
withtas(SELECT[CableId],[InspectionTime],[Machineid],Process=casewhenProcess='Foaming...
with t as (
SELECT [CableId],[InspectionTime],[Machineid],
Process=case when Process='Foaming' then '发泡'
when Process='WeldingAndCorrugating' then '轧纹' else '护套' end , [UserId],[InspectionState]
FROM [zhrf].[dbo].[pro_ProductDataByClass] where ProductDate> DATEADD(mm,-1,GETDATE()) AND InspectionState='是'
) ,
ts as (
SELECT distinct CableId AS Bh FROM zhrf.zhrf.Pro_FoamingTestRecordData
WHERE Date> DATEADD(mm,-2,GETDATE())
union all
SELECT distinct [Bh] FROM [E5070B].[dbo].[BCPJC]
WHERE JYRQ> DATEADD(mm,-2,GETDATE())
union all
SELECT distinct [Bh] FROM [E5070B].[dbo].[Jc]
where CableType='馈线' and JYRQ> DATEADD(mm,-2,GETDATE())
)
select [CableId],[InspectionTime],[Machineid],
Process ,[UserId],[InspectionState] from t where not exists(select Bh from ts where ts.Bh=t.CableId ) 现在需要20来秒, 第一个部分和第二个部分 独立查询都不到1秒, 但是合并关联查询后就很慢, 第一部分查询出有3500,第二个35000,数据比较少的, 求大神优化 展开
SELECT [CableId],[InspectionTime],[Machineid],
Process=case when Process='Foaming' then '发泡'
when Process='WeldingAndCorrugating' then '轧纹' else '护套' end , [UserId],[InspectionState]
FROM [zhrf].[dbo].[pro_ProductDataByClass] where ProductDate> DATEADD(mm,-1,GETDATE()) AND InspectionState='是'
) ,
ts as (
SELECT distinct CableId AS Bh FROM zhrf.zhrf.Pro_FoamingTestRecordData
WHERE Date> DATEADD(mm,-2,GETDATE())
union all
SELECT distinct [Bh] FROM [E5070B].[dbo].[BCPJC]
WHERE JYRQ> DATEADD(mm,-2,GETDATE())
union all
SELECT distinct [Bh] FROM [E5070B].[dbo].[Jc]
where CableType='馈线' and JYRQ> DATEADD(mm,-2,GETDATE())
)
select [CableId],[InspectionTime],[Machineid],
Process ,[UserId],[InspectionState] from t where not exists(select Bh from ts where ts.Bh=t.CableId ) 现在需要20来秒, 第一个部分和第二个部分 独立查询都不到1秒, 但是合并关联查询后就很慢, 第一部分查询出有3500,第二个35000,数据比较少的, 求大神优化 展开
若以下回答无法解决问题,邀请你更新回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询