473,608 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Update Query Runs Forever

I'm running an update query in SQL 7 from QA, and it runs forever. Has been
running for 20 minutes so far! The query is quite simple: update a single
field in a table, based on a join with another table. There are a total of
1200 rows that are affected (out of a total of 60,000 in the table). Should
not take 20 minutes! Also, retrieving the 1200 rows using the same criteria
is instantaneous.

Anyone have any idea what's going on? Here's my SQL:

UPDATE INVTRY
SET Web = 'REVIEW'
FROM INVTRY INNER JOIN tabStatus
ON INVTRY.Status = tabStatus.Statu s
WHERE INVTRY.Web = 'NEW'
AND tabStatus.ForWe b = 1
AND INVTRY.PRICE <0
AND Invtry.HoldInit IS NULL
Jun 27 '08 #1
13 4157
Use an alias for the table to update, so there is no ambiguity:

UPDATE Invtry
SET Web = 'REVIEW'
FROM Invtry AS I
INNER JOIN tabStatus AS S
ON I.Status = S.Status
WHERE I.Web = 'NEW'
AND S.ForWeb = 1
AND I.Price <0
AND I.HoldInit IS NULL;

Also, you can rewrite the query as follows and see if it makes any
difference (although in general using the MS SQL syntax for UPDATE with FROM
is faster):

UPDATE Invtry
SET Web = 'REVIEW'
WHERE Web = 'New'
AND Price <0
AND HoldInit IS NULL
AND EXISTS(SELECT *
FROM tabStatus AS S
WHERE Invtry.Status = S.Status
AND S.ForWeb = 1);

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #2
Also, look for any locks on the table created by concurrent queries.

HTH,

Plamen Ratchev
http://www.SQLStudio.com
Jun 27 '08 #3
I'll try this tonight after the users are out of it. But, really, faster or
not, my original query should have run, especially since it was just
updating 1200 rows. As it is, it ran for a couple of hours without
completing, and then I stopped it.

So there has to be something else going on.

Also, this syntax has been in place for years, and has never had a problem.
But it was usually run on smaller batches, usually less than 100 items.
Still, 1200 is not a large number.


"Plamen Ratchev" <Pl****@SQLStud io.comwrote in message
news:4p******** *************** *******@speakea sy.net...
Use an alias for the table to update, so there is no ambiguity:

UPDATE Invtry
SET Web = 'REVIEW'
FROM Invtry AS I
INNER JOIN tabStatus AS S
ON I.Status = S.Status
WHERE I.Web = 'NEW'
AND S.ForWeb = 1
AND I.Price <0
AND I.HoldInit IS NULL;

Also, you can rewrite the query as follows and see if it makes any
difference (although in general using the MS SQL syntax for UPDATE with
FROM is faster):

UPDATE Invtry
SET Web = 'REVIEW'
WHERE Web = 'New'
AND Price <0
AND HoldInit IS NULL
AND EXISTS(SELECT *
FROM tabStatus AS S
WHERE Invtry.Status = S.Status
AND S.ForWeb = 1);

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #4
If you have concurrent queries on the table they may be blocking the update.

HTH,

Plamen Ratchev
http://www.SQLStudio.com
Jun 27 '08 #5
Also, if you have triggers on the table, they could be waiting for another
blocked resource.

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #6
Neil wrote:
>
I'm running an update query in SQL 7 from QA, and it runs forever. Has been
running for 20 minutes so far! The query is quite simple: update a single
field in a table, based on a join with another table. There are a total of
1200 rows that are affected (out of a total of 60,000 in the table). Should
not take 20 minutes! Also, retrieving the 1200 rows using the same criteria
is instantaneous.

Anyone have any idea what's going on? Here's my SQL:

UPDATE INVTRY
SET Web = 'REVIEW'
FROM INVTRY INNER JOIN tabStatus
ON INVTRY.Status = tabStatus.Statu s
WHERE INVTRY.Web = 'NEW'
AND tabStatus.ForWe b = 1
AND INVTRY.PRICE <0
AND Invtry.HoldInit IS NULL
I agree with Plamen. If the SELECT equivalent of this UPDATE returns the
1200 rows almost instantly, then most likely your UPDATE statement is
being blocked.

You can test this theory by running the SELECT equivalent if you include
a WITH (UPDLOCK) hint.

If so, you can use sp_lock and sp_lock2 to investigate possible
culplits.

If not blocking, then you should check the triggers on table INVTRY.

--
Gert-Jan
SQL Server MVP
Jun 27 '08 #7

"Gert-Jan Strik" <so***@toomuchs pamalready.nlwr ote in message
news:48******** *******@toomuch spamalready.nl. ..
Neil wrote:
>>
I'm running an update query in SQL 7 from QA, and it runs forever. Has
been
running for 20 minutes so far! The query is quite simple: update a single
field in a table, based on a join with another table. There are a total
of
1200 rows that are affected (out of a total of 60,000 in the table).
Should
not take 20 minutes! Also, retrieving the 1200 rows using the same
criteria
is instantaneous.

Anyone have any idea what's going on? Here's my SQL:

UPDATE INVTRY
SET Web = 'REVIEW'
FROM INVTRY INNER JOIN tabStatus
ON INVTRY.Status = tabStatus.Statu s
WHERE INVTRY.Web = 'NEW'
AND tabStatus.ForWe b = 1
AND INVTRY.PRICE <0
AND Invtry.HoldInit IS NULL

I agree with Plamen. If the SELECT equivalent of this UPDATE returns the
1200 rows almost instantly, then most likely your UPDATE statement is
being blocked.

You can test this theory by running the SELECT equivalent if you include
a WITH (UPDLOCK) hint.

If so, you can use sp_lock and sp_lock2 to investigate possible
culplits.

If not blocking, then you should check the triggers on table INVTRY.

--
Gert-Jan
SQL Server MVP
Plamen and Gert-Jan:

Yes, it was the trigger. I disabled the trigger, and the update query
executed in a couple of seconds. This didn't come to light before because it
seems to work fine with a few hundred records. But when the number got
larger (1200) it hung.

The problem with my trigger, perhaps, is that it uses a cursor.

See, the trigger updates the DateModified field, and it copies the entire
record to a history table. Because the record contains a timestamp field, I
can't use * to just copy the record. Instead, I have to list the fields
individually. Thus, I have the SQL to copy the record in its own stored
procedure, so that I have it in just one place. The trigger then just calls
the stored procedure. Since it's calling the stored procedure, I needed to
use a cursor to enable calling it for multiple records.

Here's my trigger:

ALTER Trigger INVTRY_Modified
On dbo.INVTRY
For Update
As

UPDATE INVTRY SET INVTRY.DateModi fied = getdate()
From INVTRY Inner Join Inserted
On INVTRY.[Index]=Inserted.[Index]

declare @Index1 int
declare curItems Cursor Local Forward_Only
For
SELECT [Index]
From Inserted
Open curItems
Fetch Next From curItems Into @Index1

WHILE @@Fetch_Status = 0
BEGIN
Execute Invtry_Archive @Index=@Index1
Fetch Next From curItems Into @Index1
END

Close curItems
Deallocate curItems
And here's the Invtry_Archive stored procedure that it calls:

ALTER Procedure Invtry_Archive

@Index int

AS
SET NOCOUNT ON

BEGIN
INSERT INTO Invtry_History ( [Index], DupedFromID, AUTHILL1, AUTHILL2,
TITLE, AUTHILL3, AUTHILL4, LOT, PAID, PRICE, BUYER, INCAT, LANG,
QTEDATE, Quote, GUIDE, YRPUB, INVDATE, TOPIC1, TOPIC2, TOPIC3, TOPIC4,
TOPIC5, TOPIC6, SHELF, JIT, MImage, HImage,
Feature, Track, HiRes, HiResCount, LowRes, Scan, SSEmail, SSPrint,
Status, ShootSpecs, Comment2, Owner, Location, Web,
WebStatusPendin g, Illus, EditStatus, CatStatus, HoldDate, HoldInit,
Comment, UpdateInd, ActivateDate, DeactivateDate,
AlternateItemID , WebAddedBatchID , WebModifiedBatc hID, FeaturedBook,
GiftBook1, GiftBook2, GiftBook3, AcquiredDate,
NewAcquisition, AllowDuplicate, WebAction, WebActionPendin g,
ApplyPendingWeb Action, DateWebActionAp plied, attFirstEdition ,
attSigned, attSignedPD, attAssociation, attAutograph, attIllustrated,
attIllustratedP D, attColorPlate, attColorPlatePD , attMaps,
attPhotographs, attSets, attSetsVols, attFineBinding, attFineBindingP D,
attForeignLang, attForeignLangP D, attPressBooks,
attPressBooksPD , attABEExport, attILABExport, attNonBook, Verification,
ShowcaseText, HeadlineQuote, SaleSrc, CatCode,
AdCode, ABEExport, ILABExport, DealerSrc, FullDescription DateStamp,
MiniDescription DateStamp, Desc_LC, DateCreated,
DateModified )
SELECT INVTRY.[Index], INVTRY.DupedFro mID, INVTRY.AUTHILL1 ,
INVTRY.AUTHILL2 , INVTRY.TITLE, INVTRY.AUTHILL3 , INVTRY.AUTHILL4 , INVTRY.LOT,
INVTRY.PAID, INVTRY.PRICE, INVTRY.BUYER, INVTRY.INCAT, INVTRY.LANG,
INVTRY.QTEDATE, INVTRY.Quote, INVTRY.GUIDE,
INVTRY.YRPUB, INVTRY.INVDATE, INVTRY.TOPIC1, INVTRY.TOPIC2,
INVTRY.TOPIC3, INVTRY.TOPIC4, INVTRY.TOPIC5, INVTRY.TOPIC6,
INVTRY.SHELF, INVTRY.JIT, INVTRY.MImage, INVTRY.HImage, INVTRY.Feature,
INVTRY.Track, INVTRY.HiRes, INVTRY.HiResCou nt,
INVTRY.LowRes, INVTRY.Scan, INVTRY.SSEmail, INVTRY.SSPrint,
INVTRY.Status, INVTRY.ShootSpe cs, INVTRY.Comment2 ,
INVTRY.Owner, INVTRY.Location , INVTRY.Web, INVTRY.WebStatu sPending,
INVTRY.Illus, INVTRY.EditStat us, INVTRY.CatStatu s,
INVTRY.HoldDate , INVTRY.HoldInit , INVTRY.Comment, INVTRY.UpdateIn d,
INVTRY.Activate Date, INVTRY.Deactiva teDate,
INVTRY.Alternat eItemID, INVTRY.WebAdded BatchID,
INVTRY.WebModif iedBatchID, INVTRY.Featured Book, INVTRY.GiftBook 1,
INVTRY.GiftBook 2, INVTRY.GiftBook 3, INVTRY.Acquired Date,
INVTRY.NewAcqui sition, INVTRY.AllowDup licate, INVTRY.WebActio n,
INVTRY.WebActio nPending, INVTRY.ApplyPen dingWebAction,
INVTRY.DateWebA ctionApplied, INVTRY.attFirst Edition, INVTRY.attSigne d,
INVTRY.attSigne dPD, INVTRY.attAssoc iation, INVTRY.attAutog raph,
INVTRY.attIllus trated, INVTRY.attIllus tratedPD, INVTRY.attColor Plate,
INVTRY.attColor PlatePD, INVTRY.attMaps, INVTRY.attPhoto graphs,
INVTRY.attSets, INVTRY.attSetsV ols, INVTRY.attFineB inding,
INVTRY.attFineB indingPD, INVTRY.attForei gnLang, INVTRY.attForei gnLangPD,
INVTRY.attPress Books, INVTRY.attPress BooksPD,
INVTRY.attABEEx port, INVTRY.attILABE xport, INVTRY.attNonBo ok,
INVTRY.Verifica tion, INVTRY.Showcase Text, INVTRY.Headline Quote,
INVTRY.SaleSrc, INVTRY.CatCode, INVTRY.AdCode, INVTRY.ABEExpor t,
INVTRY.ILABExpo rt, INVTRY.DealerSr c,
INVTRY.FullDesc riptionDateStam p, INVTRY.MiniDesc riptionDateStam p,
INVTRY.Desc_LC, INVTRY.DateCrea ted, INVTRY.DateModi fied
FROM INVTRY
Where [Index]=@Index
END

IF @@ERROR 0 GOTO on_error

RETURN(0)

on_error:
RETURN(1)
What would be great would be if I could copy the record without having to
list all the fields. But because I have to exclude the timestamp field, I
have to list all the fields except that. (Why don't they just make it so you
could do "* except [field list]"? That would be superb!)

Anyway, so I suppose the solution would be to move the above SQL to my
trigger and nix the cursor. Perhaps that would solve the problem.

Thoughts? Suggestions?

Thanks!

Neil
Jun 27 '08 #8
Just move the insert statement to the trigger body and remove the cursor and
the stored procedure. There is no need at all to process data one row at a
time, a single statement can handle it all.

Also, if possible move the update to DateModified to your application logic
(for example, if you have a stored procedure that performs the updates on
the table add the update to the column there). That way the trigger will
have less work to do and will complete faster.

Listing all columns in a statement is good. Using "*" is not a good idea and
can lead to more trouble down the road.

HTH,

Plamen Ratchev
http://www.SQLStudio.com

Jun 27 '08 #9

"Plamen Ratchev" <Pl****@SQLStud io.comwrote in message
news:Sq******** *************** *******@speakea sy.net...
Just move the insert statement to the trigger body and remove the cursor
and the stored procedure. There is no need at all to process data one row
at a time, a single statement can handle it all.
As noted, the reason I had it in a stored procedure was so that there would
be one place that listed all the fields, in case the fields change, so I
don't have to update it in two places. But, as it is, I'm not using the SP
except through the trigger, so, yeah, I guess I can move it.
Also, if possible move the update to DateModified to your application
logic (for example, if you have a stored procedure that performs the
updates on the table add the update to the column there). That way the
trigger will have less work to do and will complete faster.

Listing all columns in a statement is good. Using "*" is not a good idea
and can lead to more trouble down the road.
Just curious: what kind of trouble down the road would using * cause? I
would think it would prevent trouble down the road since, if the fields
change, it automatically uses the current fields at all times. What kind of
trouble are you referring to.

Also, a question: do you have any idea why the update query and trigger
worked when there were 200 rows being updated, but not 1200?

Thanks!

Neil

>
HTH,

Plamen Ratchev
http://www.SQLStudio.com


Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
9103
by: David Berman | last post by:
I'm having a problem with an update operation in a stored procedure. It runs so slowly that it is unusable, unless I comment a part out in which case it is very fast. However, I need the whole thing :). I have a table of email addresses of people who want to get invited to parties. Each row contains information like email address, city, state, country, and preferences for what types of events are of interest. The primary key is an...
3
10073
by: Dan Berlin | last post by:
I have two tables: T1 : Key as bigint, Data as char(20) - size: 61M records T2 : Key as bigint, Data as char(20) - size: 5M records T2 is the smaller, with 5 million records. They both have clustered indexes on Key. I want to do:
5
13212
by: Don Seckler | last post by:
I have an update query that runs when a report closes. I have several reports that will need to run the update query with diferent criteria. I'd like to simply make the criteria change in the report vba instead of making different queries. Here's my query sql: UPDATE Draw SET Draw.Billed = Yes WHERE (((Draw.Billed)=No) AND ((Draw.WholesalerName)="Hudson"));
4
11325
by: deko | last post by:
I'm trying to update the address record of an existing record in my mdb with values from another existing record in the same table. In pseudo code it might look like this: UPDATE tblAddress SET AddressDescription of Entity 456 = AddressDescription of Entity_ID 123 Address1 of Entity 456 = Address1 of Entity_ID 123 City of Entity 456 = City of Entity_ID 123
6
10745
by: Sparticus | last post by:
I have a database that isn't very big. It has about 2400 rows in it. I try and do an update like this one below (it looks big, but it's really not if you look at it) : UPDATE jobs SET prodduedate = '1/5/2005', clientduedate = '1/5/2005', digitizingcnd = 'x', digitizingcndnonsub = '', digitizingintl = '', retrieval = '', wellsitetape = '', boardsubmissions = '', hardcopylogs = '', burningcd = '', las20 = '', metric = '', tops = '',...
1
2617
by: Greg Strong | last post by:
Hello All, Any reason why a select query that runs will not run as an update query? What I've done is created a select query that runs successfully. The query has several custom functions to create data. When I attempt to run the query as an update query I receive an error message which reads:
3
2602
by: John Cosmas | last post by:
I have a DATATABLE which I have populated in my application, and I need it written out to a particular table I specify in my ACCESS database. My code works to the point of the MERGE and UPDATE, but it creates exactly the number of BLANK records per the populated DATATABLE. Here is my code... pstrDestinationTable = "tws_tbl_Case_Scanner_" & GetDateTimeStamp() pstrSQL = "SELECT * INTO " & pstrDestinationTable & " FROM...
16
3483
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for renaming the duplicate records? My thinking was to take the results of the duplicate query, and somehow have it number each line where there is a duplicate (tried a groups query, but "count" won't work), then do an update query to change the duplicate to...
1
1777
by: gkinu | last post by:
I have 2 tables with a parent-child relationship. Parent table's primary key field is Entry_no and is of type VarChar(50). This parent table has about 50 fields. The child has 3 fields, Entry_No (varchar(50), Requestor char(3) and id (integer). I need the following query QUERY 1. select p.entry_no, p.reg_date from tparent p, tchild c where p.entry_no<>c.entry_no and c.requestor='KPA' order by p.reg_date, p.entry_no; The above query...
0
8067
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8010
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8501
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6820
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6015
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3967
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4030
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.