0

我不是数据库人员,并且是我办公室中唯一可以理解任何 SQL 或 ACCESS 的人之一。因此,我成为了首选。

我有一个包含超过 3000 万条记录的表,并且正在尝试创建一个使用多个 group by 查询来创建的查询。唯一的问题是它需要几个小时才能运行,我不知道如何提高它的性能。这是第一个查询的示例:

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)

所有其他 group by 查询都相似,唯一的区别是 HAVING (dbo.All_data.HD = 101) 中的 HD 不同。然后,我试图将它们全部组合在一起以创建一个输出。

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] 放入 Group by 我会收到以下错误: 列 'a.Date/Time' 在选择列表中无效,因为它既不包含在聚合函数中,也不包含在GROUP BY 子句。但是,如果我输入 a.[Date/Time],我会得到所有日期时间,而不仅仅是 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]
4

1 回答 1

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])应该对此有所帮助。

于 2013-11-15T00:21:26.177 回答