Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update query taking long time
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

1 Posts

Posted - 11/04/2013 :  16:55:57  Show Profile  Reply with Quote

I am trying to update a table with around 500 million rows. The column i am updating is Cost field. Please see the below listed query. The user defined function runs in less than a minute, so its not the issue. But the update statement is taking around 17 -n 18 hours to complete. I have created a index on BuyID,PageID which are the columns used in the where clause of update statement. Can some one please tell me what i need to do to optimize the query?



declare @year int,@month int,@StartDate DateTime,@EndDate DateTime,@Threshold int
SET @year=2013
SET @month=8

SET @StartDate= (Select dateadd(month, @Month - 1, dateadd(year, @Year - 1900, 0)))
SET @EndDate= (Select dateadd(month, @Month,dateadd(year, @Year - 1900, -1)))

select EffectiveCPMRate,DFACampaignId,DFAPlacementId into #CPM
from dbo.udf_GadgetPlacementEffectiveCPMRateForDateRange(@StartDate, @EndDate)

update CT
set CT.[Cost] = ISNULL(((ISNULL(CP.[EffectiveCPMRate],0)/1000)*CT.[Imps]),0)
from Kraft.MonthlySummaryData CT Inner join #CPM CP
on CT.BuyID = CP.DFACampaignId and CT.[PageID] = CP.DFAPlacementId

Edited by - tkizer on 11/04/2013 16:58:17

Almighty SQL Goddess

38200 Posts

Posted - 11/04/2013 :  17:00:21  Show Profile  Visit tkizer's Homepage  Reply with Quote
How many rows are there in #CPM? And how many rows match those rows in Kraft.MonthlySummaryData? What does the execution plan show? Is it blocked at times? Have you tried adding an index to #CPM for DFACampaignId,DFAPlacementId?

Tara Kizer
SQL Server MVP since 2007
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000