|
Prefetch Prefetch – Querying at the
speed of SAN:
Prefetch is a mechanism
with which SQL Server can fire up many I/O requests in parallel
for Nested Loop join.
The SAN administrator
says your data volume has a throughput capacity of 400MB/sec.
But your long running query is waiting for I/Os (PAGEIOLATCH_SH)
and Windows Performance Monitor shows your data volume is doing
4MB/sec. Where is the problem?
When SQL Server does a
Nested Loop join, it might enable Prefetch. Prefetch is a
functionality where SQL Server fires up many I/O requests in
parallel (many outstanding I/Os). Prefetch will lead to better
performance for Nested Loop join queries when there are lots of
rows in the outer input table. 25 is the magic number. When SQL
Server estimates less than 25 rows for the outer input table
Prefetch will be disabled. And when more than 25 rows are
estimated Prefetch will be enabled.
This and similar topics are covered in my
workshops. If you are interested in learning practical
techniques to improve your SQL server performance, register for the upcoming 3 Day Level 400 Microsoft SQL Server 2012 and 2008 Performance Monitoring & Tuning Hands-on Workshop in Helsinki, Finland
during
February 27-March 01, 2013, click
here to register. These
are hands-on workshops with a maximum of 12 participants and not
lectures. For consulting engagements click
here.
This is a perfect design
(yes, I am an ex-Microsoft employee!); SQL Server wants to avoid
Prefetch for light weight queries, especially in an OLTP
environment as too many queries doing Prefetch will hurt
performance. I remember from my days at SQL Server Development
team, OLTP benchmarks including latest TPC-E disables Prefetch
with trace flag 8744. Write me an email and I will send you the
link.
To read additional articles I wrote
click
here.
Why this is a problem: I observe this
at many
customers; plan is cached with Prefetch disabled because first
execution resulted in less than 25 rows estimation for the outer
input table. Then the plan is used for parameters that are
resulting in more than 25 rows, actually 1000s of rows for the
outer input table. In this case since SQL Server cached the
plan, Prefetch is disabled and SQL Server is firing up 1 I/O at a
time. This leads to poor utilization of SAN. Symptoms: Query is
waiting for I/Os (PAGEIOLATCH_SH) for a long time (reading page after page),
but disk queue length is never more than 1.
The best way to learn is
to practice. To create the below tables and reproduce the
behavior, join the mailing list by using this link:
www.sqlworkshops.com/ml and
I will send you the table creation script.
--Example provided by www.sqlworkshops.com
--Create procedure that pulls orders based on City
drop
proc RegionalOrdersProc
go
create
proc RegionalOrdersProc @City
char(20)
as
begin
declare
@OrderID int,
@OrderDetails char(200)
select
@OrderID = o.OrderID,
@OrderDetails = o.OrderDetails
from
RegionalOrders ao inner
join Orders o
on (o.OrderID
= ao.OrderID)
where
City = @City
end
go
set statistics time on
go
--Example provided by www.sqlworkshops.com
--Execute the procedure with parameter SmallCity1
exec
RegionalOrdersProc
'SmallCity1'
go
--Right click on Clustered Index Scan
--to look at the properties
--Estimated number of rows in the
--outer
input table is less than 25

--Right click on Nested Loops
--to look at the properties
--Notice Prefetch is disabled (missing)

--Clear
data cache
checkpoint
dbcc
dropcleanbuffers
go
Think about a business
running a billing job looping over thousands of their customers
using stored procedure. If the first customer has placed less
than 25 orders for the billing period then Prefetch will be
disabled and every customer is limited to using 1 spindle at a
time.
--Example provided by www.sqlworkshops.com
--Execute the procedure with parameter BigCity
--We
are using cached plan
exec
RegionalOrdersProc
'BigCity'
go
--Estimated number of rows in the
--outer
input table is less than 25

--Notice Prefetch is disabled (missing)

--Query
execution time 7 seconds
--What
was the execution time?
--Clear
procedure cache
--to
trigger a new optimization
dbcc
freeproccache
go
--Example provided by www.sqlworkshops.com
--Execute the procedure with parameter SmallCity2
exec
RegionalOrdersProc
'SmallCity2'
go
--Clear
data cache
checkpoint
dbcc
dropcleanbuffers
go
--Example provided by www.sqlworkshops.com
--Execute the procedure with parameter SmallCity
--We
are using cached plan
exec
RegionalOrdersProc
'BigCity'
go
--Estimated
number of rows in the
--outer
input table is greater than 25

--Notice Prefetch is enabled

--Query
execution time 3 seconds
--On a SAN it was less than a second
--What
was the execution time?
--Example provided by www.sqlworkshops.com
--You
can fix the issue by using any of the following
--hints
--Create procedure that pulls orders based on City
drop
proc RegionalOrdersProc
go
create
proc RegionalOrdersProc @City
char(20)
as
begin
declare
@OrderID int,
@OrderDetails char(200)
select
@OrderID = o.OrderID,
@OrderDetails = o.OrderDetails
from
RegionalOrders ao inner
join Orders o
on (o.OrderID
= ao.OrderID)
where
City = @City
--Hinting optimizer to use SmallCity2 for estimation
option
(optimize
for (@City
=
'SmallCity2'))
--Hinting optimizer to estimate for the currnet parameters
--option
(recompile)
--Hinting optimize not to use histogram rather
--density for estimation (average of all 3 cities)
--option
(optimize for (@City UNKNOWN))
--option
(optimize for UNKNOWN)
end
go
--Example provided by www.sqlworkshops.com
--Execute the procedure with parameter SmallCity1
exec
RegionalOrdersProc
'SmallCity1'
go
--Clear
data cache
checkpoint
dbcc
dropcleanbuffers
go
--Example provided by www.sqlworkshops.com
--Execute the procedure with parameter BigCity
--We
are using cached plan
exec
RegionalOrdersProc
'BigCity'
go
--Notice Prefetch is enabled
--Estimated number of rows in the
--outer
input table is greater than 25
--Query
execution time 3 seconds
--On a SAN it was less than a second
--What
was your execution time?
Some of you might think
plan guides or plan freezing might solve this issue, well it
won’t help here. The estimation from your plan guide plan will
be ignored by the optimizer. Optimizer makes new estimation
based on current set of parameter values (compile time parameter
values).
I explain these concepts with detailed examples in
my webcasts (www.sqlworkshops.com/webcasts), I recommend
you to watch them. The best way to learn is to practice. To
create the above tables and reproduce the behavior, join the
mailing list at
www.sqlworkshops.com/ml and I will send you the
relevant SQL Scripts.
If you search for trace flag 8744 you
will end up with a KB Article 920093 titled: Tuning options for
SQL Server 2005 and SQL Server 2008 when running in high
performance workloads. There it is explained: Trace flag 8744
disables pre-fetching for the Nested Loops operator (with
caution). Even though the topic looks interesting ‘Tuning
options for high performance workloads’, don’t try these in
production. From my point of view this article is about
documenting undocumented trace flags used in benchmarks, let’s
leave it at that!
Register for the upcoming 3 Day Level 400 Microsoft SQL Server 2012 and 2008 Performance Monitoring & Tuning Hands-on Workshop in Helsinki, Finland
during
February 27-March 01, 2013, click
here to register. These
are hands-on workshops with a maximum of 12 participants and not
lectures. For consulting engagements click
here.
Disclaimer and copyright information:
This article refers to organizations and products that may be
the trademarks or registered trademarks of their various owners.
Copyright of this article belongs to
R Meyyappan
/ www.sqlworkshops.com. You may freely use the ideas and
concepts discussed in this article with acknowledgement
(www.sqlworkshops.com), but you may not claim any of it as your
own work.
This article is for informational purposes
only; you use any of the suggestions given here entirely at your
own risk.

Ramesh's consulting customers include Austria:
Allgemeines Krankenhaus Linz, BMD Systemhaus,
Bundesrechenzentrum, bwin, C07 IT Operations, CMC Markets,
derStandard, ERGO Insurance Service, Fabasoft Austria,
Greentube, IAEA (UN), Interwetten, INTERSPORT, Kapsch, Linz
AG, Mayr-Melnhof Karton, NÖM, ÖBB, OMV, Österreichische
Post, RACON West Software, Raiffeisen Bankengruppe,
Raiffeisen Informatik, S.Spitz, Salzburg AG, Siemens, TIWAG,
UC4, Vinzenz Gruppe, Wiener Zeitung, Wirtschaftskammer
Österreich; Bulgaria: MobilTel;
Czech Republic: Česká pojišťovna, ČSOB Bank, GTS
Novera, Telefónica O2, Vodafone; Denmark:
Danfoss, Egmont, NNIT; Finland: Cap Gemini,
Kemira, Pohjola; Germany: JAM Software, SAP
AG; Netherlands: De Nederlandsche Bank;
Norway: Acta, Aker Solutions, Det Norske
Veritas(DNV), Komplett; Russia: Mediatel;
Serbia: Tarkett; Slovakia:
Adastra Corporation, VÚB Banka; Slovenia:
Bankart, Iskratel, KDD, Krka, Mobitel, Pošta Slovenije,
Sava, SRC.SI; South Africa: De Beers,
Kumba, Sasol; Spain: CIE Automotive, Gas
Natural, ITP; Sweden: Sony Ericsson;
Switzerland: F. Hoffmann-La Roche, Philip
Morris International; UAE: Dubai Customs.
For past consulting engagement feedbacks click
here.
COPYRIGHT AND TRADEMARK NOTICES:
The copyright in all material provided on this site is held
by SQLWorkshops.com or by the original creator of the material.
Except as stated herein, none of the material may be copied,
reproduced, distributed, republished, downloaded, displayed,
posted or transmitted in any form or by any means, including,
but not limited to, electronic, mechanical, photocopying,
recording, or otherwise, without the prior written permission of
SQLWorkshops.com or the copyright owner. Permission is granted
to download the webcast on this Site for personal,
non-commercial use only, provided you do not modify the webcast
and that you retain all copyright and other proprietary notices
contained in the webcast. You also may not, without
SQLWorkshops.com permission, "mirror" any webcast contained on
this Site on any other server. This permission terminates
automatically if you breach any of these terms or conditions.
Upon termination, you must immediately destroy any downloaded
webcast. Any unauthorized use of any webcast contained on this
Site may violate copyright laws, trademark laws, the laws of
privacy and publicity, and communications regulations and
statutes.
Microsoft is a registered trademark of Microsoft Corporation in
the United States and/or other countries. SQL Server is a
trademark of Microsoft Corporation in the United States and/or
other countries. This webcast may refer to organizations and
products that may be the trademarks or registered trademarks of
their various owners.
You may freely use the ideas and concepts discussed in this
webcast, but you may not claim any of it as your own work. You
may quote freely, with acknowledgements to SQLWorkshops.com.
This article is for informational purposes only; you use any of
the suggestions given here entirely at your own risk.
|