我一直在想减少下一个查询的执行时间。我已经做了很多研究,但没有发现任何可以帮助我的东西。该查询大约需要六个小时才能完成执行。我遇到的一个主要问题是我无法使用索引,是的,这很重要。
我的想法是,由于我不是SQL专家,所以也许我没有使用最佳实践来实现此查询所需的结果。
我正在处理大量的联接,聚合,临时表等。也许,由于我的经验不足,有些解决方法是我不知道的。因此,也许您可以帮助我。
SELECT
ROW_NUMBER() over (ORDER BY C.CourseKey) AS 'RowNumber',
C.CourseKey,
COALESCE(DCC.College,C.College) 'College',
COALESCE(DCC.Department,C.Department) 'Department',
COALESCE(DCC.CourseNumber,C.CourseNumber) 'CourseNumber',
COALESCE(DCC.[Subject],C.[Subject]) 'Subject',
C.Description + ' ('+C.BatchUID+')' as 'CourseSimple',
COALESCE(DCC.[Description],C.[Description]) as 'Section',
F.AcademicYear,
F.Description as 'Term',
--E.Description as 'Instructor',
B.Description as 'Item',
B.CourseItemGroup,
G.FullDate,
C.StartDate,
DU.UserKey,
SUM(A.CourseItemMinutes) AS 'Minutes',
SUM(A.CourseItemAccesses) As 'Items'
Into #CourseDesign
FROM Final.FactCourseItemActivity A
INNER JOIN Final.DimCourseItemType B on A.CourseItemTypeKey = B.CourseItemTypeKey
INNER JOIN Final.DimCourse C on A.CourseKey = C.CourseKey
INNER JOIN Final.DimUser DU on A.UserKey = DU.UserKey
INNER JOIN Final.FactStudentCourseSummary FSCS on FSCS.UserSourceKey = DU.SourceKey
And FSCS.CourseKey = A.CourseKey
And A.TermKey = FSCS.TermKey
INNER JOIN CustomFinal.DimCourseCustom DCC on FSCS.CourseCustomKey = DCC.CourseCustomKey
INNER JOIN Final.DimTerm F on A.TermKey = F.TermKey
INNER JOIN Final.DimDate G on A.DateKey = G.DateKey
WHERE A.CourseItemMinutes > 0
GROUP BY
C.CourseKey,
COALESCE(DCC.College,C.College),
COALESCE(DCC.Department,C.Department),
COALESCE(DCC.CourseNumber,C.CourseNumber),
COALESCE(DCC.[Subject],C.[Subject]),
C.Description + ' ('+C.BatchUID+')',
COALESCE(DCC.[Description],C.[Description]),
F.AcademicYear,
F.Description,
--E.Description,
B.Description,
B.CourseItemGroup,
G.FullDate,
C.StartDate,
DU.UserKey
Select
A.RowNumber,
A.Coursekey,
A.College,
A.Department,
A.CourseNumber,
A.[Subject],
A.CourseSimple,
A.Section,
A.AcademicYear,
A.Term,
A.Item,
A.CourseItemGroup,
A.FullDate,
H.WeekLevel,
SUM(A.[Minutes]) 'Minutes',
COUNT(Distinct A.UserKey) 'Students',
SUM(A.Items) 'Items'
Into #FinalTable
From #CourseDesign A
INNER JOIN Final.DimDayOfTerm H on
case
when A.FullDate is null then -2
when DATEDIFF(dd,A.StartDate,A.FullDate)+1 < 0 then -1
else DATEDIFF(dd,A.StartDate,A.FullDate)
end = H.DayOfTermKey
Group by
A.RowNumber,
A.Coursekey,
A.College,
A.Department,
A.CourseNumber,
A.[Subject],
A.CourseSimple,
A.Section,
A.AcademicYear,
A.Term,
A.Item,
A.CourseItemGroup,
A.FullDate,
H.WeekLevel
Select A.RowNumber,
A.Coursekey,
A.College,
A.Department,
A.CourseNumber,
A.[Subject],
A.CourseSimple,
A.Section,
A.AcademicYear,
A.Term,
A.Item,
A.CourseItemGroup,
A.FullDate,
A.WeekLevel,
STUFF((SELECT ', ' + DU2.Description
FROM #FinalTable AS A2
LEFT OUTER JOIN CustomFinal.ReportingInstructors RI2
on A2.CourseKey = RI2.CourseKey
LEFT OUTER JOIN Final.DimUser DU2
on RI2.UserKey = DU2.UserKey
WHERE ((A.RowNumber IS NULL AND A2.RowNumber IS NULL) OR A.RowNumber = A2.RowNumber)
FOR XML PATH('')), 1, 2, '') AS Instructor,
A.Minutes,
A.Students,
A.Items
From #FinalTable A
LEFT OUTER JOIN CustomFinal.ReportingInstructors RI on A.CourseKey = RI.CourseKey
LEFT OUTER JOIN Final.DimUser DU on RI.UserKey = DU.UserKey
Group by
A.RowNumber,
A.Coursekey,
A.College,
A.Department,
A.CourseNumber,
A.[Subject],
A.CourseSimple,
A.Section,
A.AcademicYear,
A.Term,
A.Item,
A.CourseItemGroup,
A.FullDate,
A.WeekLevel,
A.Minutes,
A.Students,
A.Items
答案 0 :(得分:0)
最近,我不得不将大量数据从一个数据库传输到另一个数据库。在将数据插入到目标中时,我不得不想出一种不会削弱我的源SQL Server的方法。
这就是我要做的。如果您正在做类似的事情,也许这也会对您有帮助。
insert_more_rows:
INSERT INTO MyDestination (
cols ...
)
SELECT TOP 1000
cols ...
FROM MySource
WHERE
NOT EXISTS ( condition to exclude existing data )...
IF ( ( some condition to determine there are more rows ) > 0 )
GOTO insert_more_rows;
这对我来说效果很好,并大大减少了所涉及的时间。