CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2003
    Location
    Athens, Greece
    Posts
    1,094

    Speed of an Insertion

    I am using SQL server 2000.
    I am using a stored procedure to make a creation of a specific object.
    This involves at most three INSERT statements and two SELECTS in a transaction.
    The problem is that every time the stored procedure is executed for the 1st time on the server it takes a lot of time to complete. The next insertions are almost instantaneous - I guess because the execution plan had been stored.
    Is there a way to load the execution plan before the first object creation? Or should I search for a way to speed up my creation stored procedure?
    Extreme situations require extreme measures

  2. #2
    Join Date
    Jun 2005
    Posts
    1,255

    Re: Speed of an Insertion

    There is a very clear and informative article about tuning SQL server's stored procedure at http://www.sql-server-performance.co...procedures.asp.

  3. #3
    Join Date
    Apr 2003
    Location
    Athens, Greece
    Posts
    1,094

    Re: Speed of an Insertion

    Nice article. Well, I guess I will try to measure the performance and find where exactly there is the problem.
    Thanx.
    Extreme situations require extreme measures

  4. #4
    Join Date
    Apr 2003
    Location
    Athens, Greece
    Posts
    1,094

    Re: Speed of an Insertion

    Ok I found the bottleneck:
    my creation stored procedure calls another one which takes around 80% of the execution time.
    This is the actual procedure.
    Code:
    CREATE PROC prc_dic_CreatePhrase
    @Phrase 	TPhrase,
    @LangAbbr 	TLangAbbr,
    @id TID OUTPUT
    AS
    	SET NOCOUNT ON
    
    	-- Fix the phrase quotes (make them double for the query)
    	SET @Phrase = Replace(@Phrase, '''', '''''')
    
    	-- Attempt to Insert the new phrase (the scope ID should be retrieved in the same batch)
    	DECLARE @Insert_stmt TStmt	
    	SET @Insert_stmt =	'SET @ScopeID = -1
    				INSERT INTO DIC_' + @LangAbbr + ' ([phrase]) VALUES (N''' + @Phrase + ''')
    				IF (@@ERROR = 0)
    				BEGIN
    				 	SELECT @ScopeID = SCOPE_IDENTITY() FROM DIC_' + @LangAbbr +'
    				END'
    
    	EXECUTE sp_executesql @Insert_stmt, N'@ScopeID int OUTPUT', @ScopeID = @id OUTPUT
    
    	SET NOCOUNT OFF
    TStmt is nvarchar(4000), TPhrase is nvarchar(250), TLangAbbr is char(2), TID is int. This takes around 5.5 seconds on clean cache, and around 1 sec when it has been executed before, to execute on my PC where the database table already has more than 2.000.000 records. The table has the following fields: an ID (indexed) and the phrase (PK, clustered). I need to keep these indexes. Is there a way to speed up the INSERTion?
    Extreme situations require extreme measures

  5. #5
    Join Date
    Jul 2004
    Location
    Jakarta, Indonesia
    Posts
    596

    Re: Speed of an Insertion

    ...to execute on my PC where the database table already has more than 2.000.000 records.
    try BULK INSERT instead and remove the index while inserting then re-index again..i'm not sure how but i read somewhere to use that kind of method

    and using Unicode datatype tooks 2 time more processing then using non-Unicode..i read it somewhere too

    if u figure it out, plz post the answer ok..

    thx..

    1st NF - a table should not contain repeating groups.
    2nd NF - any fields that do not depend fully on the primary key should be moved to another table.
    3rd NF - there should be no dependency between non key fields in same table.
    - E. Petroutsos -


    eRiCk

    A collection of "Laku-abis" Ebook, Permanent Residence

    Access Reserved Words, a Classic Form Bug, Access Limitation, Know run Process and the Lock they hold in, Logging User Activity in MSSQL

  6. #6
    Join Date
    Apr 2003
    Location
    Athens, Greece
    Posts
    1,094

    Re: Speed of an Insertion

    Actually it was not the INSERT!!
    It was the FROM Clause in the SELECT SCOPE_IDENTITY() statement. When I changed the following:
    Code:
    SELECT @ScopeID = SCOPE_IDENTITY() FROM DIC_' + @LangAbbr +'
    To:
    Code:
    SELECT @ID = @@IDENTITY
    or
    Code:
    SELECT @ScopeID = SCOPE_IDENTITY()
    The execution time changed dramatically (on a clean cache).

    Anyone has a clue why omitting the FROM <table> changed performance dramatically?
    Last edited by panayotisk; July 29th, 2005 at 05:13 AM.
    Extreme situations require extreme measures

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured