我不是数据库人,也是我办公室中唯一可以理解任何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)
所有其他组的查询都相似,唯一不同的是HAVING中的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。[日期/时间]我得到以下错误:列'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]
答案 0 :(得分:0)
您似乎可以将一些查询组合在一起。假设第一个查询是Mayfield视图的一个示例,那么您可以将前四个结合起来(可能是语法错误):
Select
c.TagCode,
c.Release,
c.[Release Date/Time],
min(IIf(a.HD = 101, a.[Date/Time], Null)) AS HD101_First_Detection,
datediff(second, c.[Release Date/Time], min(IIf(a.HD = 101, a.[Date/Time], Null)) as TravelTime_101,
max(IIf(a.HD = 101, a.[Date/Time], Null)) AS HD101_Last_Detection,
min(IIf(a.HD = 102, a.[Date/Time], Null)) AS HD102_First_Detection,
datediff(second, c.[Release Date/Time], min(IIf(a.HD = 102, a.[Date/Time], Null)) as TravelTime_102,
max(IIf(a.HD = 102, a.[Date/Time], Null)) AS HD102_Last_Detection,
min(IIf(a.HD = 203, a.[Date/Time], Null)) AS HD102_First_Detection,
datediff(second, c.[Release Date/Time], min(IIf(a.HD = 203, a.[Date/Time], Null)) as TravelTime_203,
max(IIf(a.HD = 203, a.[Date/Time], Null)) AS HD102_Last_Detection,
min(IIf(a.HD = 204, a.[Date/Time], Null)) AS HD204_First_Detection,
datediff(second, c.[Release Date/Time], min(IIf(a.HD = 204, a.[Date/Time], Null)) as TravelTime_204,
max(IIf(a.HD = 204, a.[Date/Time], Null)) AS HD204_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, 102, 203, 204) 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
All_data (TagCode, HD, [Date/Time])
上的索引应该对此有所帮助。