tpavan7329
Starting Member
18 Posts |
Posted - 2011-03-11 : 01:45:02
|
Hi, I want to optimize below Store Procedure and try to make it run faster and minimize of using temporary tables. Can any one help me please? Alter proc [dbo].[bsp_DRProcess_1GetData]Asdeclare @startDate datetimedeclare @endDate datetimeset @startDate= getdate() - 1set @endDate= getdate() select @startdate, @enddate/*** START.1: Combine AFP & CORE Records ****/--AFP recordsselect m.myphoneid as afp_myphoneid,m.destination as afp_destination,mh.id as afp_mhid,mh.statusCode as afp_statusCode,mh.reasoncode as afp_reasonCode,mh.statusText as afp_statusText,mh.aggregatorStatusCode as afp_aggregatorStatusCode,mh.aggregatorReasonCode as afp_aggregatorReasonCode,mh.aggregatorStatusText as afp_aggregatorStatusText,mh.created as afp_CreationDateinto #temp_afp_all_historyfrom [ATLANTIS].[AFP].[dbo].[messageHistory] mh, [ATLANTIS].[AFP].[dbo].[message] m with (nolock)where mh.msgTableId=m.id and responsetype=7 and m.created between @startDate and @endDate--AFP records with most recent messages (id) per myphoneidselect afp.* into #temp_afp_allfrom #temp_afp_all_history afpinner join (select afp_myphoneid,max(afp_mhid) as afp_mhid from #temp_afp_all_history group by afp_myphoneid) afp_laston afp.afp_mhid=afp_last.afp_mhid--AFP+CORE.BCN through myphoneidSelect bcn.destinator as msisdn,bcn.mcc, bcn.BillingCycleNotificationId,bcn.NotificationType,bcn.Myphoneid,bcn.BillingRequestStateId, bcn.status as bcn_Status,bcn.creationdate as bcn_CreationDate, bcn.CreatedBy as bcn_CreatedBy,bcn.UpdatedBy as bcn_UpdatedBy --SH100610,bcn.DrReturnCode as bcn_DrReturnCode, bcn.DrReasonText as bcn_DrReasonText,afp.*into #temp_bcn_afp_all_historyfrom CORE..BillingCycleNotification bcn with (nolock)inner join #temp_afp_all afp on convert(nvarchar,afp.afp_myphoneid)=convert(nvarchar,bcn.myphoneid)--Most recent BCNs per myphoneidselect ba.* into #temp_bcn_afp_allfrom #temp_bcn_afp_all_history bainner join (SELECT max(billingCycleNotificationId) as billingCycleNotificationId FROM #temp_bcn_afp_all_history group by myphoneid) bamax on (ba.billingCycleNotificationId=bamax.billingCycleNotificationid)--Comments--1. Combine above quries into one--BCN+BRselect br.BillingRequestId, br.status as br_status,br.creationdate as br_CreationDate, br.CreatedBy as br_CreatedBy --SH100610,br.UpdatedBy as br_UpdatedBy --SH100610,br.LastRetryDate, br.RetryScheduleLock, br.mnc, br.RetryCount ,bre.termId, bre.CustomerId, bre.BillingRequestEventId, bre.RequestType ,ptr.sessionId,ptr.CommandType,ptr.PersistentTaskRecordId --SH100621 added commandType,ba.* into #temp_brbcn_status_all_historyfrom #temp_bcn_afp_all bainner join CORE..BillingRequestState brs with (nolock) on brs.BillingRequestStateId=ba.BillingRequestStateIdinner join CORE..BillingRequest br with (nolock) on br.BillingRequestId = brs.BillingRequestIdinner join CORE..BillingRequestEvent bre with (nolock) on bre.BillingRequestEventId = br.BillingRequestEventIdinner join CORE..persistentTaskRecord ptr with (nolock) on ptr.billingRequestId=br.billingRequestId --3PG sessionId--Most recent myphoneid per BRselect bb.*into #temp_brbcn_status_all_history2from #temp_brbcn_status_all_history bbinner join (select max(Myphoneid) Myphoneid from #temp_brbcn_status_all_history group by BillingRequestId,NotificationType) bbmaxon bb.Myphoneid=bbmax.Myphoneid--Most recent PersistentTaskRecordId per BillingRequestId 3PG persistent issueselect bb.*into #temp_brbcn_status_allfrom #temp_brbcn_status_all_history2 bbinner join (select max(PersistentTaskRecordId) PersistentTaskRecordId from #temp_brbcn_status_all_history2 group by BillingRequestId) bbmaxon bb.PersistentTaskRecordId=bbmax.PersistentTaskRecordId |
|