[posted and mailed, please reply in news]
Karaoke Prince (ka************@hotmail.com) writes:
I have an update statement to update a field of a table (~15,000,000
records). It took me around 3 hours to finish 2 weeks ago. After that
no one touched the server and no configuration changed. Until
yesterday, I re-ran it again and it took me more than 18hrs and still
not yet finished!!!
The first thing to check for is blocking. It might simply be that
some other process is holding a lock on the [all sales] table, preventing
you from continue. Use sp_who to check this. If you see a non-zero value
in the Blk column, this spid is blocking the spid on this line.
The second thing to check for is triggers. A trigger on a 15-million-
row table could be fatal.
Not that these are the most likely reasons, but they are easy to check.
What's wrong with it? I can ran it successfully before. I have tried
two times but the result was still the same.
My SQL statement is:
update [all_sales] a
set a.accounting_month = b.accounting_month
from date_map b
where a.sales_date >= b.start_date and a.sales_date < b.end_date;
If that is your SQL, you are not running SQL Server, but some other
product. When I run the above, I get a syntax error:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'a'.
In lieu of other information, I have to assume that the real query is
update [all_sales]
set accounting_month = b.accounting_month
from [all sales] a, date_map b
where a.sales_date >= b.start_date and a.sales_date < b.end_date;
I would recommend that you add this condition to this query:
and a.accounting_month <> b.accounting_month
This may or may not improve the query plan, but at least it reduces
the number of rows. (Since you ran this statement a couple of
months ago, I guess that most rows already have the correct
accounting_month.)
Note that the query is such that SQL Server will have to read all rows
in the table. (I assume that all values of [all sales].sales_date
are covered by date_map.)
If there were clustered indexes on a.sales_date and b.start_date,
SQL Server could use a merge join. But this would only give a minor
improvement over the case where there are no indexes at all, in
which case I would expect SQL Server to hash date_map (I guess
this table has < 1000 rows).
Really, what is going on I don't know, but if you have a powerful machine,
SQL Server may be doing some sort of a loop join, invoking parallellism.
You could type the query into Query Analyzer, and then press Ctrl-L to
see the estimated execution plan. If the plan involves loop join, I
would try this:
update [all_sales]
set accounting_month = b.accounting_month
from [all sales] a
inner hash join date_map b
on a.sales_date >= b.start_date and a.sales_date < b.end_date
where a.accounting_month <> b.accounting_month
SQL Server will issue a warning when you use join hint, but as long as
the query executes well, that's OK.
--
Erland Sommarskog, SQL Server MVP,
so****@algonet.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp