Union / Union All使查询变慢

时间:2016-03-18 12:03:19

标签: sql sql-server optimization union

我想优化此查询以在1秒内获取数据。目前它与联盟一起花费4-6秒。联盟全部需要大约4秒钟。使用Union获取数据大约需要6秒钟。请帮我快速查询。这是我的查询:

 SELECT  smsFDGPatientOrder.ID
          ,smsFDGPatientOrder.[Order]
          ,[ExamDate]
          ,[ArrivalTime]
          ,[Activity]
          ,[PatientFirstName]
          ,[PatientMiddleName]
          ,[PatientLastName]
          ,[DOB]
          ,[Order]
          ,[ActualExamTimeIn]
          ,[ActualExamTimeOut]
          ,[ActualScannerID]
          ,[ActualExamDate]
          ,[ActualCustomer]
          ,[ActualPatientFirstName]
          ,[ActualPatientLastName]
          ,[ActualDOB]
          ,[InsuranceCoID]
          ,[InsuranceID]
          ,[StartedInPreAuth]
          ,[DateReceived]
          ,[TimeReceived]
          ,[FDGPatientOrder]
          ,smsFDGPatientOrder.[StatusID] AS PatientorderStatusID
          ,smsFDGOrder.[StatusID] AS FDGorderStatusID
          --,smsFDGPatientOrder.[TimeAdded]
          ,smsFDGOrder.[TimeAdded]
          ,smsFDGPatientOrder.[Notes]
          ,smsFDGPatientOrder.[cntID]
          ,smsFDGPatientOrder.[empID]
          ,[Isotope]
          ,[Weight]
          ,[Diabetic]
          ,[Indication]
          ,[Procedure]
          ,[Physician]
          ,[InjectionTime]
          ,smsCustomers.custID
          ,effectChildren
    FROM VW_PatientOrderByMaxTimeAddedProc smsFDGPatientOrder 
    INNER JOIN [dbo].[VW_FGDOrderByMaxTimeAddedProc] AS smsFDGOrder ON smsFDGPatientOrder.[Order]=smsFDGOrder.ID  
    LEFT JOIN smscustomers ON smsFDGOrder.Customer=smscustomers.custID WHERE
    (
     smsFDGPatientOrder.StatusID IN (2,5,13,14, 15, 16, 17, 18, 19, 23,8,25,39)
    AND
    smsFDGOrder.StatusID  IN(8,12,23,25)
    )
    UNION ALL
    (

      SELECT 
          smsFDGPatientOrder.ID
          ,smsFDGPatientOrder.[Order]
          ,[ExamDate]
          ,[ArrivalTime]
          ,[Activity]
          ,[PatientFirstName]
          ,[PatientMiddleName]
          ,[PatientLastName]
          ,[DOB]
          ,[Order]
          ,[ActualExamTimeIn]
          ,[ActualExamTimeOut]
          ,[ActualScannerID]
          ,[ActualExamDate]
          ,[ActualCustomer]
          ,[ActualPatientFirstName]
          ,[ActualPatientLastName]
          ,[ActualDOB]
          ,[InsuranceCoID]
          ,[InsuranceID]
          ,[StartedInPreAuth]
          ,[DateReceived]
          ,[TimeReceived]
          ,[FDGPatientOrder]
          ,smsFDGPatientOrder.[StatusID] AS PatientorderStatusID
          ,smsFDGOrder.[StatusID] AS FDGorderStatusID
          --,smsFDGPatientOrder.[TimeAdded]
          ,smsFDGOrder.[TimeAdded]
          ,smsFDGPatientOrder.[Notes]
          ,smsFDGPatientOrder.[cntID]
          ,smsFDGPatientOrder.[empID]
          ,[Isotope]
          ,[Weight]
          ,[Diabetic]
          ,[Indication]
          ,[Procedure]
          ,[Physician]
          ,[InjectionTime]
          ,smsCustomers.custID
          ,effectChildren
    FROM VW_PatientOrderByMaxTimeAddedProc smsFDGPatientOrder 
    INNER JOIN [dbo].[VW_FGDOrderByMaxTimeAddedProc] AS smsFDGOrder ON smsFDGPatientOrder.[Order]=smsFDGOrder.ID  
    LEFT JOIN smscustomers ON smsFDGOrder.Customer=smscustomers.custID WHERE [order] IN
    (
    SELECT DISTINCT [order] FROM  VW_PatientOrderByMaxTimeAddedProc smsFDGPatientOrder INNER JOIN [dbo].[VW_FGDOrderByMaxTimeAddedProc] AS smsFDGOrder ON smsFDGPatientOrder.[Order]=smsFDGOrder.ID WHERE smsFDGPatientOrder.StatusID IN(3) AND smsFDGOrder.StatusID IN (8,12)
    EXCEPT
    SELECT DISTINCT [order] FROM  VW_PatientOrderByMaxTimeAddedProc smsFDGPatientOrder INNER JOIN [dbo].[VW_FGDOrderByMaxTimeAddedProc] AS smsFDGOrder ON smsFDGPatientOrder.[Order]=smsFDGOrder.ID WHERE smsFDGPatientOrder.StatusID  IN(select id FROM smsStatus WHERE ID<>3) AND smsFDGOrder.StatusID IN (8,12)
    )

    ) 

    UNION 

    SELECT 
    smsFDGPatientOrder.ID
    ,smsFDGPatientOrder.[Order]
          ,[ExamDate]
          ,[ArrivalTime]
          ,[Activity]
          ,[PatientFirstName]
          ,[PatientMiddleName]
          ,[PatientLastName]
          ,[DOB]
          ,[Order]
          ,[ActualExamTimeIn]
          ,[ActualExamTimeOut]
          ,[ActualScannerID]
          ,[ActualExamDate]
          ,[ActualCustomer]
          ,[ActualPatientFirstName]
          ,[ActualPatientLastName]
          ,[ActualDOB]
          ,[InsuranceCoID]
          ,[InsuranceID]
          ,[StartedInPreAuth]
          ,[DateReceived]
          ,[TimeReceived]
          ,[FDGPatientOrder]
          ,smsFDGPatientOrderStatus.[StatusID] AS PatientorderStatusID
          ,smsFDGOrder.[StatusID] AS FDGorderStatusID
          ,smsFDGPatientOrderStatus.[TimeAdded]
          ,smsFDGPatientOrderStatus.[Notes]
          ,smsFDGPatientOrderStatus.[cntID]
          ,smsFDGPatientOrderStatus.[empID]
          ,[Isotope]
          ,[Weight]
          ,[Diabetic]
          ,[Indication]
          ,[Procedure]
          ,[Physician]
          ,[InjectionTime]
          ,smsCustomers.custID
          ,effectChildren
      FROM smsFDGPatientOrder LEFT JOIN smsFDGPatientOrderStatus ON smsFDGPatientOrder.ID= smsFDGPatientOrderStatus.FDGPatientOrder 
     INNER JOIN [dbo].[VW_FGDOrderByMaxTimeAddedProc] smsFDGOrder ON smsFDGPatientOrder.[Order]=smsFDGOrder.ID LEFT JOIN smscustomers ON smsFDGOrder.Customer=smscustomers.custID 
     WHERE smsFDGPatientOrderStatus.StatusID IS NULL AND  FDGPatientOrder IS NULL  AND smsFDGOrder.StatusID IN (8,12)  AND effectChildren=1 order by ID asc

1 个答案:

答案 0 :(得分:-4)

在每个表名之后使用(NoLock),如

Select * FROM smsFDGPatientOrder With(NoLock)

备选您可以在列上构建非聚集索引,这些列在连接中使用或在许多查询中选择。我们创建了索引和查询,为我们提供了更快的结果。