Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > sqlserver server > august 2006 >

sqlserver server : SQL Profiler Question


Jim Norton
8/1/2006 4:31:40 PM
I am running SQL Profiler against a simple stored procedure and am recording
RPC: Completed to get the duration of the procedure. This is working fine
but I need a little help with what this really means because I am seeing
variance anywhere between 13ms to 6000ms. I am assuming that the duration
measures the time period starting when the server receives the query -
through execution - and ultimately to formulating the output...but NOT
actual delivery of the output. I guess what I am trying to determine is if
a slow network link will alter this result or if the variance I am seeing is
most likely caused by an internal resource on the SQL Server
(CPU/Disk/Memory/Locking etc.). Can I eliminate the network from being a
problem? Can I eliminate the client machine sending the requests as being
the problem? (it is always the same client btw).

Any help would be appreciated.

Thanks

Andrew J. Kelly
8/1/2006 9:18:14 PM
The duration is from start to finish meaning until the last row is
delivered. If this was network related I would expect to see a very high
value in the waitstats for NetworkIO waits. You can use DBCC
SQLPERF(WAITSTATS) to get a snap shot of how many milliseconds in wait times
you have for each wait. You can clear it with DBCC SQLPERF(WAITSTATS,
CLEAR). But I would put my bet on blocking as the culprit. What does the
CPU & Reads columns say for a sp that completed in 13ms vs. 6000ms? If they
are roughly the same you can pretty much bet you are being blocked can can
confim this with sp_who2 or sp_lock.

--
Andrew J. Kelly SQL MVP

[quoted text, click to view]

JXStern
8/2/2006 11:02:16 AM
[quoted text, click to view]

As others have said, so the basic question is whether the time varies
according to the parameters in the SQL, the same SP or SQL can have
very different execution plans depending on values specified.

J.

Greg Linwood
8/3/2006 12:00:00 AM
This is only true if the SP happens to recompile due to some other factor -
as differing param values (even wildly different values) alone don't cause a
SP to recompile..

Regards,
Greg Linwood
SQL Server MVP

[quoted text, click to view]

Greg Linwood
8/3/2006 1:25:12 AM
Hi Jim

Just adding to what Andy's already said - another useful Profiler measure is
CPU, which tells you how much time the query spends on the CPU within SQL
Server (as opposed to Duration, which involves returning the resultset). CPU
also doesn't provide the full picture though, as blocked processes don't
report much CPU time. But it can give you a feel for how much actual
execution time with SQL Server a query takes up.

The fact that you've said this is always the same client might be important
if you expect a lot of data to be transfered back to clients from this proc.
If you ARE expecting lots of data to be sent back to the client, network
issues could be involved & Wireshark (previously Ethereal) is a great way to
confirm / deny this (as it provides network protocol level stats). If you
AREN'T expecting lots of data to be sent back, this is less likely & my
money's with Andys - that you could well have a blocking issue.

Regards,
Greg Linwood
SQL Server MVP

[quoted text, click to view]

JXStern
8/3/2006 3:50:40 PM
On Thu, 3 Aug 2006 08:42:01 +1000, "Greg Linwood"
[quoted text, click to view]

Not true, it might have compiled several plans previously and keeps
them in cache, and just choses between them by parameter sniffing.

J.


[quoted text, click to view]
Greg Linwood
8/4/2006 12:00:00 AM
This isn't true - multiple plans are NOT stored in the proc cache for
different parameters at all. Multiple plans ARE stored for different user
connection settings though..

Regards,
Greg Linwood
SQL Server MVP

[quoted text, click to view]

Andrew J. Kelly
8/4/2006 12:37:19 AM
Greg is correct here. If the query was properly parsed and had valid
parameters (or was auto parameterized) the values them selves would not be a
factor. At least not in how many plans there were. Just what plan it may use
depending on the values passed in at compile time. Parameters themselves do
not cause recompiles.

--
Andrew J. Kelly SQL MVP

[quoted text, click to view]

JXStern
8/4/2006 9:13:01 AM
On Fri, 4 Aug 2006 00:37:19 -0400, "Andrew J. Kelly"
[quoted text, click to view]

And an explicit sp_recompile first clears the cache, so that won't do
it either?

How about recompiles that take place due to, say, #temp tables?

J.
Andrew J. Kelly
8/4/2006 4:57:50 PM
If a plan gets recompiled it will create a new one that replaces the old one
given that everything is the same, not counting the values passed in. It is
certainly true that you can get a different type of plan (meaning scan vs
seek etc.) depending on the values passed in when the query is first run or
first run after it becomes invalidated. But if the only thing that changes
is the value of the parameters you will get a plan signature that is always
the same. So you should never have two different plans in cache with the
same signature at any given time.

--
Andrew J. Kelly SQL MVP

[quoted text, click to view]

JXStern
8/5/2006 10:39:12 AM
On Fri, 4 Aug 2006 16:57:50 -0400, "Andrew J. Kelly"
[quoted text, click to view]

Well then, I'm confused.

If "parameter sniffing" is going to do something, doesn't it need a
couple of different plans to choose from? What are the scenarios that
this works for?

I know that one of the boogeymen in SQLServer operations is an SP that
uses a plan optimized for one value and given another, but I thought
that the value (or at least the statistics slot the value corresponds
to somewhere downstream in the code) was part of the signature.

Josh
Kalen Delaney
8/5/2006 12:19:36 PM
The parameters are not part of the 'signature'.
You only need one plan in cache for parameter sniffing to be a problem.

I must admit, I had been hearing the term 'parameter sniffing' being tossed
about for quite a while before I found out what it meant, and to me, it
doesn't really seem like an appropriate term for the behavior it is applied
to.

Parameter sniffing is when SQL Server 'sniffs' the values coming on when a
plan is first compiled, and then continues to use that same plan for
subsequent executions, even if that plan is not a good plan for the new
values being passed in.

--
HTH
Kalen Delaney, SQL Server MVP


[quoted text, click to view]

Alexander Kuznetsov
8/5/2006 1:14:49 PM
[quoted text, click to view]

Kalen,

How would you prefer to call it?
Kalen Delaney
8/5/2006 1:39:08 PM
In class, I usually just describe the problem as inappropriate re-use of
plans.
I haven't come up with some short catch-phrase yet. :-)

--
HTH
Kalen Delaney, SQL Server MVP


[quoted text, click to view]

JXStern
8/5/2006 2:46:57 PM
On Sat, 5 Aug 2006 13:39:08 -0700, "Kalen Delaney"
[quoted text, click to view]

So, one sniff and it's history? Well then.

So, what if you have an app that feeds in dynamic SQL, like

select * from mytable where notpk = 'abc'
select * from mytable where notpk = 'xyz'

If 'abc' and 'xyz' fall into very different stat slots, could that
produce two different plans in cache? Or only a single plan, possibly
inappropriate for one value? But the full text is different then, so
does that constitute two signatures? But then it wouldn't be
'sniffing' as such. Guess I could try it and see.

If the 'sniffing' is that limited, hmm, have to reevaluate what was
going on in some past situations I worked on.

Thanks all.

Josh

ps - so, what about in SQL2005, where the unit of recompile is just
the statement, not the SP? Can't even formulate the questions for
that yet, but if it only keeps one plan for any unit, then, um,
something something.
Kalen Delaney
8/5/2006 3:38:26 PM
No, one sniff and that's what it continues to use until the plan is removed
from cache.

Dynamic SQL is not the same as stored procedures. In your example, if the
two values have a potential of leading to completely different plans, then
two plan will be stored and these will be considered 'ad-hoc queries'. There
are no parameters and thus nothing to sniff.

Take a look at this whitepaper:

Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server 2005
http://www.microsoft.com/technet/prodtechnol/sql/2005/recomp.mspx

--
HTH
Kalen Delaney, SQL Server MVP


[quoted text, click to view]

JXStern
8/5/2006 4:19:22 PM
On Sat, 5 Aug 2006 15:38:26 -0700, "Kalen Delaney"
[quoted text, click to view]

I'd read the paper, but managed to read it with the *assumption*
(yeah, I know) that it *meant* the plans were reused and that the
values, or the stats slot of the value, *were* part of the signature,
or at least the autoparameterization, or something like it, was very
common, and a number of similar plans differing only by the value
would be cached.

Looking at it again now, knowing this is *not* the case, well, "that's
very different!".

And again, thinking how this is supposed to work in SQL2005, where the
unit of recompilation is smaller, and/or where you can lock the plan
(I forget exactly the new terminology there), well, I'll have to spend
some time on it, I guess.

Josh
AddThis Social Bookmark Button