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 DimProjectPublicationIndicatorSET HasObligation = 'Y'FROM DimProjectPublicationIndicator uJOIN DimProjectAction t ON u.PublicationId = t.PublicationId AND u.ProjectPublicationId = t.ProjectPublicationIdJOIN DimAction a ON t.PublicationId = a.PublicationId AND t.ActionId = a.ActionIdJOIN DimActionType c ON a.PublicationId = c.PublicationId AND a.ActionTypeId = c.ActionTypeIdWHERE 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
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 MVPhttp://visakhm.blogspot.com/
|
 |
|
Kristen
Test
22859 Posts |
Posted - 2010-08-11 : 07:31:38
|
UPDATE DimProjectPublicationIndicator USET 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 |
 |
|
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 #fooCREATE TABLE #foo ( [ID] INT PRIMARY KEY , [value] VARCHAR(50) )IF OBJECT_ID('tempDb..#bar') IS NOT NULL DROP TABLE #barCREATE 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 gUPDATE #foo SET [value] = 'HELLO'FROM #foo f JOIN #bar b ON b.[fooId] = f.[ID]WHERE b.[fooID] = 1UPDATE f SET [value] = 'WORLD'FROM #foo f JOIN #bar b ON b.[fooId] = f.[ID]WHERE b.[fooID] = 2SELECT * 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 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2010-08-11 : 10:33:15
|
An ANSI type update may help, otherwise update stats, check indexes etcUPDATE DimProjectPublicationIndicatorSET 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' ) )) |
 |
|
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_PhoneIDUPDATE PhoneStatus SET RecordEndDate = GETDATE()WHERE PhoneID = 126If I execute the following query, which includes the new FROM, I still have the same problem with the index not usedUPDATE PhoneStatus SET RecordEndDate = GETDATE()FROM Cust_Profile.PhoneStatus WHERE PhoneID = 126But 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 = 126SQL 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 |
 |
|
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_PhoneIDUPDATE PhoneStatus SET RecordEndDate = GETDATE()WHERE PhoneID = 126If I execute the following query, which includes the new FROM, I still have the same problem with the index not usedUPDATE PhoneStatus SET RecordEndDate = GETDATE()FROM Cust_Profile.PhoneStatus WHERE PhoneID = 126But 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 = 126SQL 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 MVPhttp://visakhm.blogspot.com/ |
 |
|
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 |
 |
|
|