|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 1:36 PM
Points: 887,
Visits: 2,062
|
|
I'm a bit confused. Several of you suggested using "fully qualified names". But this usually refers to using all 4 parts: Server.Database.Schema.Object.
Is that what you suggest we should use everywhere in code?
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:21 AM
Points: 5,244,
Visits: 7,062
|
|
rVadim (5/11/2010) I'm a bit confused. Several of you suggested using "fully qualified names". But this usually refers to using all 4 parts: Server.Database.Schema.Object.
Is that what you suggest we should use everywhere in code?
That was a bit unclear of "several of you" (of which I am one).
In this context, "fully qualified" should be read as "schema-qualified". My apologies.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Tuesday, December 07, 2010 12:55 AM
Points: 771,
Visits: 504
|
|
| Great question. I learned something new, which is always great!
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 10:25 AM
Points: 18,754,
Visits: 12,337
|
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 2:53 AM
Points: 1,530,
Visits: 359
|
|
thanks.. good question
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Friday, May 17, 2013 2:53 AM
Points: 1,530,
Visits: 359
|
|
i generally don't use recompile option for my procs... execution plan is created the first time stored proc is executed and then it takes less time to execute...
plz. suggest... what are the pros & cons of recompile option.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:21 AM
Points: 5,244,
Visits: 7,062
|
|
ziangij (5/11/2010) plz. suggest... what are the pros & cons of recompile option. Parameter sniffing.
Basically, SQL Server has three ways to deal with constant values in a query:
a) If it's a constant (e.g. WHERE Age = 35), that value is used to optimize. SQL Server will find a plan that is most efficient for that value.
b) If it's a variable (e.g. WHERE Age = @Age), the run-time value is not yet knwon when the query is optimized. Statistical information about the average spread of values in the Age column is used to optimze. The plan SQL Server creates will work good for most values, but might not be optimal for atypical values. If, for instance, a large majority of rows will have an age over 65 and only a small minority will have a younger age (as would be the case in a database of dementia patients), the plan will be optimal when @Age is 80, but less so when @Age is 43.
c) If it's a parameter in a procedure (e.g. CREATE PROC Something @Age int AS .... WHERE Age = @Age), then SQL Server will use ("sniff") the value passed to the parameter when the plan gets created, and find a plan that is optimal for that value. Since the plan goes into cache for future reuse, subsequent executions of the procedure will use the same plan, even if they are called with a different parameter value. In the above case, if the first execution of the procedure happens to use @Age = 43, then it is possible that all future executions with a higher value for @Age are very slow, because SQL Server picked a plan that is optimal for values of @Age where only a small minority of rows match.
In practice, parameter sniffing does more good than it does harm. But in cases where you find it does harm, you can use the RECOMPILE hint to force SQL Server to receompile the plan every time the stored procedure is executed. The benefit is that each execution uses a plan that is optimal for the @Age value passed in (since SQL Server will still sniff the parameter); the down side is that SQL Server has to spend a lot of resources for all those recompilations.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 7:15 AM
Points: 2,865,
Visits: 2,467
|
|
Thanks for the explanation Hugo. The questio happened to fall while I was on vacation and traveling. In addition to your explanation, each database should have a good maintenance plan. Within that plan should be the updating of statistics on the various indexes as well as rebuilding indexes that need it. I also use a DBCC UPDATEUSAGE as well. After all of my maintenance is completed, I want to ensure that the various stroed procedures are using an optimal QueryPlan. So, the last thing I do is to perform a recompile of all stored procedures and triggers. Ultimately, this will force the procedures to reaquire a new queryplan the first time that they are run.
Additionally, within your explanation, you should go a bit further. It slips me right this moment which hint it is, but for the scenario you describe, there is a hint that can be used in order to have the procedure use the queryplan for the longest running parameter. this takes some effort to identify, but works. I found it while trying to quell an attempted developer revolt, in that the developers wanted to recompile procedures each time that they ran. In a 1200 user high transaction database, where some of these procedures are called literally a hundred times an hour, could be a problem. I will lookup the hint and post it later.
Steve Jimmo Sr DBA “If we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 3:21 AM
Points: 5,244,
Visits: 7,062
|
|
sjimmo (5/12/2010) It slips me right this moment which hint it is, but for the scenario you describe, there is a hint that can be used in order to have the procedure use the queryplan for the longest running parameter. I think you are referring to the OPTIMIZE FOR option, right?
You can use this to force optimization for the "most often" used vlaue (getting the best performance in most cases, but worse -potentially much worse- in rare cases). Or you can use it to force optimization for a value that will result in a plan that might not be optimal for all values, but will never hit extreme execution times.
[funny fact - I am typing this right after identifying a query ona customer site where the optimizer decided on a nested loops join and a table scan, based on an estimate of 1 input row; the actual row count turned out to be over 20,000 so a 25,000+-row table got scanned over 20,000 times... - that would be the extreme execution time]
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 7:15 AM
Points: 2,865,
Visits: 2,467
|
|
I think you are referring to the OPTIMIZE FOR option, right?
That is the one. Yes there is a down side to it, as with most hints.
Unfortunately, many think that a hint is law, and have spent many hours debating hints, as well as showing hints that work and then not work because the optimizeer deemed that it knew better.
I have not worked with this one yet, but think it is coming with a specific issue. Thanks for the info of your experience.
Steve Jimmo Sr DBA “If we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan
|
|
|
|