从另一个查询生成的访问查询需要很长时间才能运行

时间:2016-02-03 21:02:38

标签: database vba ms-access

我正在为客户端的MS Access DB添加一些新功能。原始数据库有几个查询。我需要添加的新功能要求我重新使用这些查询并将它们合并到我的VBA和SQL代码中。

例如,我正在基于以前的查询之一生成新查询(通过VBA和SQL)。然后我将结果导出为ex​​cel文件。

但是,每当我尝试运行其中一个新查询时,大约需要15分钟才能完成。在此期间,屏幕右下角显示消息,表示"运行查询。 "

这是我正在运行的SQL查询之一。请注意,当只有一个WHERE条件时,它会快速运行。

SELECT
   StudentProgram.fkCohortID AS [Cohort],
   Student.pkStudentID AS [Student ID],
   Student.EmplID AS [Employee ID],
   Student.LastName AS [Last Name],
   Student.FirstName AS [First Name],
   PostBaccActivity.fkSemesterID AS [Semester],
   PostBaccActivity.fkPostBaccID AS [PostBacc],
   PostBaccActivity.fkGradSchoolID AS [GradSchool],
   PostBaccActivity.ProjectTitle AS [ProjectTitle],
   PostBaccActivity.fkFacultyID AS [Faculty], 
   PostBaccActivity.BeginDate AS [BeginDate],
   PostBaccActivity.EndDate AS [EndDate],
   PostBaccActivity.Status AS [Status] 

FROM qryRptJoinAll
WHERE
       qryRptJoinAll.StudentProgram.fkCohortID  BETWEEN 1 AND 12 
    OR qryRptJoinAll.StudentProgram.fkCohortID = 25 
    OR qryRptJoinAll.StudentProgram.fkCohortID = 28 
    OR qryRptJoinAll.StudentProgram.fkCohortID = 49 
    OR qryRptJoinAll.StudentProgram.fkCohortID = 215 
    OR qryRptJoinAll.StudentProgram.fkCohortID = 220  

 GROUP BY StudentProgram.fkCohortID, Student.pkStudentID, Student.EmplID, Student.LastName,
          Student.FirstName, PostBaccActivity.fkSemesterID, PostBaccActivity.fkPostBaccID,
          PostBaccActivity.fkGradSchoolID, PostBaccActivity.ProjectTitle, PostBaccActivity.fkFacultyID,
          PostBaccActivity.BeginDate, PostBaccActivity.EndDate, PostBaccActivity.Status

这是我用来生成其他查询的查询:

SELECT 
   Student.pkStudentID, Student.EmplID, Student.OldID, Student.Inactive,
   Student.InactiveReason, Student.Status, Student.LastName, Student.MarriedName,
   Student.FirstName, Student.MiddleName, Student.DOB, Student.Sex, Student.SSN,
   Student.Email, Student.Race, Student.Ethnicity, Student.EmailSecondary,
   Student.fkSemesterBCStart, Student.fkSemesterGrad, Student.TotalCredits,
   Student.CreditsAttempted, Student.IndexCredits, Student.QualityPoints,
   Student.LocalCredits, Student.TransferCredits, Student.OtherCredits,
   StudentProgram.*, StudentEvent.*, StudentResearch.*, StudentEmployment.*,
   StudentMajor.*, PostBaccActivity.*, StudentPresentation.*, Presentation.*,
   StudentNote.*, SemesterGPA.*, StudentPublication.*, Publication.*, StudentCourse.fkCourseID,
   StudentCourse.fkFacultyID, StudentCourse.fkSemesterID, StudentCourse.Grade, Grade.GradeValue,
   Grade.NoValue, Course.*, RptCumulativeScienceGPA2.ScienceGPACalc, RptStudentControlList.pkStudentControlID
FROM 

    ((((((((PostBaccActivity RIGHT JOIN ((((((StudentProgram RIGHT JOIN 
Student ON StudentProgram.fkStudentID = Student.pkStudentID)
LEFT JOIN Cohort ON StudentProgram.fkCohortID = Cohort.pkCohortID) LEFT JOIN StudentEmployment ON Student.pkStudentID = StudentEmployment.fkStudentID)
LEFT JOIN StudentNote ON Student.pkStudentID = StudentNote.fkStudentID) LEFT JOIN StudentPresentation ON Student.pkStudentID = StudentPresentation.fkStudentID)
LEFT JOIN Presentation ON StudentPresentation.fkPresentationID = Presentation.pkPresentationID) ON PostBaccActivity.fkStudentID = Student.pkStudentID) 
LEFT JOIN RptCumulativeScienceGPA2 ON Student.pkStudentID = RptCumulativeScienceGPA2.fkStudentID) 
LEFT JOIN RptStudentControlList ON Student.pkStudentID = RptStudentControlList.fkControlID)
LEFT JOIN (StudentPublication LEFT JOIN Publication ON StudentPublication.fkPublicationID = Publication.pkPubID) ON Student.pkStudentID = StudentPublication.fkStudentID)
LEFT JOIN StudentResearch ON Student.pkStudentID = StudentResearch.fkStudentID) LEFT JOIN StudentMajor ON Student.pkStudentID = StudentMajor.fkStudentID)
LEFT JOIN StudentEvent ON Student.pkStudentID = StudentEvent.fkStudentID) 
LEFT JOIN (Course RIGHT JOIN (Grade RIGHT JOIN StudentCourse ON Grade.Grade = StudentCourse.Grade) ON Course.pkCourseID = StudentCourse.fkCourseID) ON Student.pkStudentID = StudentCourse.fkStudentID) 
LEFT JOIN SemesterGPA ON Student.pkStudentID = SemesterGPA.fkStudentID;

无论如何,我可以减少他们跑步所需的时间吗?

2 个答案:

答案 0 :(得分:1)

在不知道确切查询的情况下,只能提供通用建议:

  1. 将索引添加到表中的相关字段,这取决于查询
  2. 如果重复相同的查询,请缓存结果并重复使用
  3. 可以通过预过滤和缓存来优化大型表的连接
  4. 如果可能,实现异步查询,这些查询可以在后台运行时返回部分结果;这可以给出更快响应的错觉

答案 1 :(得分:1)

  • 由于您没有使用任何聚合函数,因此请删除GROUP BY子句。
  • 确保StudentProgram.fkCohortID
  • 上有索引

但我的猜测是实际复杂性在qryRptJoinAll,因此您也必须向我们展示此查询。