我得到了一个简单的任务:为表中的每个唯一 ID 选择“id”、最小值和最大值。所以我写得很简单group by
,但查询需要很长时间才能执行(30-60 秒)
SELECT CHPDataElement.DataElementID, MIN(CHPDataElementData.UTCDataTime) AS MinDataTime, MAX(CHPDataElementData.UTCDataTime) AS MaxDataTime
FROM CHPDataElement INNER JOIN
CHPDataElementData ON CHPDataElement.DataElementID = CHPDataElementData.DataElementID
GROUP BY CHPDataElement.DataElementID
order by
CHPDataElement.DataElementID
于是我开始着手改进。并提出了在 0.3-0.5 秒内返回相同数据的简单迭代。
declare @result table
(
DataElementID int,
MinDataTime datetime NULL,
MaxDataTime datetime null
)
declare @currentID int
declare @nextID int
declare @time datetime
insert into @result (DataElementID, MinDataTime, MaxDataTime)
select DataElementID,null,null from CHPDataElement
order by DataElementID
select top 1 @currentID=DataElementID from @result
while @currentID is not null
begin
print @currentID
select top 1 @time=UTCDataTime from CHPDataElementData
where DataElementID = @currentID
order by UTCDataTime asc
update @result set MinDataTime = @time
where DataElementID = @currentID
select top 1 @time=UTCDataTime from CHPDataElementData
where DataElementID = @currentID
order by UTCDataTime desc
update @result set MaxDataTime = @time
where DataElementID = @currentID
set @nextID = null
select top 1 @nextID=DataElementID from @result where DataElementID > @currentID
set @currentID = @nextID
end
select * from @result
谁能解释为什么“分组依据”与第二个查询相比效率如此之低?