Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
tooms
Starting Member
2 Posts |
Posted - 2005-07-27 : 14:09:06
|
I have a table like thisdbo.RawData[C_datetime] [C_value]2005-07-27 11:00 12005-07-27 11:01 22005-07-27 11:02 12005-07-27 11:03 32005-07-27 11:04 42005-07-27 11:05 12005-07-27 11:06 12005-07-27 11:07 32005-07-27 11:08 12005-07-27 11:09 22005-07-27 11:10 22005-07-27 11:11 12005-07-27 11:12 42005-07-27 11:13 12005-07-27 11:14 1 and I use this T-sql to get the avg(C_value) for each 5 min. intervaldeclare @Starttime datetimedeclare @endtime datetimedeclare @Finishtime datetimedeclare @value floatset @Starttime = getdate()set @EndTime = getdate()set @FinishTime = DATEADD(day, -1, @Starttime)CREATE TABLE #TBOUT ([C_DateTime] [datetime] NOT NULL PRIMARY KEY,[C_Value] [float] NULL)while @Endtime >= @FinishTime begin set @Endtime = DATEADD(second, -300, @Endtime) Select @value = avg(C_Value) from dbo.RawData where (C_Datetime between @Endtime and @Starttime) if @value IS NOT NULL BEGIN Insert into #TBOUT (C_DateTime, C_Value) values (@Starttime, @value) end if @Endtime <= @FinishTime break Set @Starttime = @Endtime endSelect * from #TBOUT order by C_DatetimeDROP TABLE #TBOUT output from t-sql is[C_datetime] [C_value]2005-07-27 11:00 112005-07-27 11:05 82005-07-27 11:10 9 my question is now how can i get it to run faster beause it is very slow and my webpages are timeout on the usershere is a sample with a timespan on 7 days with a interval on 5 min.Time to query this is 4 min. and 48 sec. and returns 2016 rows, and the server show 100% cpu when processing.Thank Thomas |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-07-27 : 14:24:24
|
[code]Create Table #n (n int identity(1,1), x int)Insert Into #n Select x=0 From master.dbo.syscolumns--Select * From #nCreate Table #myTable (c_Datetime datetime, c_Value int)Insert Into #myTableSelect '2005-07-27 11:00', 1 Union AllSelect '2005-07-27 11:01', 2 Union AllSelect '2005-07-27 11:02', 1 Union AllSelect '2005-07-27 11:03', 3 Union AllSelect '2005-07-27 11:04', 4 Union AllSelect '2005-07-27 11:05', 1 Union AllSelect '2005-07-27 11:06', 1 Union AllSelect '2005-07-27 11:07', 3 Union AllSelect '2005-07-27 11:08', 1 Union AllSelect '2005-07-27 11:09', 2 Union AllSelect '2005-07-27 11:10', 2 Union AllSelect '2005-07-27 11:11', 1 Union AllSelect '2005-07-27 11:12', 4 Union AllSelect '2005-07-27 11:13', 1 Union AllSelect '2005-07-27 11:14', 1Select * From #myTableDeclare @StartDateTime datetimeSelect @StartDateTime = min(c_Datetime) From #myTableSelect BeginTime, EndTime, Ttl = sum(c_value) From ( Select BeginTime = dateadd(s,(n-1)*300,@StartDateTime), EndTime = dateadd(s,n*300,@StartDateTime) From #n ) AInner Join #myTable BOn A.BeginTime <= B.c_Datetimeand A.EndTime > B.c_DatetimeGroup By BeginTime, EndTimeDrop Table #myTableDrop Table #n[/code]Corey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
|
tooms
Starting Member
2 Posts |
Posted - 2005-07-27 : 15:49:37
|
Thanks alot, it is now around 2 sec. to return the rows and not 4min and 48 sec. |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-07-28 : 08:40:26
|
I'd say thats faster Corey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
|
|
|
|
|
|