Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [mk].[ListView](@from int,@to int,@userId bigint,@status varchar(max),@dateCount  int,@authorityList varchar(max),@stratDate date,@endDate date)

as
begin

declare @geofence varchar(max);
declare @exclusionFence varchar(max);
declare @dataTable1 table(ID bigint,UserId varchar(max),EventTypeID int,Description varchar(max),Latitude float,Longitude float,PicturePath varchar(max),DateTime datetime,Location varchar(max),Status int,MediaType int,AssignTo varchar(max),Department int,Comment varchar(max),AssignedEmployeeID int,Type varchar(max),IconPath varchar(max),RW int);
declare cursor1 CURSOR FOR select geofence from dbo.Area where AuthorityId in (SELECT value FROM STRING_SPLIT(@authorityList, ','));
declare cursor2 CURSOR FOR select ExclusionFence from dbo.Area where AuthorityId in (SELECT value FROM STRING_SPLIT(@authorityList, ','));

open cursor1
open cursor2



FETCH NEXT FROM  cursor1
INTO @geofence
FETCH NEXT FROM cursor2
INTO @exclusionFence


WHILE @@FETCH_STATUS =0
BEGIN




DECLARE @g geometry;  
DECLARE @ex geometry;  

SET @g = geometry::STGeomFromText('POLYGON(('+ @geofence +'))',4036);  
SET @ex = geometry::STGeomFromText('POLYGON(('+ @exclusionFence +'))',4036);  

SET NOCOUNT ON;


 if (@exclusionFence !=NULL)
 begin

	          if(@status!=-1)
              begin

        WITH Events_CTE (ID,USERID,[EventTypeID],[Description],[Latitude],[Longitude] 

             ,[PicturePath],[DateTime],[Location],[Status],[MediaType],[AssignTo],[Department],[Comment],AssignedEmployeeID,[Type],IconPath,RW)

             AS(
	            SELECT * FROM( select e.*,et.Type,et.IconPath , ROW_NUMBER() over(order by e.DateTime desc)rw

		       from Event e join EventType et on e.EventTypeID=et.ID  

		       where @g.STContains(geometry::STGeomFromText('POINT('+CAST(Latitude AS nvarchar(max))  +' '+CAST(Longitude AS nvarchar(max)) +')', 4036)) =1

			   and

			  (e.DateTime >= DATEADD(day,@dateCount, GETDATE() ) or e.DateTime  BETWEEN @stratDate and @endDate)

		       and  e.Status=@status)x    WHERE x.rw BETWEEN @from AND @to )

			   --

          insert into    @dataTable1  SELECT * FROM Events_CTE O

               where 

	         @ex.STContains(geometry::STGeomFromText('POINT('+CAST(O.Latitude AS nvarchar(max))  +' ' +CAST(O.Longitude AS nvarchar(max)) +')', 4036)) <>1


             FETCH NEXT FROM cursor1 INTO @geofence
             FETCH NEXT FROM cursor2 INTO  @exclusionFence
 end
 

 else
 begin
        WITH Events_CTE (ID,USERID,[EventTypeID],[Description],[Latitude],[Longitude] ,

       [PicturePath],[DateTime],[Location],[Status],[MediaType],[AssignTo],[Department],[Comment],AssignedEmployeeID,[TYPE],ICONPATH,RW)

       AS(
             SELECT * FROM( select e.*,et.Type,et.IconPath , ROW_NUMBER() over(order by e.DateTime desc)rw from Event e 

             join EventType et on e.EventTypeID=et.ID

             where 

			  (e.DateTime >= DATEADD(day,@dateCount, GETDATE() ) or e.DateTime  BETWEEN @stratDate and @endDate)

			  and 
			 
			 @g.STContains(geometry::STGeomFromText('POINT('+CAST(Latitude AS nvarchar(max))  +' ' +CAST(Longitude AS nvarchar(max)) +')', 4036)) =1

             )x WHERE x.rw BETWEEN @from AND @to)

     insert into    @dataTable1   SELECT * FROM Events_CTE O

      where 

	  @ex.STContains(geometry::STGeomFromText('POINT('+CAST(O.Latitude AS nvarchar(max))  +' ' +CAST(O.Longitude AS nvarchar(max)) +')', 4036)) <>1


      FETCH NEXT FROM cursor1 INTO @geofence
      FETCH NEXT FROM cursor2 INTO  @exclusionFence
 end

 END
 ELSE
  begin

 if(@status!=-1)
 begin

             WITH Events_CTE (ID,USERID,[EventTypeID],[Description],[Latitude],[Longitude] ,[PicturePath],[DateTime],[Location],

             [Status],[MediaType],[AssignTo],[Department],[Comment],AssignedEmployeeID,[TYPE],ICONPATH,RW)

             AS(

		      SELECT * FROM( select e.*,et.Type,et.IconPath , ROW_NUMBER() over(order by e.DateTime desc)rw

		     from Event e join EventType et on e.EventTypeID=et.ID 
			 
			 where @g.STContains(geometry::STGeomFromText('POINT('+CAST(Latitude AS nvarchar(max))  +' ' +CAST(Longitude AS nvarchar(max)) +')', 4036)) =1

			and

			 (e.DateTime >= DATEADD(day,@dateCount, GETDATE() ) or e.DateTime  BETWEEN @stratDate and @endDate)

			  and

		    e.Status=@status)x WHERE x.rw BETWEEN @from AND @to )


          insert into    @dataTable1     SELECT * FROM Events_CTE 

             FETCH NEXT FROM cursor1 INTO @geofence
             FETCH NEXT FROM cursor2 INTO  @exclusionFence
 end 

 else

 begin

              WITH Events_CTE (ID,USERID,[EventTypeID],[Description],[Latitude],[Longitude] ,[PicturePath],[DateTime],

              [Location],[Status],[MediaType],[AssignTo],[Department],[Comment],AssignedEmployeeID,[TYPE],ICONPATH,RW)
           AS(
              SELECT * 
                     FROM( 
                          select e.*,et.Type,et.IconPath , ROW_NUMBER() over(order by e.DateTime desc)rw

                             from Event e 

                        join EventType et on e.EventTypeID=et.ID  
						
						where 

					    (e.DateTime >= DATEADD(day,@dateCount, GETDATE() ) or e.DateTime  BETWEEN @stratDate and @endDate)

						 and
	@g.STContains(geometry::STGeomFromText('POINT('+CAST(Latitude AS nvarchar(max))  +' ' +CAST(Longitude AS nvarchar(max)) +')', 4036)) =1
 
 
            )x
                          WHERE x.rw BETWEEN @from AND @to)

                    insert into    @dataTable1   SELECT * FROM Events_CTE

 FETCH NEXT FROM cursor1 INTO @geofence
 FETCH NEXT FROM cursor2 INTO  @exclusionFence
 end

 END
 
end
CLOSE cursor1;
CLOSE cursor2;
DEALLOCATE cursor1;
DEALLOCATE cursor2;

select DISTINCT * from @dataTable1

end


What I have tried:

it tackes 1.45 minit i want to reduce this execution time
Posted
Updated 12-Sep-22 21:50pm

1 solution

Start with the execution plan:

Connect SSMS to the database and open a new query.
ype the stored procedure name in the query window, and right click - then pick "Display Estimated Execution Plan" from the pop-up menu.

The plan will show you the percentage time each part takes and allow you to see where the time is taken and to focus on that area of your SP.
 
Share this answer
 
Comments
Lahiru Mendis 13-Sep-22 6:04am    
/*Missing Index Details from SQLQuery14.sql - tcp:getconnectz.database.windows.net.ManKiwwa (getconnectz (66))
The Query Processor estimates that implementing the following index could improve the query cost by 56.2817%.
*/

/*
USE [ManKiwwa]
GO
CREATE NONCLUSTERED INDEX [<name of="" missing="" index,="" sysname,="">]
ON [dbo].[Event] ([Status],[DateTime])
INCLUDE ([UserID],[EventTypeID],[Description],[Latitude],[Longitude],[PicturePath],[Location],[MediaType],[AssignTo],[Department],[Comment],[AssignedEmployeeID])
GO
*/
OriginalGriff 13-Sep-22 6:30am    
And?
Did it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900