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 2005 Forums
 Transact-SQL (2005)
 Update Query running too slow.

Author  Topic 

kellog1
Starting Member

35 Posts

Posted - 2010-08-10 : 15:01:57
Can anybody tell why is this query so slow...Basically I amtrying to update a column based on output from joins below.

UPDATE DimProjectPublicationIndicator
SET HasObligation = 'Y'
FROM DimProjectPublicationIndicator u
JOIN DimProjectAction t ON
u.PublicationId = t.PublicationId AND
u.ProjectPublicationId = t.ProjectPublicationId
JOIN DimAction a ON
t.PublicationId = a.PublicationId AND
t.ActionId = a.ActionId
JOIN DimActionType c ON
a.PublicationId = c.PublicationId AND
a.ActionTypeId = c.ActionTypeId
WHERE c.Code = 'O'

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-10 : 15:14:49
how many records does tables contain? what are available indexes?


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

kellog1
Starting Member

35 Posts

Posted - 2010-08-10 : 15:24:55
These are small size tables with biggest being DimProjectPublicationIndicator table having 200,000 records. And I have non clustered indexes on each tables. But I am wondering if my update statement is incorrect?

quote:
Originally posted by visakh16

how many records does tables contain? what are available indexes?


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-11 : 07:31:38
UPDATE DimProjectPublicationIndicator U
SET HasObligation = 'Y'
FROM DimProjectPublicationIndicator U
...

not quite sure what happens if you give the original table name for an aliased table - perhaps you get a cartesian join?

Anyways, better to always use an Alias Name for the table to be updated IMHO
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-08-11 : 10:03:47
Interesting suggestion Kristen but I don't think the query plan will differ. This one doesn't (on 2008). OFC you *should* use the alias and I've got no idea what might happen if another copy of the updated table were to be added to the FROM clause.


IF OBJECT_ID('tempDb..#foo') IS NOT NULL DROP TABLE #foo
CREATE TABLE #foo (
[ID] INT PRIMARY KEY
, [value] VARCHAR(50)
)

IF OBJECT_ID('tempDb..#bar') IS NOT NULL DROP TABLE #bar
CREATE TABLE #bar (
[ID] INT IDENTITY(1,1) PRIMARY KEY
, [fooID] INT
, [value] UNIQUEIDENTIFIER
)

CREATE INDEX IX_BAR_FOO_LINK ON #bar([fooID])


INSERT #foo ([Id], [value])
SELECT 1, 'aaaaa'
UNION SELECT 2, 'bbbbb'
UNION SELECT 3, 'ccccc'


INSERT #bar([fooID], [value])
SELECT
a.[ID]
, NEWID()
FROM
#foo a
CROSS JOIN #foo b
CROSS JOIN #foo c
CROSS JOIN #foo d
CROSS JOIN #foo e
CROSS JOIN #foo f
CROSS JOIN #foo g

UPDATE #foo SET
[value] = 'HELLO'
FROM
#foo f
JOIN #bar b ON b.[fooId] = f.[ID]
WHERE
b.[fooID] = 1

UPDATE f SET
[value] = 'WORLD'
FROM
#foo f
JOIN #bar b ON b.[fooId] = f.[ID]
WHERE
b.[fooID] = 2

SELECT * FROM #foo




kellog1 -- you said you had nonclustered indexes on each table. Are you sure your query is using them. Have you checked the actual execution plan?


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Ifor
Aged Yak Warrior

700 Posts

Posted - 2010-08-11 : 10:33:15
An ANSI type update may help, otherwise update stats, check indexes etc

UPDATE DimProjectPublicationIndicator
SET HasObligation = 'Y'
WHERE EXISTS
(
SELECT *
FROM DimProjectAction t
WHERE DimProjectPublicationIndicator.PublicationId = t.PublicationId
AND DimProjectPublicationIndicator.ProjectPublicationId = t.ProjectPublicationId
AND EXISTS
(
SELECT *
FROM DimAction a
WHERE t.PublicationId = a.PublicationId
AND t.ActionId = a.ActionId
AND EXISTS
(
SELECT *
FROM DimActionType c
WHERE a.PublicationId = c.PublicationId
AND a.ActionTypeId = c.ActionTypeId
AND c.Code = 'O'
)
)
)

Go to Top of Page

BarDev
Starting Member

2 Posts

Posted - 2010-08-19 : 13:56:00
If I run the following update query, the index that is created on PhoneID is not used. Also there is no way to tell the query to use the index IX_PhoneStatus_PhoneID

UPDATE PhoneStatus
SET RecordEndDate = GETDATE()
WHERE
PhoneID = 126

If I execute the following query, which includes the new FROM, I still have the same problem with the index not used

UPDATE PhoneStatus
SET RecordEndDate = GETDATE()
FROM Cust_Profile.PhoneStatus
WHERE
PhoneID = 126

But if I add the HINT to force the use of the index on the FROM it seems to work correctly.

UPDATE PhoneStatus
SET RecordEndDate = GETDATE()
FROM Cust_Profile.PhoneStatus WITH(INDEX(IX_PhoneStatus_PhoneID))
WHERE
PhoneID = 126

SQL Server should figure out what index to use, and I shouldn't have to force the index to be used. But this is how I got it to work.

BarDev

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-19 : 14:01:31
quote:
Originally posted by BarDev

If I run the following update query, the index that is created on PhoneID is not used. Also there is no way to tell the query to use the index IX_PhoneStatus_PhoneID

UPDATE PhoneStatus
SET RecordEndDate = GETDATE()
WHERE
PhoneID = 126

If I execute the following query, which includes the new FROM, I still have the same problem with the index not used

UPDATE PhoneStatus
SET RecordEndDate = GETDATE()
FROM Cust_Profile.PhoneStatus
WHERE
PhoneID = 126

But if I add the HINT to force the use of the index on the FROM it seems to work correctly.

UPDATE PhoneStatus
SET RecordEndDate = GETDATE()
FROM Cust_Profile.PhoneStatus WITH(INDEX(IX_PhoneStatus_PhoneID))
WHERE
PhoneID = 126

SQL Server should figure out what index to use, and I shouldn't have to force the index to be used. But this is how I got it to work.

BarDev




how many records are there in table with PhoneID=126? whats the % of records out of total?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-08-20 : 01:32:13
... and are the statistics been updated regularly?

Certainly, for me, I would manually update the statistics before checking the query plan - to make sure that the query planner was using the most up to date information
Go to Top of Page
   

- Advertisement -