Stored Proc and performance

  • Is there any SQL that can be added to a Stored Proc to track if there was any blocking of its SQL, or for that matter explain the example below?

    I am tasked with explaining, and if possible fixing, jobs that sometimes run more than twice their normal run times. A good example is a program that almost always runs in less than one min, but this morning it took 4 1/2 hours to run. This created problems since our data was not fully loaded when folks started using the application.

    Thanks

  • You could try turning on the deadlocks trace flag:

    DBCC TRACEON (1222, -1)

    And then checking the log to see if it reports deadlocks during the period the job is running.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwilliscp (1/16/2013)


    Is there any SQL that can be added to a Stored Proc to track if there was any blocking of its SQL, or for that matter explain the example below?

    I am tasked with explaining, and if possible fixing, jobs that sometimes run more than twice their normal run times. A good example is a program that almost always runs in less than one min, but this morning it took 4 1/2 hours to run. This created problems since our data was not fully loaded when folks started using the application.

    Thanks

    No. There is no SQL that can be added to a Stored Proc to track if there was any blocking of its SQL.

    But you can create another job to periodically check for the blocking and send you alert if there is any blocking.

    You can start here:select * from sys.dm_exec_requests where blocking_session > 0

    Note that your job might have taken 4.5 hrs due to some other reason also (not necessary due to blocking).

    There was no deadlock. Deadlock would fail the job.

  • Suresh B. (1/17/2013)


    There was no deadlock. Deadlock would fail the job.

    Incorrect. SQL Server kills only the transaction that costs the least to rollback, so the competing transaction could have been killed and the SP completed without failure.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Thanks I will try and see what I get, but not sure that it will show much if I am just checking for deadlocks. All the job (at that step does) is copy records from one table to update a different table. PerfMon did not show anything abnormal (CPU,Mem,Phy IO), so my guess is that a user was in making changes to the forecast and did not file the screen... but that is just a guess. If I am right it would not have created a deadlock, just blocking.

  • dwilliscp (1/17/2013)


    Thanks I will try and see what I get, but not sure that it will show much if I am just checking for deadlocks. All the job (at that step does) is copy records from one table to update a different table. PerfMon did not show anything abnormal (CPU,Mem,Phy IO), so my guess is that a user was in making changes to the forecast and did not file the screen... but that is just a guess. If I am right it would not have created a deadlock, just blocking.

    Here my experience is a little rough but my understanding is that blocks are a normal process. SQL must block some transactions, allowing others to complete if they've established locks. When a lock is kept and held for too long, the blocked transaction will then be considered in deadlock with it. So that is when SQL Server kicks in to decide which of the transaction processes should be killed.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwain.c (1/17/2013)


    dwilliscp (1/17/2013)


    Thanks I will try and see what I get, but not sure that it will show much if I am just checking for deadlocks. All the job (at that step does) is copy records from one table to update a different table. PerfMon did not show anything abnormal (CPU,Mem,Phy IO), so my guess is that a user was in making changes to the forecast and did not file the screen... but that is just a guess. If I am right it would not have created a deadlock, just blocking.

    Here my experience is a little rough but my understanding is that blocks are a normal process. SQL must block some transactions, allowing others to complete if they've established locks. When a lock is kept and held for too long, the blocked transaction will then be considered in deadlock with it. So that is when SQL Server kicks in to decide which of the transaction processes should be killed.

    Not correct, Dwain. Blocking and Deadlock are two different beasts. A deadlock exists if two processes are are waiting are each waiting for a resource locked by the other process. If a process is simply blocked by another, like an update is waiting for a shared read (not sure of the correct term) to be released to place an exclusive lock to make an update, you may get a time-out, but it isn't deadlocked.

  • Lynn Pettis (1/17/2013)


    A deadlock exists if two processes are are waiting are each waiting for a resource locked by the other process.

    Easy for you to say. 🙂

    Like I said, I was a little rough on this myself, so figured someone would come along and correct me.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Any old CICS mainframe programmers here remember calling a deadlock a "deadly embrace"? That wasn't limited to just databases, which were relatively new in my environment (we were still using VSAM), but also the resources that you needed to control. :w00t:

  • tyson.price (1/21/2013)


    Any old CICS mainframe programmers here remember calling a deadlock a "deadly embrace"? That wasn't limited to just databases, which were relatively new in my environment (we were still using VSAM), but also the resources that you needed to control. :w00t:

    I remember VSAM. Wasn't the utility to create them called IDCAMS?

    Thanks for the trip down memory lane.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwain.c (1/21/2013)


    tyson.price (1/21/2013)


    Any old CICS mainframe programmers here remember calling a deadlock a "deadly embrace"? That wasn't limited to just databases, which were relatively new in my environment (we were still using VSAM), but also the resources that you needed to control. :w00t:

    I remember VSAM. Wasn't the utility to create them called IDCAMS?

    Thanks for the trip down memory lane.

    Yep:-)

    That was also used to do most of the maintenance on them. The IBM DB2 databases (DBMS) was basically ISAM files, a forerunner of VSAM, using lots and lots of sorts. That is an over simplification but close. I also worked with IMS and IDMS.

    I don't bring stuff like that up where I work now. It's all Windows, SQL server, and VB .Net. I'm a relic here at 58 years old and no one wants to here about the old days when I fixed buggy whips.

    I get a chance to chuckle once in a while...the many lessons about standards, writing maintanable code, the KISS programming method, etc that all came about from many late nights and blood on the floor seemed to have gone by the wayside with some of the new programmers and the rush to use the latest and greatest. Even with top management. We get to learn them all over again :hehe:

  • dwain.c (1/21/2013)


    tyson.price (1/21/2013)


    Any old CICS mainframe programmers here remember calling a deadlock a "deadly embrace"? That wasn't limited to just databases, which were relatively new in my environment (we were still using VSAM), but also the resources that you needed to control. :w00t:

    I remember VSAM. Wasn't the utility to create them called IDCAMS?

    Thanks for the trip down memory lane.

    Yep:-)

    That was also used to do most of the maintenance on them. The IBM DB2 databases (DBMS) was basically ISAM files, a forerunner of VSAM, using lots and lots of sorts. That is an over simplification but close. I also worked with IMS and IDMS.

    I don't bring stuff like that up where I work now. It's all Windows, SQL server, and VB .Net. I'm a relic here at 58 years old and no one wants to here about the old days when I fixed buggy whips.

    I get a chance to chuckle once in a while...the many lessons about standards, writing maintanable code, the KISS programming method, etc that all came about from many late nights and blood on the floor seemed to have gone by the wayside with some of the new programmers and the rush to use the latest and greatest. Even with top management. We get to learn them all over again :hehe:

  • I had to investigate a similar problem and discovered the cause was a junior DBA running the following piece of code at 11PM and then going home.

    BEGIN TRAN;

    UPDATE MyTable SET MyValue = @MyValue;

    -- COMMIT;

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply