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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 better performance for an update procedure?

Author  Topic 

tempp
Starting Member

2 Posts

Posted - 2006-12-24 : 07:20:55
hey yall :)

a quick but very important (performance wise) question:
i need to update a few thousands of records in a table, all with the same value - which is the average of a a few other thousands. now this goes on something like this:

update my_table t1
set (t1.my_field) =
(
select avg(t2.my_field)
from my_table t2
where
// (some simple criteria and join here)
t2.id = t1.id
AND t2.my_date > <<some date>> - <<some number of days>>
AND t2.my_date <= <<some date>>
)
where
t1.my_date > <<some date>>



now the value being updated is the same for all those with the same t1.id whose dates are future to <<some date>>.

any suggestions of improving performance of this query?


thanx, r. :)

Smart bombs exist only in the minds of stupid people.

MohammedU
Posting Yak Master

145 Posts

Posted - 2006-12-24 : 18:21:07
select avg(t2.my_field) as Avg_my_field , t2.id into #temp
from my_table t2
where
AND t2.my_date > <<some date>> - <<some number of days>>
AND t2.my_date <= <<some date>>

update t1
set (t1.my_field) = t2.Avg_my_field
from my_table t1
join #temp t2 on t2.id = t1.id
where
t1.my_date > <<some date>>


MohammedU
Go to Top of Page

tempp
Starting Member

2 Posts

Posted - 2006-12-24 : 19:43:33
Thanx Mohammed, I'll look it up!

Eid Mubarak, Merry Christmas

~~~~
Smart bombs exist only in the minds of stupid people.
Go to Top of Page
   

- Advertisement -