Как избежать курсоров в SQL Server - PullRequest
0 голосов
/ 04 октября 2018

У меня есть таблица с именем tmp_emp_city с empid, recordcount и cityid.Я выбираю empid, recordcount и cityid из tmp_emp_city и извлекаю информацию о каждом сотруднике с помощью cityid и count из таблицы sulekhasmartmaster_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_v1 и sulekhasmartmaster_v1 имеет количество записей, превышающее 4 миллиона строк в двух таблицах.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...