2016-08-18 79 views
2

有一個關於查詢性能的簡短問題。 最近幾天,我一直在努力提高查詢性能。 在MS Access中,我有一個沒有任何條件(約15個連接)的查詢,它返回了〜50列(id,name等)和〜1200條記錄(所有記錄)。此查詢的執行時間約爲15分鐘。 但是,當我添加一個簡單的標準:在哪裏ID < 10000(或任何其他數字大於我的最後一個ID)(我的最大ID是1139)所有查詢在眨眼間執行。 有人可以解釋爲什麼這樣簡單的條件極大地加快了我的查詢嗎?在MS Access查詢中的性能

下面是查詢(由SQLView訪問生成):

SELECT MasterTableT.ID 
,[General:CompanyProgrammeNameT].[Company Programme Abbreviation] AS [Company Programme] 
,[General:BlockNameT].[Block Name] 
,[General:Position/JobTitleT].[Position/Job Title] 
,[General:DeliveryStreamT].[Delivery Stream] 
,[General:AgileTeamT].[Agile Team name] 
,MasterTableT.[Modification Date] 
,[General:ProgrammePeopleT].[Full Name] AS [Functional Manager] 
,MasterTableT.[Expected: StartDate] 
,MasterTableT.[Expected: EndDate] 
,DateDiff("m", [MasterTableT] ! [Expected: StartDate], [MasterTableT] ! [Expected: EndDate]) AS [Expected Duration of involvement (months)] 
,[General:LocationsT].Locations AS [Expected location] 
,[General:TravelT].Travel AS [Expected Travel] 
,[General:ProfileTypeT].[Profile type] AS [Expected Profile Type] 
,[General:HireTypeT].[Hire Type] AS [Expected Type of Hire] 
,[General:BA/GF and UnitT].[BA/GF and Unit Name] AS [If allocated, coming from BA/GF] 
,MasterTableT.[Request Comments] 
,[General:RecruitmentStatusT].[Recruitment Status] 
,MasterTableT.[Employee Name] 
,MasterTableT.[Employee User ID Number] 
,MasterTableT.[Employee SOFT ID] 
,[General:LocationsT_1].Locations AS [Actual Location] 
,MasterTableT.[Actual: Allocation (%)] 
,MasterTableT.[Actual: Start date in the Programme] 
,MasterTableT.[Agreed: End Date in the programme] 
,DateDiff("m", MasterTableT ! [Actual: Start Date in the programme], MasterTableT ! [Agreed: End Date in the programme]) AS [Time remaining in the Programme] 
,[General:ProfileTypeT_1].[Profile type] AS [Actual Profile Type] 
,[General:HireTypeT_1].[Hire Type] AS [Actual Type of Hire] 
,[General:BA/GF and UnitT_1].[BA/GF and Unit Name] AS [Actual BA/GF and Unit] 
,[General:ProgrammePeopleT_1].[Full Name] AS [Recruitment Driver] 
,[General:ProgrammePeopleT_2].[Full Name] AS [Recruitment Manager] 
,[General:ProgrammePeopleT_2].[Company ID] AS [Recruitment Manager ID] 
,[General:ProgrammePeopleT_2].[Cost Center] AS [Recruitment Manager Cost Center] 
,MasterTableT.[HR Comments] 
,MasterTableT.[NIIDS number] 
,IIf(IsNull([Actual: Start date in the Programme]), [Expected: StartDate], [Actual: Start date in the Programme]) AS [Expected/Actual StartDate] 
,IIf(IsNull([Agreed: End Date in the programme]), [Expected: EndDate], [Agreed: End Date in the programme]) AS [Expected/Agreed End Date] 
,MasterTableT.[Expected Allocation %] 
,MasterTableT.[Training: GSU Intro] 
,[General:PmoOrgLevel3].OrgL3Name 
,[General:PmoOrgLevel4].OrgL4Name 
,MasterTableT.[PMO: Org Level 5] 
,MasterTableT.[Prioritized Recruitment] 
FROM (
(
    (
     (
      (
       (
        (
         (
          (
           (
            (
             (
              (
               (
                (
                 (
                  (
                   (
                    (
                      MasterTableT LEFT JOIN [General:AgileTeamT] ON MasterTableT.[Team Name ID] = [General:AgileTeamT].[ID Agile Team] 
                     ) LEFT JOIN [General:BA/GF and UnitT] ON MasterTableT.[Coming from: BA/GF/External and unit ID] = [General:BA/GF and UnitT].[ID BA/GF and Unit] 
                    ) LEFT JOIN [General:BlockNameT] ON MasterTableT.[Block Name ID] = [General:BlockNameT].[ID Block] 
                   ) LEFT JOIN [General:DeliveryStreamT] ON MasterTableT.[Delivery Stream ID] = [General:DeliveryStreamT].[ID Delivery Stream] 
                  ) LEFT JOIN [General:TravelT] ON MasterTableT.[Expected: Travel ID] = [General:TravelT].[ID Travel] 
                 ) LEFT JOIN [General:HireTypeT] ON MasterTableT.[Expected: Hire Type ID] = [General:HireTypeT].[ID Hire Type] 
                ) LEFT JOIN [General:LocationsT] ON MasterTableT.[Expected: Location ID] = [General:LocationsT].[ID Location] 
               ) LEFT JOIN [General:CompanyProgrammeNameT] ON MasterTableT.[Company Programmes ID] = [General:CompanyProgrammeNameT].[ID Company Programmes] 
              ) LEFT JOIN [General:Position/JobTitleT] ON MasterTableT.[Position/Job Title ID] = [General:Position/JobTitleT].[Position/Job title ID] 
             ) LEFT JOIN [General:ProfileTypeT] ON MasterTableT.[Expected: Profile Type ID] = [General:ProfileTypeT].[ID Profile type] 
            ) LEFT JOIN [General:ProgrammePeopleT] ON MasterTableT.[Functional Owner ID] = [General:ProgrammePeopleT].[ID Programme People] 
           ) LEFT JOIN [General:RecruitmentStatusT] ON MasterTableT.[Recruitment Status ID] = [General:RecruitmentStatusT].[ID Recruitment Status] 
          ) LEFT JOIN [General:LocationsT] AS [General:LocationsT_1] ON MasterTableT.[Actual: Location ID] = [General:LocationsT_1].[ID Location] 
         ) LEFT JOIN [General:ProfileTypeT] AS [General:ProfileTypeT_1] ON MasterTableT.[Actual: Profile type ID] = [General:ProfileTypeT_1].[ID Profile type] 
        ) LEFT JOIN [General:HireTypeT] AS [General:HireTypeT_1] ON MasterTableT.[Actual: type of Hire ID] = [General:HireTypeT_1].[ID Hire Type] 
       ) LEFT JOIN [General:BA/GF and UnitT] AS [General:BA/GF and UnitT_1] ON MasterTableT.[Actual: BA/GF/External and Unit ID] = [General:BA/GF and UnitT_1].[ID BA/GF and Unit] 
      ) LEFT JOIN [General:ProgrammePeopleT] AS [General:ProgrammePeopleT_1] ON MasterTableT.[Recruitment Driver ID] = [General:ProgrammePeopleT_1].[ID Programme People] 
     ) LEFT JOIN [General:ProgrammePeopleT] AS [General:ProgrammePeopleT_2] ON MasterTableT.[Recruitment Manager ID] = [General:ProgrammePeopleT_2].[ID Programme People] 
    ) LEFT JOIN [General:PmoOrgLevel3] ON MasterTableT.[PMO: Org Level 3] = [General:PmoOrgLevel3].OrgL3ID 
) 
LEFT JOIN [General:PmoOrgLevel4] ON MasterTableT.[PMO: Org Level 4] = [General:PmoOrgLevel4].OrgL4ID 
WHERE (((MasterTableT.ID) < 10000)); // <-- and this condition is my a query booster 

這個查詢是不是我的,我剛剛從別人:) 繼承沒有任何MSSQL服務器相關的表...全部都在MS Access數據庫文件中。

在此先感謝您的解釋。

+0

您需要提供更大的上下文。現在,這將是所有猜測。向我們顯示您的表,查詢計劃,創建索引的統計信息... –

+0

所有表都在同一個數據庫中,還是混合使用Access和SQLServer(或類似)表? –

回答

2

當您添加額外條件時,它可能會獲得不同的執行路徑。要查看執行計劃,您可以enable it。在不知道查詢的情況下,我猜add id會改變表的連接順序,而不是先連接一些表,然後用ids將它連接到表,然後按照不同的順序完成,其中第一個連接只傳遞一個很少的記錄,而不是整個表格。

+2

以下是用於啓用SHOWPLAN for Access 2010的註冊表項:http://stackoverflow.com/a/28370097/3820271 – Andre