The First execution of a Stored Proc shows a delay between SP:StmtStarting and SP:Starting

We experience a performanceproblem with some of our Stored Procedures. SQL Server is "Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)"
Situation:
SQL Server Proc1 executes some SQL Statement and starts some other SQL Stored Procedures. I open a SQL Management Studio Session "example session_id 105", trace the session 105 with the SQL Server Profiler.
I start Proc 1, when Proc1 starts the execution of Proc 2, the Profiler Trace shows a delay of 6 seconds between SP:StmtStarting "execute db..proc2 @SomeVar" and SP:Starting "execute db..proc2 @SomeVar".
All following executions of Proc1 in the Session 105 runs without a delay between SP:StmtStarting "execute db..proc2 @SomeVar" and SP:Starting "execute db..proc2 @SomeVar".
But when i open a new SQL Server Management Session "session_id 124", the first execution of Proc1 when it executes Proc 1, there is again the delay of 6 seconds between SP:StmtStarting "execute db..proc2 @SomeVar" and SP:Starting "execute
db..proc2 @SomeVar".
Proc 1 starts the execution of Proc2 with a simple execute statement like this:
Execute DB..Proc2 @SomeVar
So its not dynamic SQL.
What is SQL Server doing? I understand that SQL Server has to do some work when it executes the first time a Stored Procedure. But why is the SQL Server doing it in every new Session?
How can i prevent this behavior or how to make it faster?
Best Regards
Paolo

>In my case the temp tables ruined the performance.
Creating temp tables takes time & resources. Temporary table usage should be justified and tested in stored procedures. There are cases when temporary table usage is helpful especially with very complex queries.
In your case it appears that not one but several temp tables were applied. That can be punishing.
Paul White's blog: "Ask anyone what the primary advantage of temporary tables over
table variables is, and the chances are they will say that temporary tables support statistics and table variables do not.  This is true, of course; even the indexes that enforce PRIMARY KEY and UNIQUE constraints on table variables do not have
populated statistics associated with them, and it is not possible to manually create statistics or non-constraint indexes on table variables.  Intuitively, then, any query that has alternative execution plans to choose from ought to benefit from using
a temporary table rather than a table variable.  This is also true, up to a point.
The most common use of temporary tables is in stored procedures, where they can be very useful as a way of simplifying a large query into smaller parts, giving the optimizer a better chance of finding good execution plans, providing statistical
information about an intermediate result set, and probably making future maintenance of the procedure easier as well.  In case it is not obvious, breaking a complex query into smaller steps using temporary tables makes life easier for the optimizer in
several ways.  Smaller queries tend to have a smaller number of possible execution plans, reducing the chances that the optimizer will miss a good one.  Complex queries are also less likely to have good cardinality (row count) estimates and statistical
information, since small errors tend to grow quickly as more and more operators appear in the plan.
This is a very important point that is not widely appreciated.  The SQL Server query optimizer is only as good as the information it has to work with.  If cardinality or statistical information is badly wrong
at any point in the plan, the result will most likely be a poor execution plan selection from that point forward.  It is not just a matter of creating and maintaining appropriate statistics on the base tables, either.  The optimizer does
use these as a starting point, but it also derives new statistics at every plan operator, and things can quickly conspire to make these (invisible) derived statistics hopelessly wrong.  The only real sign that something is wrong (aside from poor performance,
naturally) is that actual row counts vary widely from the optimizer’s estimate.  Sadly, SQL Server does not make it easy today to routinely collect and analyse differences between cardinality estimates and runtime row counts, though some small (but welcome)
steps forward have been made in SQL Server 2012 with new row count information in the
sys.dm_exec_query_stats view.
The benefits of using simplifying temporary tables where necessary are potentially better execution plans, now and in the future as data distribution changes and new execution plans are compiled.  On the cost side of the ledger we have
the extra effort needed to populate the temporary table, and maintain the statistics.  In addition, we expect a higher number of recompilations for optimality reasons due to changes in statistics.  In short, we have a trade-off between potential
execution plan quality and maintenance/recompilation cost.
LINK:
http://sqlblog.com/blogs/paul_white/archive/2012/08/15/temporary-tables-in-stored-procedures.aspx
Kalman Toth Database & OLAP Architect
IPAD SELECT Query Video Tutorial 3.5 Hours
New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

Similar Messages

  • How come whenever i plug my iPhone 5 into the computer the device will not show up on my itunes.  This is the first time that i have done it since i updated to iOS7 and before i had no problem but now it just wont work.

    How come whenever i plug my iPhone 5 into the computer the device will not show up on my itunes.  This is the first time that i have done it since i updated to iOS7 and before i had no problem but now it just wont work.

    Hi Celia51510,
    Welcome to the Support Communities!
    The article below may be able to help you with this issue.
    Click on the link to see more details and screenshots. 
    I can't tell from your post if you are using a Windows or a Mac Computer.
    Try deleting the iTunes application and redownloading the version you need below:
    iTunes 11.1 Download for Mac
    http://support.apple.com/kb/DL1614
    iTunes 11.1 for Windows (64-bit)
    http://support.apple.com/kb/DL1615
    Cheers,
    - Judy

  • Since the 4.0.1 update the first Firefox window I open refuses to open tabs, only new windows, and all refuse to show my bookmarks toolbar. All Sync settings and account seem lost.

    Since the update the first Firefox window I open refuses to open tabs, only new windows, and all refuse to show my bookmarks toolbar. All Sync settings and account seem lost.

    G'day again, I've also lost the option of a 'Stop loading' or 'Refresh' button.

  • I opened my iPhoto library today which was stored on an external hard drive and several of my videos now will not play. The weird thing is that each file shows a duration of 1 second and when I open them it just shows one frame. What the

    I opened my iPhoto library today which was stored on an external hard drive and several of my videos now will not play at all. The weird thing is that each file shows a duration of 1 second and when I open them it just shows one frame. What the !&%$#@ Where did my original video files go?

    Sounds like a good plan, you can never be to safe and having a second External HD is safer than one.
    The only thing that I would do different, is setup the 3T as my TimeMachine Backup and then erase and move my Photos, Videos and perhaps even iTunes Media to the 1T. That way you will have room to backup both the Internal Hard Drive and the 1T External Hard Drive to the 3T using TimeMachine.
    Take your time, study and use the > Time Machine - Frequently Asked Questions if you get stuck or aren't sure how to make the move.
    Also see > iPhoto: How to move the Library folder to a new location
    iMovie ’08: Storing or moving your video library to an external hard disk
    and >  iTunes for Mac: Moving your iTunes Media folder

  • When I open 2 or more browsers of Firefox, click to open and close email it pops up the First Firefox browser since I have verrsion 6 as of yesterday. And some times the tool bar dosen't appear or have to refresh it.

    Question
    When I open 2 or more browsers of Firefox, click to open and close email it pops up the First Firefox browser since I have verrsion 6 as of yesterday. And some times the tool bar dosen't appear or have to refresh it.

    Question
    When I open 2 or more browsers of Firefox, click to open and close email it pops up the First Firefox browser since I have verrsion 6 as of yesterday. And some times the tool bar dosen't appear or have to refresh it.

  • I was setting up my Airport and thought the first set up did not go through, so I set up again and I ended up with two accounts instead of one.  How can I manage to have only one account now? Thanks for the help.

    I was setting up my Airport and thought the first set up dod not go through, then I set up again and ended up with two wireless accounts.  I use it for the prointer and the iPad, and I can see both accounts in the iPad.  How do I get rid of one account?  Thanks for the help!

    me.com accounts can be used for iCloud.  See the FAQ section in:
    <http://support.apple.com/kb/ht4895>
    but it may be too late if you have already created a new AppleID.
    A few years ago Apple said they were working on allowing account merging, but it never happened (maybe objections from copyright holders).

  • I plugged my iphone in for the first time to my computer. I synced it with my itunes and it deleted everything. Now I don't know what to do because I did not even have the chance to back it up! everything is gone please please help me!

    I plugged my iphone in for the first time to my computer. I synced it with my itunes and it deleted everything. Now I don't know what to do because I did not even have the chance to back it up! everything is gone please please help me!

    When you saw this dialog box, did you choose "Erase and Sync"?
    If you did, your iPhone was erased, then your iTunes Library was transferred to it. If your iTunes Library was empty, your iPhone will be also.
    Your previously purchased music, apps, books, and videos are still yours. You just have to download them again.
    Read: Downloading past purchases from the App Store, iBookstore, and iTunes Store

  • I've just been offered the first iphone (1st Generation) which was released some time ago, in which and what way will I be underhanded.

    I've just been offered the first iphone (1st Generation) which was released some time ago, in which and what way will I be underhanded as the price range is very handsome.

    mirameahora wrote:
    You will get no use out of this phone and wont be able to use itunes either
    I don't know why you say that. Many people are still quite happily using first-generation iPhones. They are very limited in the carrier support - EDGE only as Tim said - and the apps available are few now, but the phones still work and serve their original purpose.
    That said, I certainly wouldn't buy one. Used iPhone 3G and even 3GS units are available quite affordably and are much more flexible.
    Regards.

  • I try to make update for my iphone and start showing itunes need to be connected and never start and i connect my phone on itunes but it say need to be format for phone .please help me .

    I try to make update for my iphone and start showing itunes need to be connected and never start and i connect my phone on itunes but it say need to be format for phone .please help me .

    Hello Huss87bash,
    This screen indicates that the device is in recovery mode, and must be restored in iTunes before it can be used again.
    If you can't update or restore your iOS device
    http://support.apple.com/kb/HT1808
    Once the device has been restored, you should be able to restore your device from a backup to replace all of your content and settings.
    iOS: Back up and restore your iOS device with iCloud or iTunes
    http://support.apple.com/kb/HT1766
    Cheers,
    Allen

  • Remote Desktop cannot verify the identity of the computer because there is a time or date diffrence between your computer and remote computer

    Hello.....
    I'm not able to log into Windows Server 2008 r2 server thorugh Remote Desktop connection, receiving below error message.
    This issue is with only three servers in the environment
    "Remote Desktop cannot verify the identity of the computer because there is a time or date diffrence between your computer and remote computer......"
    The date/time is correct on the server when i checked in the console session of the server
    Can see below messages in event logs
    Event ID 1014:
    "Name resolution for the name XYZdomain.com timed out after none of the configured DNS servers responded."
    Event ID 1053:
    The processing of Group Policy failed. Windows could not resolve the user name. This could be caused by one of more of the following:
    a) Name Resolution failure on the current domain controller.
    b) Active Directory Replication Latency (an account created on another domain controller has not replicated to the current domain controller).
    Any thoughts ....

    Hi,
    Have you tried to connect these three servers with IP address instead of computer name or DNS name?
    Check Remote Desktop Connection settings: Option-->Advanced-->Connect from anywhere-->Settings-->Connection Settings-->Select “Do not user an RD Gateway server”
    For more information please refer to following MS articles:
    Remote Desktop cannot verify the identity of the remote computer because there is a time or date difference between your computer and the remote computer
    http://social.technet.microsoft.com/Forums/en-US/w7itpronetworking/thread/c1f64836-5606-49b0-82eb-56be7f696520
    Cannot connect via Remote Desktop
    http://social.technet.microsoft.com/Forums/en-US/windowsserver2008r2general/thread/5087e897-8313-468c-ad37-ef18b87d4dd6
    Lawrence
    TechNet Community Support

  • The first tab in my browser always shows loading and cannot close it, Why?

    When my browser is open and have multiple tabs open, the first tab has the animated circle that shows that page is loading, and if I try to close that tab it does not close. I have tried to uninstall and reinstall but have not been able to clear this issue up, Please help!

    * "Clear the Cache": Tools > Options > Advanced > Network > Offline Storage (Cache): "Clear Now"
    * "Remove the Cookies" from sites that cause problems: Tools > Options > Privacy > Cookies: "Show Cookies"
    Start Firefox in [[Safe Mode]] to check if one of the add-ons is causing the problem (switch to the DEFAULT theme: Tools > Add-ons > Themes).
    * Don't make any changes on the Safe mode start window.
    See:
    * [[Troubleshooting extensions and themes]]
    * [[Troubleshooting plugins]]
    If it does work in Safe-mode then disable all extensions and then try to find which is causing it by enabling one at a time until the problem reappears.
    * Use "Disable all add-ons" on the [[Safe mode]] start window to disable all extensions.
    * Close and restart Firefox after each change via "File > Exit" (Mac: "Firefox > Quit"; Linux: "File > Quit")

  • How to capture the first execution of a report

    Hi,
    I am executing a report in background. The first time the report is executed I have to do a different processing. After the 1st execution I have to different processing.
    Someone please tell me how to capture the 1st execution of a report. Is there a system variable ?
    Appreciate your <removed by moderator> response.
    Thanks,
    Dikshitha G
    Edited by: Thomas Zloch on May 12, 2011 11:36 AM

    Keshav.T wrote:
    Are you going to create a Z table for this ???? ... Is there nobody to advice you in your firm ???
    Hello Keshav,
    A couple of years ago i would have recommended using the INDX table, but it has got it's demerits.
    Maintenance of Z-table is easier than the INDX table. Say there is some error in the program & you want to override the flag. Will it be easier to do so in the INDX table or via SM30 for the Z-Table?
    As a matter-of-fact i will recommend using the solution provided by Florian the TVARVC technique.
    1. Create a parameter(specific to your program) & transport it.(See the trxn STVARV)
    2. In your program check the value of this param & set it accordingly.
    Using TVARVC you will 2 birds with one stone:
    1. You don't have to create a custom table.
    2. Easy maintenance via STVARV trxn.
    Hope you get the point!
    Cheers,
    Suhas
    PS: In our system we have a Z-table designed specifically for this particular purpose. All the programs having this kind of requirement refer to this table.

  • Query is slower on the first execution

    I noticed that when I run a query command on the shell, the first run is significantly slower. I used the time command to measure the execution time. The execution time for the first run is more than 10 times of the second run.
    E.g. My query took me 5.97s at the first run, but the same query only took 0.3s at the second run.
    I wonder whether this is a general circumstance for Berkeley DB XML or is it specific to the container that I created?
    Thank you!

    all of our dbs have that behaviour too. I assume it had to do with caching in memory and (if on *nix xystems) disk caching as well. any disk based db I've worked with has that "feature".                                                                                                                                                                                                                                                                                                                                                                                   

  • In Pages the first page of all my documents show the pages user guide. How can I reinstate my documents. They are still on my iPhone

    Help Please!   All my numerous documents in Pages have recently altered, so that there are no contents other than the face page, which is the Pages User Guide page. Ie Showing the pen and ink bottle. I think this was brought about by me trying to see if I could print the Guide Pages!!!  (To no avail)!
    Please. How can I reinstate my documents?  They are still showing ok on my iPhone.

    If you click the link I posted previously, it takes you right to the Pages community.
    Here >  Pages: iWork: Apple Support Communities
    Then click New.

  • It's the first time I've bought a movie from iTunes on my PC and it gives me glitches the enire movie aswell as the voices don't match the picture?

    I got a new PC. I have an iTunes account on my iPod as well but I set it up properly on here as well this is a Pavilion HP G6 it has windows 8. I dowloaded iTunes onit, it works fine. I bought "We're the Millers" today on iTunes and it gets me upset I can't watch it. Every time I play it it glitches. After the first sevral glitches the voice loses the picture; the character on the screen isn't saying the right words at the right time. I waited till it was fully down loaded and I restarted my labtop twice to see if the was the cause. It was not and and I getting up set I paid $13 for a HD movie that isn't even a movie; someone please help!

    "Bought a movie from iTunes which as gone into my videos file and I am trying to watch it on my computer. "
    But you can't? Does it show up in iTunes?
    "have iTunes on my pc but even after transferring files and syncing the video still won't show on the pc"
    Transferring from/to where?
    How are you transferring?

Maybe you are looking for

  • Db server doesn't start. Error message: ORA-00020: maximum number of proces

    After I changed the parameter from archive log mode. My db server is having problems. I can't log in as sys using sqlplus. when I log in through sqlplus /nolog, and tried to shut down, here is the error message: $ sqlplus /nolog SQL*Plus: Release 10.

  • Home Theatre question

    Hello! I need to know what is needed to hook up my computer with Xtreme Gamer to my 42" LCD TV. The TV? has a Sony 5. surround with RCA hookup. I don't want to buy a computer-speaker system. I heard you can get 3.5mm to RCA coversion cables, but will

  • Oracle i Supplier Portal Workflows - Supplier Change Order Workflow

    Hi Friends, Oracle iSupplier Portal uses the following workflows: ■ Supplier Change Order Workflow ■ Update Capacity Workflow ■ Order Modifiers Workflow ■ Advance Shipment Notice (ASN) Workflow ■ Purchase Order Acknowledgement (handled by PO Approval

  • DOS COMMAND IN ANOTHER MACHINE

    Hello! how can I execute a DOS command in another machine which is in my same network?? I need it because I have to execute an oracle report from my java program but the Oracle Reports software is in another machine ..... ( really i don't know if it'

  • Set up of a new iPad

    My first iPad was set up and working correctly with my iTunes account. My iPad had issues and since it was under protection I was able to have it replaced. Now I am sitting here trying to figure out how to set up my new iPad with my existing iTunes a