|
Plan Freezing Flying with Plan Freezing – Mostly you experience thrust or stay afloat, but rarely this add-on can be a drag:
Plan freezing is an
interesting feature, targeting plan stability, performance
predictability. We all want stability, especially during the
good times. There are exceptions, there are always exceptions,
that’s what software testing is all about, there is no software
without flaws, you have to learn to identify and navigate around
the flaws. Most of the time these flaws will be eventually
corrected (sometimes they do get marked ‘By Design/Feature’!),
but a mission critical application can’t wait for the service
pack or hotfix, it needs to perform today, now!
To read additional articles I wrote
click
here.
As I mentioned in the
previous article ‘Prefetch – Querying at the speed of SAN’, plan
freezing will not help you with estimation. SQL Server will
still estimate based on current set of parameter values (compile
time parameter values) and not based on the plan you use for
freezing.
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.
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.
Optimized Bitmap Filtering is an
interesting feature, if you execute the below query with MAXDOP
1, Bitmap Filtering will not happen and the query will consume
more CPU resource.
You have to execute all the below
example queries with parallelism enabled (with MAXDOP 0 or > 1).
I did not use hint OPTION (MAXDOP 0) as my SQL Server
Configuration Parameter ‘max degree of parallelism’ is set to 0.
Here is a query that uses the Optimized
Bitmap Filter (that’s new in SQL Server 2008):
set
statistics
time on
go
--Example provided by www.sqlworkshops.com
declare
@i int
select
@i = f1.c1
from Fact1 f1
inner
join Dim1 d1
on (f1.c2
= d1.c2)
inner
join Dim2 d2
on (f1.c3
= d2.c2)
where
d1.c3 between 100000
and 110000
and d2.c3
between 200000
and
210000
go
--The
above query takes 201ms of CPU

--There
are 2 Optimized Bitmap Filters
--eliminating non qualifying rows from
--table Dim1(Fact1.c2) and
Dim2(Fact1.c3)

--Hash join bottom input
has only 63 rows
 You can test Plan Freezing this with
sp_create_plan_guide or sp_create_plan_guide_from_handle or
simply with USE PLAN hint. We will use ‘USE PLAN’ hint.
You need to get the XML plan from the above
query and then use than XML Plan in the below query. Make sure
you disable 'Include Actual Execution Plan' in SQL Server
Management Studio to get the XML plan.
--Example provided by www.sqlworkshops.com
--You
need to turn off 'Include Actual Execution Plan'
--or
Graphical plan and then execute
--set
set statistics xml on
--to
get the xml plan
--in
SQL Server Management Studio.
set
statistics
xml on
go
--Example provided by www.sqlworkshops.com
--let’s
execute the above query again
declare
@i int
select
@i = f1.c1
from Fact1 f1
inner
join Dim1 d1
on (f1.c2
= d1.c2)
inner
join Dim2 d2
on (f1.c3
= d2.c2)
where
d1.c3 between 100000
and 110000
and d2.c3
between 200000
and
210000
go
 Right click on the XML Plan, do not left
click, right click and choose Copy and paste the XML Plan in the
OPTION (USE PLAN ‘<...XMLPlan...>’) clause between
‘<...XMLPlan...>’.
--Example provided by www.sqlworkshops.com
--Cut
and paste the above xml plan here
declare
@i int
select
@i = f1.c1
from Fact1 f1
inner
join Dim1 d1
on (f1.c2
= d1.c2)
inner
join Dim2 d2
on (f1.c3
= d2.c2)
where
d1.c3 between 100000
and 110000
and d2.c3
between 200000
and
210000
option
(use
plan
'<...XMLPlan...>')
go
--The
above query takes 331ms of CPU

--Notice There is only one Bitmap Filter
--not
the 2008 Optimized Bitmap Filter
--eliminating non qualifying rows only
--from
table Dim2(c3) and not from
--table Dim1(c2)

--Hash
join bottom input has only 212835 rows
--to
join instead of 63 rows like before
Duration may not be much different in our
example even though we are joining less rows. This is because we
have not that many rows like a real world OLAP database and we
are using integer columns for the join and not large columns.
 Remember the last time, you found
something good, you liked it, you froze it and then it didn’t
taste as good as it was before. Well this can happen with real
world SQL Server queries as well, keep your eyes open and keep
learning.
I recommend you
to watch my webcasts (www.sqlworkshops.com/webcasts).
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.
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.
|