10 billion records? 10,000,000,000 records?And you ask why a CURSOR-based solution is slow?Try a SET-based solution! This suggesion also allows a parameter for Status so that you don't need three SP for same thing.CREATE PROCEDURE dbo.uspRptPhase( @YearMonth INT, @Status CHAR(1))ASSET NOCOUNT ONCREATE TABLE #RptPhase ( gpo_id BIGINT, PaidMO BIGINT )IF @Status IS NULL INSERT #RptPhase ( gpo_id, PaidMO ) SELECT gpo_id, COUNT(mno) FROM masterdata WHERE year_month = @YearMonth AND service_id = 'B' AND status IS NULL GROUP BY gpo_idELSE IF @Status IN ('B', 'D') INSERT #RptPhase ( gpo_id, PaidMO ) SELECT gpo_id, COUNT(mno) FROM masterdata WHERE year_month = @YearMonth AND service_id = 'B' AND status = @Status GROUP BY gpo_idELSE BEGIN DROP TABLE #RptPhase RETURN ENDCREATE NONCLUSTERED INDEX IX_RptPhase ON #RptPhase (gpo_id, PaidMO)UPDATE tSET t.PaidMO = w.PaidMO FROM rptPhase AS tINNER JOIN #RptPhase AS w ON w.gpo_id = t.CodeWHERE t.YearMonth = @YearMonthDROP TABLE #RptPhaseGO
You easily supply 'B', 'D' or NULL as parameter for @status and there is no longer a need for three different stored procedures.
E 12°55'05.63"N 56°04'39.26"