Unix Technical Forum

sp_executesql increases number of reads?

This is a discussion on sp_executesql increases number of reads? within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I am using SQL 2000 SP4. I have compared 2 scenarios: Scenario 1: insert into #bacs_report SELECT ..... ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-12-2008, 05:23 PM
Yash
 
Posts: n/a
Default 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-12-2008, 05:23 PM
Erland Sommarskog
 
Posts: n/a
Default 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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-12-2008, 05:23 PM
Yash
 
Posts: n/a
Default 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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 09-12-2008, 05:23 PM
Erland Sommarskog
 
Posts: n/a
Default 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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 09-12-2008, 05:23 PM
Yash
 
Posts: n/a
Default 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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 09-16-2008, 12:13 PM
Erland Sommarskog
 
Posts: n/a
Default 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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads for: sp_executesql increases number of reads?

Thread Thread Starter Forum Replies Last Post
Large number of reads on sysibm.sysuserauth. Patrick Finnegan DB2 0 08-29-2008 09:58 AM
exec sp_executesql vs. sp_executesql and performance elRoyFlynn SQL Server 3 02-29-2008 07:55 AM
Phycial Reads / Logical Reads best course of action? Thiko! Oracle Database 0 02-24-2008 02:36 PM
number of blocks reads in FTS mark Oracle Database 2 02-24-2008 12:10 PM
Unusually high number of current mode reads for a GTT insert VC Oracle Database 7 02-23-2008 08:28 AM


All times are GMT. The time now is 05:59 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com