Insert into table with identity insert taking almost a second












1















We have an oltp application and I am trying to troubleshoot why insert into one of our table is taking close to a second. Not all inserts are taking this much time but there are some which does take close to a second and since this insert is part of business transaction which can only take around 200ms, a one second insert is a problem.



This table has a primary key which is identity and is nonclustered. Clustered index is another column.



The insert stored procedure is very simple. Something like



Create proc usptable_insert
( a bunch of parameters)
as
insert into table (col1, col2 ........)
values (parameters1, parameter2...... etc)
DECLARE @Id INT = (SELECT SCOPE_IDENTITY());
SET IDENTITY_INSERT [table2] ON
insert into table2 (col1, col2 ........)
values (@Id, parameter2...... etc
SET IDENTITY_INSERT [table2] OFF


We are in the process of migrating from table1 to table2 hence inserting into two tables and using identity insert to make sure table2 has same keys.



First two rows are the the insert statements
enter image description here



Initially I thought it was because of latch contention because of hotspot issue on the last page. But I tried to reproduce this in pre-prod by running this stored procedure through sqlstress with 100 threads and couldn't.



If I am not wrong this behaviors cannot be blocking as its an insert and unless something is blocking the whole table which is not the case.



What can be other reasons for this behavious?










share|improve this question














bumped to the homepage by Community 14 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 2





    Since your clustered index is not the Identity column AND since you indicated that not every insert takes a long time, you may be experiencing delays due to page splits on the clustered index. There are numerous ways to diagnose page splits - Try searching the Internet.

    – Scott Hodgin
    Sep 25 '18 at 12:22











  • That makes sense. I havent pursuit that angle, but since it did not come up in pre-prod, i am less hopeful. Thanks for the comment.

    – ilovesql
    Sep 25 '18 at 20:56











  • Tested in pre-prod by doing inserts that would cause pagesplit, but that did not bring up the execution count to a second.

    – ilovesql
    Sep 26 '18 at 1:57
















1















We have an oltp application and I am trying to troubleshoot why insert into one of our table is taking close to a second. Not all inserts are taking this much time but there are some which does take close to a second and since this insert is part of business transaction which can only take around 200ms, a one second insert is a problem.



This table has a primary key which is identity and is nonclustered. Clustered index is another column.



The insert stored procedure is very simple. Something like



Create proc usptable_insert
( a bunch of parameters)
as
insert into table (col1, col2 ........)
values (parameters1, parameter2...... etc)
DECLARE @Id INT = (SELECT SCOPE_IDENTITY());
SET IDENTITY_INSERT [table2] ON
insert into table2 (col1, col2 ........)
values (@Id, parameter2...... etc
SET IDENTITY_INSERT [table2] OFF


We are in the process of migrating from table1 to table2 hence inserting into two tables and using identity insert to make sure table2 has same keys.



First two rows are the the insert statements
enter image description here



Initially I thought it was because of latch contention because of hotspot issue on the last page. But I tried to reproduce this in pre-prod by running this stored procedure through sqlstress with 100 threads and couldn't.



If I am not wrong this behaviors cannot be blocking as its an insert and unless something is blocking the whole table which is not the case.



What can be other reasons for this behavious?










share|improve this question














bumped to the homepage by Community 14 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.











  • 2





    Since your clustered index is not the Identity column AND since you indicated that not every insert takes a long time, you may be experiencing delays due to page splits on the clustered index. There are numerous ways to diagnose page splits - Try searching the Internet.

    – Scott Hodgin
    Sep 25 '18 at 12:22











  • That makes sense. I havent pursuit that angle, but since it did not come up in pre-prod, i am less hopeful. Thanks for the comment.

    – ilovesql
    Sep 25 '18 at 20:56











  • Tested in pre-prod by doing inserts that would cause pagesplit, but that did not bring up the execution count to a second.

    – ilovesql
    Sep 26 '18 at 1:57














1












1








1








We have an oltp application and I am trying to troubleshoot why insert into one of our table is taking close to a second. Not all inserts are taking this much time but there are some which does take close to a second and since this insert is part of business transaction which can only take around 200ms, a one second insert is a problem.



This table has a primary key which is identity and is nonclustered. Clustered index is another column.



The insert stored procedure is very simple. Something like



Create proc usptable_insert
( a bunch of parameters)
as
insert into table (col1, col2 ........)
values (parameters1, parameter2...... etc)
DECLARE @Id INT = (SELECT SCOPE_IDENTITY());
SET IDENTITY_INSERT [table2] ON
insert into table2 (col1, col2 ........)
values (@Id, parameter2...... etc
SET IDENTITY_INSERT [table2] OFF


We are in the process of migrating from table1 to table2 hence inserting into two tables and using identity insert to make sure table2 has same keys.



First two rows are the the insert statements
enter image description here



Initially I thought it was because of latch contention because of hotspot issue on the last page. But I tried to reproduce this in pre-prod by running this stored procedure through sqlstress with 100 threads and couldn't.



If I am not wrong this behaviors cannot be blocking as its an insert and unless something is blocking the whole table which is not the case.



What can be other reasons for this behavious?










share|improve this question














We have an oltp application and I am trying to troubleshoot why insert into one of our table is taking close to a second. Not all inserts are taking this much time but there are some which does take close to a second and since this insert is part of business transaction which can only take around 200ms, a one second insert is a problem.



This table has a primary key which is identity and is nonclustered. Clustered index is another column.



The insert stored procedure is very simple. Something like



Create proc usptable_insert
( a bunch of parameters)
as
insert into table (col1, col2 ........)
values (parameters1, parameter2...... etc)
DECLARE @Id INT = (SELECT SCOPE_IDENTITY());
SET IDENTITY_INSERT [table2] ON
insert into table2 (col1, col2 ........)
values (@Id, parameter2...... etc
SET IDENTITY_INSERT [table2] OFF


We are in the process of migrating from table1 to table2 hence inserting into two tables and using identity insert to make sure table2 has same keys.



First two rows are the the insert statements
enter image description here



Initially I thought it was because of latch contention because of hotspot issue on the last page. But I tried to reproduce this in pre-prod by running this stored procedure through sqlstress with 100 threads and couldn't.



If I am not wrong this behaviors cannot be blocking as its an insert and unless something is blocking the whole table which is not the case.



What can be other reasons for this behavious?







sql-server performance sql-server-2014






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 25 '18 at 11:55









ilovesqlilovesql

61




61





bumped to the homepage by Community 14 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 14 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.










  • 2





    Since your clustered index is not the Identity column AND since you indicated that not every insert takes a long time, you may be experiencing delays due to page splits on the clustered index. There are numerous ways to diagnose page splits - Try searching the Internet.

    – Scott Hodgin
    Sep 25 '18 at 12:22











  • That makes sense. I havent pursuit that angle, but since it did not come up in pre-prod, i am less hopeful. Thanks for the comment.

    – ilovesql
    Sep 25 '18 at 20:56











  • Tested in pre-prod by doing inserts that would cause pagesplit, but that did not bring up the execution count to a second.

    – ilovesql
    Sep 26 '18 at 1:57














  • 2





    Since your clustered index is not the Identity column AND since you indicated that not every insert takes a long time, you may be experiencing delays due to page splits on the clustered index. There are numerous ways to diagnose page splits - Try searching the Internet.

    – Scott Hodgin
    Sep 25 '18 at 12:22











  • That makes sense. I havent pursuit that angle, but since it did not come up in pre-prod, i am less hopeful. Thanks for the comment.

    – ilovesql
    Sep 25 '18 at 20:56











  • Tested in pre-prod by doing inserts that would cause pagesplit, but that did not bring up the execution count to a second.

    – ilovesql
    Sep 26 '18 at 1:57








2




2





Since your clustered index is not the Identity column AND since you indicated that not every insert takes a long time, you may be experiencing delays due to page splits on the clustered index. There are numerous ways to diagnose page splits - Try searching the Internet.

– Scott Hodgin
Sep 25 '18 at 12:22





Since your clustered index is not the Identity column AND since you indicated that not every insert takes a long time, you may be experiencing delays due to page splits on the clustered index. There are numerous ways to diagnose page splits - Try searching the Internet.

– Scott Hodgin
Sep 25 '18 at 12:22













That makes sense. I havent pursuit that angle, but since it did not come up in pre-prod, i am less hopeful. Thanks for the comment.

– ilovesql
Sep 25 '18 at 20:56





That makes sense. I havent pursuit that angle, but since it did not come up in pre-prod, i am less hopeful. Thanks for the comment.

– ilovesql
Sep 25 '18 at 20:56













Tested in pre-prod by doing inserts that would cause pagesplit, but that did not bring up the execution count to a second.

– ilovesql
Sep 26 '18 at 1:57





Tested in pre-prod by doing inserts that would cause pagesplit, but that did not bring up the execution count to a second.

– ilovesql
Sep 26 '18 at 1:57










1 Answer
1






active

oldest

votes


















0















If I am not wrong this behaviors cannot be blocking as its an insert
and unless something is blocking the whole table which is not the
case.




What is your isolation level? Read Committed?



If there are selects running on table or table2 then blocking could occur and slow down some of the inserts, as you are seeing.



You could test with read commited snapshot isolation level and use row versioning if blocking is the case.
You need to know the implications for your tempdb (version store will grow, long running open transactions can be problematic for your tempdb)



To enable RSSI:



ALTER DATABASE MyDatabase  
SET ALLOW_SNAPSHOT_ISOLATION ON

ALTER DATABASE MyDatabase
SET READ_COMMITTED_SNAPSHOT ON


Otherwise you could issue a profiler session to get more information on the issue. It is hard to answer (apart from obvious blocking) without more details.






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "182"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f218501%2finsert-into-table-with-identity-insert-taking-almost-a-second%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0















    If I am not wrong this behaviors cannot be blocking as its an insert
    and unless something is blocking the whole table which is not the
    case.




    What is your isolation level? Read Committed?



    If there are selects running on table or table2 then blocking could occur and slow down some of the inserts, as you are seeing.



    You could test with read commited snapshot isolation level and use row versioning if blocking is the case.
    You need to know the implications for your tempdb (version store will grow, long running open transactions can be problematic for your tempdb)



    To enable RSSI:



    ALTER DATABASE MyDatabase  
    SET ALLOW_SNAPSHOT_ISOLATION ON

    ALTER DATABASE MyDatabase
    SET READ_COMMITTED_SNAPSHOT ON


    Otherwise you could issue a profiler session to get more information on the issue. It is hard to answer (apart from obvious blocking) without more details.






    share|improve this answer




























      0















      If I am not wrong this behaviors cannot be blocking as its an insert
      and unless something is blocking the whole table which is not the
      case.




      What is your isolation level? Read Committed?



      If there are selects running on table or table2 then blocking could occur and slow down some of the inserts, as you are seeing.



      You could test with read commited snapshot isolation level and use row versioning if blocking is the case.
      You need to know the implications for your tempdb (version store will grow, long running open transactions can be problematic for your tempdb)



      To enable RSSI:



      ALTER DATABASE MyDatabase  
      SET ALLOW_SNAPSHOT_ISOLATION ON

      ALTER DATABASE MyDatabase
      SET READ_COMMITTED_SNAPSHOT ON


      Otherwise you could issue a profiler session to get more information on the issue. It is hard to answer (apart from obvious blocking) without more details.






      share|improve this answer


























        0












        0








        0








        If I am not wrong this behaviors cannot be blocking as its an insert
        and unless something is blocking the whole table which is not the
        case.




        What is your isolation level? Read Committed?



        If there are selects running on table or table2 then blocking could occur and slow down some of the inserts, as you are seeing.



        You could test with read commited snapshot isolation level and use row versioning if blocking is the case.
        You need to know the implications for your tempdb (version store will grow, long running open transactions can be problematic for your tempdb)



        To enable RSSI:



        ALTER DATABASE MyDatabase  
        SET ALLOW_SNAPSHOT_ISOLATION ON

        ALTER DATABASE MyDatabase
        SET READ_COMMITTED_SNAPSHOT ON


        Otherwise you could issue a profiler session to get more information on the issue. It is hard to answer (apart from obvious blocking) without more details.






        share|improve this answer














        If I am not wrong this behaviors cannot be blocking as its an insert
        and unless something is blocking the whole table which is not the
        case.




        What is your isolation level? Read Committed?



        If there are selects running on table or table2 then blocking could occur and slow down some of the inserts, as you are seeing.



        You could test with read commited snapshot isolation level and use row versioning if blocking is the case.
        You need to know the implications for your tempdb (version store will grow, long running open transactions can be problematic for your tempdb)



        To enable RSSI:



        ALTER DATABASE MyDatabase  
        SET ALLOW_SNAPSHOT_ISOLATION ON

        ALTER DATABASE MyDatabase
        SET READ_COMMITTED_SNAPSHOT ON


        Otherwise you could issue a profiler session to get more information on the issue. It is hard to answer (apart from obvious blocking) without more details.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Sep 25 '18 at 12:31









        Randi VertongenRandi Vertongen

        3,293822




        3,293822






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Database Administrators Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f218501%2finsert-into-table-with-identity-insert-taking-almost-a-second%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Liste der Baudenkmale in Friedland (Mecklenburg)

            Single-Malt-Whisky

            Czorneboh