我不是數據庫人員,而且是我辦公室中可以理解任何SQL或ACCESS的唯一人員之一。因此,我已成爲去。由多個小組通過查詢創建的查詢花費數小時的時間運行
我有一張擁有超過3000萬條記錄的表,並試圖創建一個使用多個查詢組來創建的查詢。唯一的問題是運行需要幾個小時,我不確定如何提高其性能。這是第一個查詢的示例:
SELECT dbo.Characteristics.TagCode, dbo.Characteristics.[Release Date/Time],
dbo.All_data.HD, MIN(dbo.All_data.[Date/Time]) AS [MinOfDate/Time],
MAX(dbo.All_data.[Date/Time]) AS [MaxOfDate/Time],
dbo.Detection_Sites.Site, dbo.Detection_Sites.Description,
DATEDIFF(second,
dbo.Characteristics.[Release Date/Time],
MIN(dbo.All_data.[Date/Time])
) AS TravelTime_101
FROM dbo.All_data
INNER JOIN dbo.Detection_Sites
ON dbo.All_data.HD = dbo.Detection_Sites.[Hydrophone Number]
INNER JOIN dbo.Characteristics
ON dbo.All_data.Tagcode = dbo.Characteristics.TagCode
GROUP BY dbo.Characteristics.TagCode, dbo.Characteristics.[Release Date/Time],
dbo.All_data.HD, dbo.Detection_Sites.Site,
dbo.Detection_Sites.Description
HAVING (dbo.All_data.HD = 101)
所有通過查詢另一組是相似的,唯一的區別是在具有HD(dbo.All_data.HD = 101)是不同的。然後我試圖把它們全部拉到一起來創造一個輸出。
SELECT dbo.Characteristics.TagCode, dbo.Characteristics.Release,
dbo.Characteristics.[Release Date/Time],
dbo.Mayfield_HD101_First_Last_Detections.[MinOfDate/Time] AS HD101_First_Detection,
dbo.Mayfield_HD101_First_Last_Detections.TravelTime_101,
dbo.Mayfield_HD101_First_Last_Detections.[MaxOfDate/Time] AS HD101_Last_Detection,
dbo.Mayfield_HD102_First_Last_Detections.[MinOfDate/Time] AS HD102_First_Detection,
dbo.Mayfield_HD102_First_Last_Detections.TravelTime_102,
dbo.Mayfield_HD102_First_Last_Detections.[MaxOfDate/Time] AS HD102_Last_Detection,
dbo.Mayfield_HD203_First_Last_Detections.[MinOfDate/Time] AS HD203_First_Detection,
dbo.Mayfield_HD203_First_Last_Detections.TravelTime_203,
dbo.Mayfield_HD203_First_Last_Detections.[MaxOfDate/Time] AS HD203_Last_Detection,
dbo.Recollected_MDC_HD204_Last_Detection.[MaxOfDate/Time] AS HD204_Recollected_Last_Detection,
dbo.Recollected_MDC_HD204_Last_Detection.Recollected AS Recollected_Travel_Time,
dbo.Barrier_First_Last_Detections.[MinOfDate/Time] AS BD_First_Detection, dbo.Barrier_First_Last_Detections.TravelTime_BD,
dbo.Barrier_First_Last_Detections.[MaxOfDate/Time] AS BD_Last_Detection,
dbo.Hatchery_First_Last_Detections.[MinOfDate/Time] AS TH_First_Detection, dbo.Hatchery_First_Last_Detections.TravelTime_TH,
dbo.Hatchery_First_Last_Detections.[MaxOfDate/Time] AS TH_Last_Detection, dbo.Characteristics.FishStatus, dbo.Characteristics.DNCO,
dbo.Characteristics.Upstream, dbo.Characteristics.Tagger
FROM dbo.Characteristics
LEFT OUTER JOIN
dbo.Mayfield_HD203_First_Last_Detections
ON dbo.Characteristics.TagCode = dbo.Mayfield_HD203_First_Last_Detections.TagCode
LEFT OUTER JOIN
dbo.Barrier_First_Last_Detections
ON dbo.Characteristics.TagCode = dbo.Barrier_First_Last_Detections.TagCode
LEFT OUTER JOIN
dbo.Recollected_MDC_HD204_Last_Detection
ON dbo.Characteristics.TagCode = dbo.Recollected_MDC_HD204_Last_Detection.TagCode
LEFT OUTER JOIN
dbo.Mayfield_HD102_First_Last_Detections
ON dbo.Characteristics.TagCode = dbo.Mayfield_HD102_First_Last_Detections.TagCode
LEFT OUTER JOIN
dbo.Hatchery_First_Last_Detections
ON dbo.Characteristics.TagCode = dbo.Hatchery_First_Last_Detections.TagCode
LEFT OUTER JOIN
dbo.Mayfield_HD101_First_Last_Detections
ON dbo.Characteristics.TagCode = dbo.Mayfield_HD101_First_Last_Detections.TagCode
WHERE (NOT (dbo.Characteristics.FishStatus LIKE N'mort'))
AND (dbo.Characteristics.DNCO IS NULL)
AND (dbo.Characteristics.Upstream IS NULL)
謝謝任何幫助,你可以給我!
加法!
我已經接近一些,但遇到了一個問題。如果我沒有在組中輸入a。[Date/Time],我會得到以下錯誤:列'a.Date/Time'在選擇列表中無效,因爲它不包含在聚合函數或GROUP BY子句。但是如果我把a。[日期/時間]我得到所有的日期時間,而不是HD = 101的最小/最大值。
Select
c.TagCode,
c.Release,
c.[Release Date/Time],
MIN (CASE a.HD WHEN 101 THEN a.[Date/Time]ELSE Null END) AS HD101_First_Detection,
datediff(second, c.[Release Date/Time], a.[Date/Time]) AS TravelTime_101,
MAX (CASE a.HD WHEN 101 THEN a.[Date/Time]ELSE Null END) AS HD101_Last_Detection,
c.FishStatus,
c.DNCO,
c.Upstream,
c.Tagger
From
dbo.All_data a
inner join
dbo.Characteristics c
on a.Tagcode = c.TagCode
Where
HD In (101) And
Not c.FishStatus like N'mort' And
c.DNCO is null and
c.Upstream is null
Group By
c.TagCode,
c.Release,
c.[Release Date/Time],
c.FishStatus,
c.DNCO,
c.Upstream,
c.Tagger,
a.[Date/Time]
自從我使用Access以來已經很長時間了,但是我認爲您需要將'have dbo.All_data.HD = 101'更改爲'where dbo.All_data.HD = 101'(並且將其移到組之前)。該條款將不會被應用,直到查詢的其餘部分處理完畢。所以它會每隔一段時間旋轉所有行,然後去掉那些與之不匹配的行。 – Andrew
您在Microsoft Access中擁有30萬行表格! – Gerrat
我在想(希望!)Access是SQL Server數據庫的前端。 – Andrew