Sql-server – Capturing sp_executesql statement in sql profiler

dynamic-sqlprofilersql server

After catching some data in a SQL Profiler session, I can almost always see a second entry of type RPC:Completed for each query using a sp_executesql statement. I know that this is done to create re-usable execution plan by reducing the query with parametrization.

e.g.

exec sp_executesql N'SELECT s.* FROM s WHERE s.C= @O',
@O uniqueidentifier',
@O='11112222-4444-3333-1212-5555666677'

Duration: 10 ms

Next entry:

SELECT s.* FROM s WHERE s.C= @O

Duration: 10 ms

Looking at the total sum(Duration) of the profiler events, do I have to count both of those? The query was executed once, so 10 ms was the correct result. Instead the total result is 20 ms, with I think is just WRONG and a program-error of Profiler. Do I have to filter one of them away anyhow and how?

Best Answer

You don't have to count both. As you see, you get an rpc completed for sp_executesql. Your Select which shows up as StmtCompleted is a "subtask" of the rpc. The SQL trace already has the sum of all statements inside a rpc summed up for you.