PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Hébergement serveur > ms.sqlserver.server > sp_executesql increases number of reads?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
sp_executesql increases number of reads?

Réponse
 
LinkBack Outils de la discussion
Vieux 10/09/2008, 14h03   #1
Yash
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut sp_executesql increases number of reads?

Hi,

I am using SQL 2000 SP4.

I have compared 2 scenarios:
Scenario 1:
insert into #bacs_report
SELECT .....
WHERE <conditions>

When monitored in the profiler, this statement shows 18895 reads for
the SQL:StmtCompleted event.

The block that executes this stmt takes 750ms.

Scenario 2:
insert into #bacs_report
EXEC sp_executesql <the same SELECT query with parameters>

In profiler, this stmt shows 2 rows:
A SELECT stmt that has 60K reads
An INSERT stmt that has 70K reads

The overal block still takes around 750 ms with results that match
those of the first scenario.

Can someone explain why is there such a surge in the number of reads
when using sp_executesql? Is the profiler showing wrong results as the
total duration still remains the same?

Thanks,
Yash
  Réponse avec citation
Vieux 10/09/2008, 23h25   #2
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sp_executesql increases number of reads?

Yash (yashgt@gmail.com) writes:
> I am using SQL 2000 SP4.
>
> I have compared 2 scenarios:
> Scenario 1:
> insert into #bacs_report
> SELECT .....
> WHERE <conditions>
>
> When monitored in the profiler, this statement shows 18895 reads for
> the SQL:StmtCompleted event.
>
> The block that executes this stmt takes 750ms.
>
> Scenario 2:
> insert into #bacs_report
> EXEC sp_executesql <the same SELECT query with parameters>
>
> In profiler, this stmt shows 2 rows:
> A SELECT stmt that has 60K reads
> An INSERT stmt that has 70K reads
>
> The overal block still takes around 750 ms with results that match
> those of the first scenario.
>
> Can someone explain why is there such a surge in the number of reads
> when using sp_executesql? Is the profiler showing wrong results as the
> total duration still remains the same?


First, note that the total number of reads in the second case is 70000
reads, not 130000. The INSERT statement includes the SELECT statement.

As for the increased number of reads, this is likely to be due to
differences in query plans. You indicate that the call to sp_executesql
is parameterised, and the plan for

SELECT ... FROM tbl WHERE col = <const>

and

SELECT ... FROM tbl WHERE col = @para

is necessarily not the same. If the first case, the optimizer only needs
to consider the one and single value for the constant. In the other,
the optimizer must also account for other values. Then again, the
first time you run the query the optimizer "sniffs" the parameter, so
some there is some likelyhood for the same plan. Unless the query
already was in the cache, and the plan had been sniffed from a different
input value.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

  Réponse avec citation
Vieux 11/09/2008, 12h36   #3
Yash
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sp_executesql increases number of reads?

I don't think one query runs faster because the plan is cached. The
numbers I have given are what I see after multiple executions.
I have now tested with 2 scenarios, one with INSERT and the other
without. This time both use sp_executesql and have parameters. I
observe that the one with :
insert into #bacs_report
EXEC sp_executesql <query with parameters>
takes 60K reads
and the one with just
EXEC sp_executesql <query with parameters> takes 18K

Looks like the INSERT is causing the problem when used with
sp_executesql. Why would the plan differ in the 2 cases?

Another issuse is that SQL Query Analyzer does not show the query plan
when the INSERT stmt is present with sp_executesql.

Thanks,
Yash

Erland Sommarskog wrote:
> Yash (yashgt@gmail.com) writes:
> > I am using SQL 2000 SP4.
> >
> > I have compared 2 scenarios:
> > Scenario 1:
> > insert into #bacs_report
> > SELECT .....
> > WHERE <conditions>
> >
> > When monitored in the profiler, this statement shows 18895 reads for
> > the SQL:StmtCompleted event.
> >
> > The block that executes this stmt takes 750ms.
> >
> > Scenario 2:
> > insert into #bacs_report
> > EXEC sp_executesql <the same SELECT query with parameters>
> >
> > In profiler, this stmt shows 2 rows:
> > A SELECT stmt that has 60K reads
> > An INSERT stmt that has 70K reads
> >
> > The overal block still takes around 750 ms with results that match
> > those of the first scenario.
> >
> > Can someone explain why is there such a surge in the number of reads
> > when using sp_executesql? Is the profiler showing wrong results as the
> > total duration still remains the same?

>
> First, note that the total number of reads in the second case is 70000
> reads, not 130000. The INSERT statement includes the SELECT statement.
>
> As for the increased number of reads, this is likely to be due to
> differences in query plans. You indicate that the call to sp_executesql
> is parameterised, and the plan for
>
> SELECT ... FROM tbl WHERE col = <const>
>
> and
>
> SELECT ... FROM tbl WHERE col = @para
>
> is necessarily not the same. If the first case, the optimizer only needs
> to consider the one and single value for the constant. In the other,
> the optimizer must also account for other values. Then again, the
> first time you run the query the optimizer "sniffs" the parameter, so
> some there is some likelyhood for the same plan. Unless the query
> already was in the cache, and the plan had been sniffed from a different
> input value.
>
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
>
> Links for SQL Server Books Online:
> SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
> SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
> SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

  Réponse avec citation
Vieux 11/09/2008, 23h19   #4
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sp_executesql increases number of reads?

Yash (yashgt@gmail.com) writes:
> I don't think one query runs faster because the plan is cached. The
> numbers I have given are what I see after multiple executions.
> I have now tested with 2 scenarios, one with INSERT and the other
> without. This time both use sp_executesql and have parameters. I
> observe that the one with :
> insert into #bacs_report
> EXEC sp_executesql <query with parameters>
> takes 60K reads
> and the one with just
> EXEC sp_executesql <query with parameters> takes 18K
>
> Looks like the INSERT is causing the problem when used with
> sp_executesql. Why would the plan differ in the 2 cases?


I ran this simple test:

create table #ord(OrderID int)
go
exec sp_executesql
N'SELECT OrderID FROM Northwind..Orders WHERE CustomerID = @c',
N'@c nchar(5)', @c = N'ALFKI'
go
insert #ord
exec sp_executesql
N'SELECT OrderID FROM Northwind..Orders WHERE CustomerID = @c',
N'@c nchar(5)', @c = N'ALFKI'
go

I get 16 reads for the plain SELECT, where as the SELECT as part of the
INSERT gets 24 reads, and the INSERT batch in total has 63.

I think part of the problem here is what is a read. More IO operations
are to be the expected with the INSERT, but that would be writes. But
I don't see any writes at all, so it seems that writes counts as reads
for some reason.

I'm guessing wildly here, but I think the extra reads for the SELECT
insert the INSERT comes from when result is written to some internal
rowset.

> Another issuse is that SQL Query Analyzer does not show the query plan
> when the INSERT stmt is present with sp_executesql.


You can catch the plans with the Performance:Show Statistics Profile
event. You need to include BinaryData in the result set.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

  Réponse avec citation
Vieux 12/09/2008, 09h56   #5
Yash
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sp_executesql increases number of reads?

Now I have included the INSERT within the dynamic SQL stmt. It now
reads:
INSERT INTO #temp
SELECT ....

Even with sp_executesql, it gives 19k reads. This has solved my
problem.

Thanks,
Yash

Erland Sommarskog wrote:
> Yash (yashgt@gmail.com) writes:
> > I don't think one query runs faster because the plan is cached. The
> > numbers I have given are what I see after multiple executions.
> > I have now tested with 2 scenarios, one with INSERT and the other
> > without. This time both use sp_executesql and have parameters. I
> > observe that the one with :
> > insert into #bacs_report
> > EXEC sp_executesql <query with parameters>
> > takes 60K reads
> > and the one with just
> > EXEC sp_executesql <query with parameters> takes 18K
> >
> > Looks like the INSERT is causing the problem when used with
> > sp_executesql. Why would the plan differ in the 2 cases?

>
> I ran this simple test:
>
> create table #ord(OrderID int)
> go
> exec sp_executesql
> N'SELECT OrderID FROM Northwind..Orders WHERE CustomerID = @c',
> N'@c nchar(5)', @c = N'ALFKI'
> � go
> insert #ord
> exec sp_executesql
> N'SELECT OrderID FROM Northwind..Orders WHERE CustomerID = @c',
> N'@c nchar(5)', @c = N'ALFKI'
> go
>
> I get 16 reads for the plain SELECT, where as the SELECT as part of the
> INSERT gets 24 reads, and the INSERT batch in total has 63.
>
> I think part of the problem here is what is a read. More IO operations
> are to be the expected with the INSERT, but that would be writes. But
> I don't see any writes at all, so it seems that writes counts as reads
> for some reason.
>
> I'm guessing wildly here, but I think the extra reads for the SELECT
> insert the INSERT comes from when result is written to some internal
> rowset.
>
> > Another issuse is that SQL Query Analyzer does not show the query plan
> > when the INSERT stmt is present with sp_executesql.

>
> You can catch the plans with the Performance:Show Statistics Profile
> event. You need to include BinaryData in the result set.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
>
> Links for SQL Server Books Online:
> SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
> SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
> SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

  Réponse avec citation
Vieux 12/09/2008, 22h47   #6
Erland Sommarskog
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: sp_executesql increases number of reads?

Yash (yashgt@gmail.com) writes:
> Now I have included the INSERT within the dynamic SQL stmt. It now
> reads:
> INSERT INTO #temp
> SELECT ....
>
> Even with sp_executesql, it gives 19k reads. This has solved my
> problem.


Indeed, that is likely to be more effcient. Thanks for giving me a
reminder!



--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h06.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15196 seconds with 14 queries