MS SQL查询所有表太慢了

时间:2016-11-10 16:30:37

标签: sql sql-server performance left-join

我正在查询我的查询问题。我在MS SQL中,我试图从我的病人那里得到所有相关的表格。从这里开始,一切都很顺利。我先构建我的查询然后执行它。当我执行它时,完成需要2分钟以上。这太长了我在我的桌子上都有索引。这是我试图实现的目标:

这是我的查询构建:

  select *  from (          
      select (STUFF((
    SELECT '  ' + AA.LEFTJOIN 
    FROM (Select 'LEFT OUTER JOIN ' + A.name + ' WITH (NOLOCK) on (tbpatient.Id = ' + (A.name + '.' + B.Name) + ')' AS LEFTJOIN
             from sysobjects A Join SysColumns B on (A.id = B.id) 
            Where B.name in ('IdPatient','ImageIdPatient')  
            And not A.name in ('tbrv','tbPatient','tbRV_ToExport','tbRV_Archive','tbRV_LOG_Archives','tbPatients_ToExport',
            'tbPatientLock','tbPatient_FusionToExport','tbRV_LOG','tbPatient_A1','tbPatient_A2','tbPatient_A3','tbPatient_QVCDetail') 
            And a.type = 'U' And a.Name LIKE 'tb%') AA
    FOR XML PATH('')
    ), 1, 2, '')
) AS query
union all
 select (STUFF((
    SELECT '  ' + AA.WHERECLAUSE 
    FROM (Select  ' And ' + '(' + A.name + '.' + B.Name + ')  IS NULL '   AS WHERECLAUSE  from sysobjects A Join SysColumns B on (A.id = B.id) 
            Where B.name in ('IdPatient','ImageIdPatient')  
            And not A.name in ('tbrv','tbPatient','tbRV_ToExport','tbRV_Archive','tbRV_LOG_Archives','tbPatients_ToExport',
            'tbPatientLock','tbPatient_FusionToExport','tbRV_LOG','tbPatient_A1','tbPatient_A2','tbPatient_A3','tbPatient_QVCDetail') 
            And a.type = 'U' And a.Name LIKE 'tb%') AA
    FOR XML PATH('')
    ), 1, 2, '')
) AS query ) as AA

它正在回归:

我首先在我的代码中添加它:

Select  tbPatient.Id, tbPatient.adresse1, tbPatient.nom, tbPatient.prenom From tbPatient " & _
                                         "Left Join tbRV A On (A.Idpatient = tbPatient.ID and A.DateRV >= GETDATE()) " & _
                                         "Left Join tbRV B On ( B.Idpatient = tbPatient.ID and  B.DateRV <= GETDATE() And B.Cloturer = 1) 
  

 left join tbPatientLettres WITH (NOLOCK) on (tbpatient.Id = tbPatientLettres.IDPatient)  
left join tbPatientprofessionnel WITH (NOLOCK) on (tbpatient.Id = tbPatientprofessionnel.IDPatient)  
left join tbDossierPatient WITH (NOLOCK) on (tbpatient.Id = tbDossierPatient.IDPatient)  
left join tbPatient_CarnetVacXML WITH (NOLOCK) on (tbpatient.Id = tbPatient_CarnetVacXML.IDPatient)  
left join tbPatientDocuments WITH (NOLOCK) on (tbpatient.Id = tbPatientDocuments.IDPatient)  
left join tbPatientAttachements WITH (NOLOCK) on (tbpatient.Id = tbPatientAttachements.IDPatient)  
left join tbPatientMedecins WITH (NOLOCK) on (tbpatient.Id = tbPatientMedecins.IDPatient)  
left join tbNote WITH (NOLOCK) on (tbpatient.Id = tbNote.IdPatient)  
left join tbDossierIntervention WITH (NOLOCK) on (tbpatient.Id = tbDossierIntervention.IDPatient)  
left join tbPatientTaxe WITH (NOLOCK) on (tbpatient.Id = tbPatientTaxe.IdPatient)  
left join tbPatientModeleTaxe WITH (NOLOCK) on (tbpatient.Id = tbPatientModeleTaxe.IdPatient)  
left join tbPTI_Constat_Note_Evolution WITH (NOLOCK) on (tbpatient.Id = tbPTI_Constat_Note_Evolution.IDPatient)  
left join tbPTI_Constat_Eval WITH (NOLOCK) on (tbpatient.Id = tbPTI_Constat_Eval.IDPatient)  
left join tbPatient_OrdonnanceEntete WITH (NOLOCK) on (tbpatient.Id = tbPatient_OrdonnanceEntete.IDPatient)  
left join tbPatient_CarnetVac WITH (NOLOCK) on (tbpatient.Id = tbPatient_CarnetVac.IDPatient)  
left join tbPTIConstatSuivie WITH (NOLOCK) on (tbpatient.Id = tbPTIConstatSuivie.IDPatient)  
left join tbRNIComprimeEntete WITH (NOLOCK) on (tbpatient.Id = tbRNIComprimeEntete.IDPatient)  
left join tbPatient_QVCEntete WITH (NOLOCK) on (tbpatient.Id = tbPatient_QVCEntete.IDPatient)  
left join tbRNITeneursPatient WITH (NOLOCK) on (tbpatient.Id = tbRNITeneursPatient.IDPatient)  
left join tbPatient_QVCInfirmiere WITH (NOLOCK) on (tbpatient.Id = tbPatient_QVCInfirmiere.IDPatient)  
left join tbRNIResultat WITH (NOLOCK) on (tbpatient.Id = tbRNIResultat.IDPatient)  
left join tbPatient_QVCDestination WITH (NOLOCK) on (tbpatient.Id = tbPatient_QVCDestination.IDPatient)  
left join tbPatientRNI WITH (NOLOCK) on (tbpatient.Id = tbPatientRNI.IDPatient)  
left join tbPatient_CarnetVacRemarque WITH (NOLOCK) on (tbpatient.Id = tbPatient_CarnetVacRemarque.IDPatient)  
left join tbPatientImages WITH (NOLOCK) on (tbpatient.Id = tbPatientImages.ImageIDPatient)  
left join tbRVObjetQuestionnaire WITH (NOLOCK) on (tbpatient.Id = tbRVObjetQuestionnaire.IDPatient)  
left join tbPTI_Suivie_CLinique WITH (NOLOCK) on (tbpatient.Id = tbPTI_Suivie_CLinique.IDPatient)  
left join tbPatient_A4 WITH (NOLOCK) on (tbpatient.Id = tbPatient_A4.IDPatient)  
left join tbPatientRNIProtocole WITH (NOLOCK) on (tbpatient.Id = tbPatientRNIProtocole.IDPatient)  
left join tbPatient_D1 WITH (NOLOCK) on (tbpatient.Id = tbPatient_D1.IDPatient)  
left join tbPatient_C1 WITH (NOLOCK) on (tbpatient.Id = tbPatient_C1.IDPatient)  
left join tbPatientAssurance WITH (NOLOCK) on (tbpatient.Id = tbPatientAssurance.IdPatient)  
left join tbPatientContacts WITH (NOLOCK) on (tbpatient.Id = tbPatientContacts.IDPatient)  
left join tbPatient_B1 WITH (NOLOCK) on (tbpatient.Id = tbPatient_B1.IDPatient)

我在我的vb代码中添加了这个子句:where tbpatient.idstatut = 1 And A.Id is NULL And B.Id is NULL

And (tbPatientLettres.IDPatient)  IS NULL    
And (tbPatientprofessionnel.IDPatient)  IS NULL    
And (tbDossierPatient.IDPatient)  IS NULL    
And (tbPatient_CarnetVacXML.IDPatient)  IS NULL    
And (tbPatientDocuments.IDPatient)  IS NULL    
And (tbPatientAttachements.IDPatient)  IS NULL    
And (tbPatientMedecins.IDPatient)  IS NULL    
And (tbNote.IdPatient)  IS NULL    
And (tbDossierIntervention.IDPatient)  IS NULL    
And (tbPatientTaxe.IdPatient)  IS NULL    
And (tbPatientModeleTaxe.IdPatient)  IS NULL    
And (tbPTI_Constat_Note_Evolution.IDPatient)  IS NULL    
And (tbPTI_Constat_Eval.IDPatient)  IS NULL    
And (tbPatient_OrdonnanceEntete.IDPatient)  IS NULL    
And (tbPatient_CarnetVac.IDPatient)  IS NULL    
And (tbPTIConstatSuivie.IDPatient)  IS NULL    
And (tbRNIComprimeEntete.IDPatient)  IS NULL    
And (tbPatient_QVCEntete.IDPatient)  IS NULL    
And (tbRNITeneursPatient.IDPatient)  IS NULL    
And (tbPatient_QVCInfirmiere.IDPatient)  IS NULL    
And (tbRNIResultat.IDPatient)  IS NULL    
And (tbPatient_QVCDestination.IDPatient)  IS NULL    
And (tbPatientRNI.IDPatient)  IS NULL    
And (tbPatient_CarnetVacRemarque.IDPatient)  IS NULL    
And (tbPatientImages.ImageIDPatient)  IS NULL    
And (tbRVObjetQuestionnaire.IDPatient)  IS NULL    
And (tbPTI_Suivie_CLinique.IDPatient)  IS NULL    
And (tbPatient_A4.IDPatient)  IS NULL    
And (tbPatientRNIProtocole.IDPatient)  IS NULL    
And (tbPatient_D1.IDPatient)  IS NULL    
And (tbPatient_C1.IDPatient)  IS NULL    
And (tbPatientAssurance.IdPatient)  IS NULL    
And (tbPatientContacts.IDPatient)  IS NULL    
And (tbPatient_B1.IDPatient)  IS NULL 

然后在那之后,当我连接两个字段并执行我的查询时,它需要超过2分钟...有人知道如何帮助吗?谢谢,抱歉这篇长篇文章。

1 个答案:

答案 0 :(得分:2)

我认为您可以使用以下代码简化操作,以便让未到您诊所的患者,我认为它可能会更好:

SELECT Id
FROM tbpatient
EXCEPT
(SELECT IDPatient
FROM tbPatientLettres
UNION
SELECT IDPatient
FROM tbPatientprofessionnel
UNION
SELECT IDPatient
FROM tbDossierPatient
... and so on until you get all your tables in here
)

此外,如果您有任何或某些IDPatient列没有非聚集索引,我应该更快。