Stored procedure; Insert slowness

I have an SP that takes 10 seconds to work about 10 times (about a second every time it starts). The platform is asp.net, and the server is SQL Server 2005. I indexed the table (and not on the PC), and this is not a problem. Some reservations:

  • usp_SaveKeyword is not a problem. I commented that the whole joint venture, and it did not matter.
  • I set @SearchID to 1, and the time was significantly reduced, only an average of about 15 ms per transaction.
  • I commented on the entire stored procedure except pasting into tblSearches, and, oddly enough, it took more time to execute.

Any ideas on what could happen?

set ANSI_NULLS ON

go

ALTER PROCEDURE [dbo].[usp_NewSearch]

  @Keyword VARCHAR(50),

  @SessionID UNIQUEIDENTIFIER,

  @time SMALLDATETIME = NULL,

  @CityID INT = NULL

AS

BEGIN

  SET NOCOUNT ON;

  IF @time IS NULL SET @time = GETDATE();



  DECLARE @KeywordID INT;

  EXEC @KeywordID = usp_SaveKeyword @Keyword;

  PRINT 'KeywordID : '

  PRINT @KeywordID

  DECLARE @SearchID BIGINT;     

  SELECT TOP 1 @SearchID = SearchID

    FROM tblSearches 

   WHERE SessionID = @SessionID

     AND KeywordID = @KeywordID;



  IF @SearchID IS NULL BEGIN

        INSERT INTO tblSearches

              (KeywordID, [time], SessionID, CityID)

         VALUES

              (@KeywordID, @time, @SessionID, @CityID)

        SELECT Scope_Identity();

  END

  ELSE BEGIN

        SELECT @SearchID

  END



END
+3
source share
4 answers

top 1 @SearchID max (SearchID) where exists ? top , . , , .

SELECT TOP 1 @SearchID = SearchID    
  FROM tblSearches    
 WHERE SessionID = @SessionID     
   AND KeywordID = @KeywordID;

- - . -, -

select @SearchID = isnull (max (SearchID), -1)
  from tblSearches
 where SessionID = @SessionID
   and KeywordID = @KeywordID

( -) .

+2

"Display Estimated Execution Plan" SQL Management Studio - , ? , ( ). " " - , - -.

, , , SQL . : tblSearches?

+2

!

.

+1
  • tblSearches? , .
  • ?
  • ?
  • , ?
+1

Source: https://habr.com/ru/post/1697380/


All Articles