sql - select query in Cursor taking too long -


i have designed cursor run stats against 6500 inspectors taking long. there many other select queries in cursor running okay following select running very slow. without cursor select query running fine.

requirements:

number of visits each inspectors visits has uploaded document (1 or 2 or 13)

tables:

  • inspectors: inspectorid
  • inspectionscope: scopeid, inspectorid (fk)
  • visits: visitid, visitdate scopeid (fk)
  • visitsdoc: docid, doctype, visitid (fk)

cursor code:

declare          @curinspid int,          @datefrom date, @dateto date;  select @dateto = cast(getdate() date)        ,@datefrom = cast(getdate() - 90 date)   declare           @inspectorid int,         @totalvisits int;   declare @report table (         inspectorid int,         totalvisits int)   declare curlist cursor     select inspectorid inspectors ;           open curlist fetch next curlist @curinspid;  while @@fetch_status = 0 begin  select      @curinspid = s.inspectorid         ,@totalvisits = count(distinct v.visitid) visits v  inner join inspectionscope s on s.scopeid = v.scopeid  inner join visitdocs vd on vd.visitid = v.visitid  s.inspectorid = @curinspid , vd.doctype in (1,2,13)  , v.visitdate between @datefrom , @dateto group s.inspectorid    insert @report values(@curinspid,@totalvisits);  fetch next curlist @curinspid; end  close curlist deallocate curlist  select * @report 

following queries run okay inside same cursor

    ,@totalvisitswithreportscore = (select count(v.visitid) visits v                         inner join inspectionscope s on s.scopeid = v.scopeid                          v.reportstandard not in (0,9) , v.visittype = 1                             , v.visitdate between @datefrom , @dateto                             , s.inspectorid = @curinspid                              )      ,@totalvisitswith_reportscore_rejectionfeedback = (select count(v.visitid) visits v                         inner join inspectionscope s on s.scopeid = v.scopeid                          v.reportstandard not in (0,9) , v.visittype = 1                             , v.discrepancytype in (2,5,6,7,8)                             , v.visitdate between @datefrom , @dateto                             , s.inspectorid = @curinspid                      ) 

no need cursor here -- can use insert into select, joining on inspector table.

insert @report  select      s.inspectorid         , count(distinct v.visitid) visits v      inner join inspectionscope s on s.scopeid = v.scopeid      inner join visitdocs vd on vd.visitid = v.visitid      inner join inspector on s.inspectorid = i.inspectorid  vd.doctype in (1,2,13)  , v.visitdate between @datefrom , @dateto group s.inspectorid  

please note, may need use outer join inspector table if there results in table not exist in other tables. depends on data , desired results.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -