Does anyone know how to make this Update statement run faster? Currently it
takes over 30 minutes; there are over 4,310,000 records that would be
updated. Thanks for any suggestions.
UPDATE ParcelAuthority
SET PrevDue = lastYear.Due
FROM
(
SELECT PrcNr, AuthCd, Due
FROM ParcelAuthority
WHERE YearNr = 2004
) lastYear
WHERE ParcelAuthority.PrcNr = lastYear.PrcNr
AND ParcelAuthority.AuthCd = lastYear.AuthCd
AND ParcelAuthority.YearNr = 2005create an index on year,PrcNr, AuthCd in that order. if one does not exist
and
try to rephrase the query as
UPDATE A
SET PrevDue = B.Due
FROM ParcelAuthority a, ParcelAuthority b
WHERE A.PrcNr = B.PrcNr
AND A.AuthCd = B.AuthCd
AND A.YearNr = 2005
and B.YearNr = 2004|||Do you mean create non clustered indexes as follows:
CREATE NONCLUSTERED INDEX ParcelAuthority_YearNr
ON ParcelAuthority (TaxYearNr)
CREATE NONCLUSTERED INDEX ParcelAuthority_PrcNr
ON ParcelAuthority (PrcNr)
CREATE NONCLUSTERED INDEX ParcelAuthority_AuthCd
ON ParcelAuthority (AuthCd)
"Omnibuzz" wrote:
> create an index on year,PrcNr, AuthCd in that order. if one does not exist
> and
> try to rephrase the query as
> UPDATE A
> SET PrevDue = B.Due
> FROM ParcelAuthority a, ParcelAuthority b
> WHERE A.PrcNr = B.PrcNr
> AND A.AuthCd = B.AuthCd
> AND A.YearNr = 2005
> and B.YearNr = 2004
>|||well if you already have clustered index and not on these columns in this
order,
create the non clustered index this way.
Create Procedure stp_test
CREATE NONCLUSTERED INDEX IdxParcelAuthority
ON ParcelAuthority (TaxYearNr,PrcNr,AuthCd)|||On Wed, 19 Apr 2006 11:36:02 -0700, Omnibuzz wrote:
>well if you already have clustered index and not on these columns in this
>order,
>create the non clustered index this way.
>Create Procedure stp_test
>CREATE NONCLUSTERED INDEX IdxParcelAuthority
> ON ParcelAuthority (TaxYearNr,PrcNr,AuthCd)
Hi Omnibuzz,
If Petro adds Due to this nonclustered index, it will become a coovering
index. This should speed up the query even further.
Hugo Kornelis, SQL Server MVP|||
> Hi Omnibuzz,
> If Petro adds Due to this nonclustered index, it will become a coovering
> index. This should speed up the query even further.
Perfect Hugo. Yeah... why didn't I think of it :) and to extend hugo's
comment, if its in SQL Server 2005 we can add Due as a non-key column in the
index.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment