如何避免在SQL Server中的游标

时间:2018-10-04 07:44:53

标签: sql sql-server tsql ssms sqlperformance

我有一个名为tmp_emp_city的表,其中有empidrecordcountcityid。我从empid中选择recordcountcityidtmp_emp_city,并从cityid中使用countsulekhasmartmaster_v1获取每个员工的信息桌子。

我使用光标进行了此操作,但是这花费了很多时间(超过一分钟)。如何减少时间?

tmp_emp_city

empid   recordcount  cityid
10053       400        1
10109       400        2
10583       400        3
10689       400        1
10788       400        2
10815       400        1
10892       400        3
10921       400        1
10977       400        21
10989       400        1

我的代码是

---alter proc prc_get_dcp_leads_emp
declare 
    @empid int=null, 
    @cityname varchar(100) = 'Chennai',
    @comments varchar(16) = 'ACQ',
    @reccnt int = 400 
--as
begin
set nocount on
Declare @count int,
        @city varchar(200)
--11138
if @comments ='ACQ'
begin
    if(object_id('tmp_emp_city')) is not null 
        drop table tmp_emp_city

    select  
        a.empid,
        b.cityid,
        b.cityname,
        0 as grade,
        0 as subcategoryid,
        @reccnt as recordcount 
    into 
        tmp_emp_city 
    from 
        tbl_automode_Executives a(nolock) 
        join 
        venus..YPcities b 
            on  a.execDCPLocation = b.cityname
    where 
        b.cityname = isnull(@cityname,b.cityname) and 
        a.empid = isnull(@empid,a.empid) and 
        a.comments = @comments  and 
        a.isactive = 1
end
else
begin         
    if(object_id('#tmp_emp_city')) is not null 
        drop table #tmp_emp_city        

    select  
        c.empid,
        b.cityid,
        b.cityname,
        0 as grade,
        0 as subcategoryid 
    into 
        #tmp_emp_city 
    from 
        Venus..ypfexec_city a with(nolock) 
        join 
        smart2018..tbl_tme_city  b with(nolock) 
            on  a.cityid =  b.novacityid
        join 
        smart2018..tbl_automode_Executives c(nolock) 
            on  c.userid = a.execid   
    where 
        b.category= @comments and 
        c.empid = isnull(@empid,c.empid) and 
        a.isactive = 1  and 
        c.isactive = 1

    if(object_id('tmp_emp_city')) is not null 
        drop table tmp_emp_city 

    select 
        a.empid,
        a.cityid,
        a.cityname,
        a.grade,
        a.subcategoryid,
        round(b.recordcount,0) as recordcount 
    into 
        tmp_emp_city 
    from 
        #tmp_emp_city a 
        join 
        (select 
            empid,
            @reccnt/cast(count(cityid) as float) as recordcount 
        from 
            #tmp_emp_city 
        group by 
            empid)b
        on a.empid = b.empid

    drop table #tmp_emp_city 
end

select 
    @empid = 0,
    @count = 0,
    @city = 0

delete a 
from 
    tbl_emp_assign_lead a(nolock) 
    join 
    tmp_emp_city b(nolock) 
        on  a.assignedempid = b.empid and 
            a.cityid = b.cityid 
where 
    a.category = @comments

declare cur cursor for 
select distinct 
    empid,
    recordcount,
    cityid 
from 
    tmp_emp_city(nolock)

open cur
fetch next from cur 
into @empid,@count,@city

while @@fetch_status=0
begin
    insert into 
        tbl_emp_assign_lead(
            leadid,
            assignedempid,
            interactionstatus,
            followupdate,
            cityid,
            leadpriority,
            category)
    select distinct top (@count) 
        ms.leadid,
        ms.interaction_empid as assignedempid,
        ms.interactionstatus,
        ms.followupdate,
        ms.cityid,
        ms.leadpriority,@comments 
    from
        (select 
            a.interaction_empid,
            a.leadid,
            a.interactionstatus,
            a.interactionid,
            a.interaction_date,
            a.cityid,
            case 
                when a.interactionid = 7 then a.followupdate 
                else null end as followupdate,
            a.leadsourceid,
            a.leadsource, 
            case 
                when a.interactionid = 7 and 
                     a.leadsourceid in (7,19,22) and 
                     convert(date,a.followupdate) = convert(date,getdate()+1)               
                     then 1 --today followup
                when a.interactionid = 7 and 
                     a.leadsourceid in (7,19,22) and  
                     convert(date,a.followupdate) >= convert(date,getdate()-30) and 
                     convert(date,a.followupdate) < convert(date,getdate()+1) 
                     then 2  --missed followup
                     else 0               
                     end as 'leadpriority' 
        from 
            sulekhasmartmaster_v1 a(nolock)  
        where 
            a.interaction_empid > 0  and 
            a.leadsourceid != 6 and 
            a.interaction_empid =@empid and 
            a.cityid =@city and 
            a.interactionid = 7

        union 

        select 
            @empid as interaction_empid,
            a.leadid,
            a.interactionstatus,
            a.interactionid,
            a.interaction_date,
            a.cityid,
            null as followupdate,
            a.leadsourceid,
            a.leadsource, 
            3 as 'leadpriority'        --interested leads
        from  
            sulekhasmartmaster_v1 a(nolock)  
        where   
            isnull(a.interactionid,0) =0  and  
            a.cityid =@city AND 
            a.leadsourceid =9

        union

        select 
            @empid as interaction_empid,
            a.leadid,
            a.interactionstatus,
            a.interactionid,
            a.interaction_date,
            a.cityid,
            null  as followupdate,
            a.leadsourceid,
            a.leadsource, 
            4 as 'leadpriority'       -- online missed leads
        from  
            sulekhasmartmaster_v1 a(nolock)  
        where  
            isnull(a.interactionid,0) =0 and  
            a.cityid =@city and 
            a.leadsourceid =19

        union

        select 
            @empid as interaction_empid,
            a.leadid,
            b.interactionstatus,
            b.interactionid,
            b.interaction_date,a.cityid,
            null  as followupdate,
            a.leadsource_id,
            b.leadsource, 
            5 as 'leadpriority'  --category based
        from 
            sulekhasmartscoredetails_v1 a(nolock) 
            join 
            sulekhasmartmaster_v1 b(nolock) 
                on  a.leadid = b.leadid 
        where 
            a.publishscore > 0 and 
            a.interaction_id not in (1, 3, 4, 6, 7, 8, 11, 12, 13, 14, 17, 
                                     24, 26, 27, 28, 31,34, 35, 38) and 
            a.leadsource_id not in (6,9,19) and 
            a.subcategoryid in 
                (select 
                    subcategoryid  
                from 
                    tbl_aopcategory 
                where 
                    salescluster != 'LT' ) and 
            a.cityid = @city and 
            b.interaction_empid is null) ms 
        left join 
        tbl_emp_assign_lead b(nolock)
            on  ms.leadid = b.leadid
    where 
        ms.leadpriority>0 and  
        b.leadid is null
    order by 
        ms.leadpriority

    fetch next from cur into @empid,@count,@city
end

close cur
deallocate cur 

set nocount off
end

sulekhasmartscoredetails_v1sulekhasmartmaster_v1在两个表中的记录计数超过400万行。

0 个答案:

没有答案