SQL Server 2017 Query Performance Tuning: troubleshoot and optimize query performance Fifth Edition Grant Fritchey All Chapters Instant Download
SQL Server 2017 Query Performance Tuning: troubleshoot and optimize query performance Fifth Edition Grant Fritchey All Chapters Instant Download
com
https://textbookfull.com/product/sql-server-2017-query-
performance-tuning-troubleshoot-and-optimize-query-
performance-fifth-edition-grant-fritchey/
OR CLICK BUTTON
DOWNLOAD NOW
Query Store for SQL Server 2019 1st Edition Tracy Boggiano
Grant Fritchey
https://textbookfull.com/product/query-store-for-sql-server-2019-1st-
edition-tracy-boggiano-grant-fritchey/
textboxfull.com
https://textbookfull.com/product/sql-server-execution-plans-for-sql-
server-2008-through-to-2017-and-azure-sql-database-3rd-edition-grant-
fritchey/
textboxfull.com
https://textbookfull.com/product/biztalk-server-2016-performance-
tuning-and-optimization-agustin-mantaras/
textboxfull.com
Query Store for SQL Server 2019: Identify and Fix Poorly
Performing Queries 1st Edition Tracy Boggiano
https://textbookfull.com/product/query-store-for-sql-
server-2019-identify-and-fix-poorly-performing-queries-1st-edition-
tracy-boggiano/
textboxfull.com
SQL Cookbook: Query Solutions and Techniques for All SQL
Users 2nd Edition Anthony Molinaro
https://textbookfull.com/product/sql-cookbook-query-solutions-and-
techniques-for-all-sql-users-2nd-edition-anthony-molinaro/
textboxfull.com
https://textbookfull.com/product/learning-apache-drill-query-and-
analyze-distributed-data-sources-with-sql-1st-edition-charles-givre/
textboxfull.com
https://textbookfull.com/product/high-performance-sql-server-the-go-
faster-book-1st-edition-benjamin-nevarez-auth/
textboxfull.com
https://textbookfull.com/product/high-performance-sql-server-
consistent-response-for-mission-critical-applications-2nd-edition-
benjamin-nevarez/
textboxfull.com
SQL Server 2017
Query Performance
Tuning
Troubleshoot and Optimize Query
Performance
—
Fifth Edition
—
Grant Fritchey
SQL Server 2017 Query
Performance Tuning
Troubleshoot and Optimize Query
Performance
Fifth Edition
Grant Fritchey
SQL Server 2017 Query Performance Tuning
Grant Fritchey
Grafton, Massachusetts, USA
Introduction����������������������������������������������������������������������������������������������������������xxxi
v
Table of Contents
vi
Table of Contents
vii
Table of Contents
Context Switches/Sec������������������������������������������������������������������������������������������������������������ 72
Batch Requests/Sec�������������������������������������������������������������������������������������������������������������� 73
SQL Compilations/Sec����������������������������������������������������������������������������������������������������������� 73
SQL Recompilations/Sec������������������������������������������������������������������������������������������������������� 73
Other Tools for Measuring CPU Performance������������������������������������������������������������������������������ 74
Sys.dm_os_wait_stats���������������������������������������������������������������������������������������������������������� 74
Sys.dm_os_workers and Sys.dm_os_schedulers����������������������������������������������������������������� 74
Query Store���������������������������������������������������������������������������������������������������������������������������� 74
Measure CPU Behavior in Linux��������������������������������������������������������������������������������������������� 75
Processor Bottleneck Resolutions���������������������������������������������������������������������������������������������� 75
Optimizing Application Workload������������������������������������������������������������������������������������������� 75
Eliminating Excessive Compiles/Recompiles������������������������������������������������������������������������ 76
Using More or Faster Processors������������������������������������������������������������������������������������������ 76
Not Running Unnecessary Software�������������������������������������������������������������������������������������� 76
Network Bottleneck Analysis������������������������������������������������������������������������������������������������������ 77
Bytes Total/Sec���������������������������������������������������������������������������������������������������������������������� 77
% Net Utilization�������������������������������������������������������������������������������������������������������������������� 78
Network Bottleneck Resolutions������������������������������������������������������������������������������������������������� 78
Optimizing Application Workload������������������������������������������������������������������������������������������� 79
SQL Server Overall Performance������������������������������������������������������������������������������������������������ 79
Missing Indexes��������������������������������������������������������������������������������������������������������������������� 80
Database Concurrency���������������������������������������������������������������������������������������������������������� 82
Nonreusable Execution Plans������������������������������������������������������������������������������������������������ 83
General Behavior������������������������������������������������������������������������������������������������������������������� 84
User Connections������������������������������������������������������������������������������������������������������������������ 84
Batch Requests/Sec�������������������������������������������������������������������������������������������������������������� 84
Summary������������������������������������������������������������������������������������������������������������������������������������ 85
viii
Table of Contents
ix
Table of Contents
x
Table of Contents
xi
Table of Contents
xii
Table of Contents
xiii
Table of Contents
Recommendations�������������������������������������������������������������������������������������������������������������������� 400
Backward Compatibility of Statistics����������������������������������������������������������������������������������� 400
Auto Create Statistics���������������������������������������������������������������������������������������������������������� 401
Auto Update Statistics��������������������������������������������������������������������������������������������������������� 401
Automatic Update Statistics Asynchronously���������������������������������������������������������������������� 401
Amount of Sampling to Collect Statistics���������������������������������������������������������������������������� 402
Summary���������������������������������������������������������������������������������������������������������������������������������� 402
xiv
Table of Contents
Optimization������������������������������������������������������������������������������������������������������������������������ 457
Execution Plan Caching������������������������������������������������������������������������������������������������������� 468
Components of the Execution Plan������������������������������������������������������������������������������������������� 468
Query Plan��������������������������������������������������������������������������������������������������������������������������� 468
Execution Context���������������������������������������������������������������������������������������������������������������� 469
Aging of the Execution Plan������������������������������������������������������������������������������������������������������ 469
Summary���������������������������������������������������������������������������������������������������������������������������������� 470
xvi
Table of Contents
xvii
Table of Contents
Locks����������������������������������������������������������������������������������������������������������������������������������������� 641
Lock Granularity������������������������������������������������������������������������������������������������������������������ 641
Row-Level Lock������������������������������������������������������������������������������������������������������������������� 642
Key-Level Lock�������������������������������������������������������������������������������������������������������������������� 643
Page-Level Lock������������������������������������������������������������������������������������������������������������������ 645
Extent-Level Lock���������������������������������������������������������������������������������������������������������������� 645
Heap or B-tree Lock������������������������������������������������������������������������������������������������������������ 645
Table-Level Lock������������������������������������������������������������������������������������������������������������������ 646
Database-Level Lock����������������������������������������������������������������������������������������������������������� 647
Lock Operations and Modes������������������������������������������������������������������������������������������������������ 647
Lock Escalation������������������������������������������������������������������������������������������������������������������� 647
Lock Modes������������������������������������������������������������������������������������������������������������������������� 648
Exclusive (X) Mode�������������������������������������������������������������������������������������������������������������� 656
Intent Shared (IS), Intent Exclusive (IX), and Shared with Intent Exclusive (SIX) Modes����� 657
Schema Modification (Sch-M) and Schema Stability (Sch-S) Modes���������������������������������� 658
Bulk Update (BU) Mode�������������������������������������������������������������������������������������������������������� 658
Key-Range Mode����������������������������������������������������������������������������������������������������������������� 659
Lock Compatibility��������������������������������������������������������������������������������������������������������������� 659
Isolation Levels������������������������������������������������������������������������������������������������������������������������� 659
Read Uncommitted�������������������������������������������������������������������������������������������������������������� 660
Read Committed������������������������������������������������������������������������������������������������������������������ 661
Repeatable Read����������������������������������������������������������������������������������������������������������������� 663
Serializable�������������������������������������������������������������������������������������������������������������������������� 667
Snapshot����������������������������������������������������������������������������������������������������������������������������� 674
Effect of Indexes on Locking����������������������������������������������������������������������������������������������������� 675
Effect of a Nonclustered Index�������������������������������������������������������������������������������������������� 676
Effect of a Clustered Index�������������������������������������������������������������������������������������������������� 678
Effect of Indexes on the Serializable Isolation Level����������������������������������������������������������� 679
Capturing Blocking Information������������������������������������������������������������������������������������������������ 680
Capturing Blocking Information with SQL��������������������������������������������������������������������������� 681
Extended Events and the blocked_process_report Event��������������������������������������������������� 684
xviii
Table of Contents
xix
Table of Contents
Chapter 25: Automated Tuning in Azure SQL Database and SQL Server��������������� 783
Automatic Plan Correction�������������������������������������������������������������������������������������������������������� 784
Tuning Recommendations��������������������������������������������������������������������������������������������������� 785
Enabling Automatic Tuning�������������������������������������������������������������������������������������������������� 792
Azure SQL Database Automatic Index Management����������������������������������������������������������������� 799
Adaptive Query Processing������������������������������������������������������������������������������������������������������� 810
Batch Mode Memory Grant Feedback��������������������������������������������������������������������������������� 810
Interleaved Execution���������������������������������������������������������������������������������������������������������� 815
Summary���������������������������������������������������������������������������������������������������������������������������������� 823
xx
Table of Contents
xxi
Table of Contents
xxii
Table of Contents
Index��������������������������������������������������������������������������������������������������������������������� 911
xxiii
About the Author
Grant Fritchey, Microsoft Data Platform MVP, has more than 20 years of experience
in IT. That time was spent in technical support, development, and database
administration. He currently works as a product evangelist at Red Gate Software.
Grant writes articles for publication at SQL Server Central and Simple-Talk. He has
published books, including SQL Server Execution Plans and SQL Server 2012 Query
Performance Tuning (Apress). He has written chapters for Beginning SQL Server 2012
Administration (Apress), SQL Server Team-based Development, SQL Server MVP Deep
Dives Volume 2, Pro SQL Server 2012 Practices (Apress), and Expert Performance Indexing
in SQL Server (Apress). Grant currently serves as the president on the board of directors
of the PASS organization, the leading source of educational content and training on the
Microsoft data platform.
xxv
About the Technical Reviewer
Joseph Sack is a principal program manager at Microsoft,
focusing on query processing for Azure SQL Database and
SQL Server. He has worked as a SQL Server professional
since 1997 and has supported and developed for SQL
Server environments in financial services, IT consulting,
manufacturing, retail, and the real estate industry.
Joe joined Microsoft in 2006 and was a SQL Server
premier field engineer for large retail customers in
Minneapolis, Minnesota. He was responsible for
providing deep SQL Server advisory services, training,
troubleshooting, and ongoing solutions guidance. In 2006
Joe earned the Microsoft Certified Master: SQL Server 2005
certification, and in 2008 he earned the Microsoft Certified
Master: SQL Server 2008 certification. In 2009 he took over
responsibility for the entire SQL Server Microsoft Certified Master program and held that
post until 2011.
He left Microsoft in late 2011 to join SQLskills, working as a principal consultant.
During that time, he co-instructed for various training events and was a consultant
for customer performance tuning engagements. He recorded 13 Pluralsight courses,
including SQL Server: Troubleshooting Query Plan Quality Issues, SQL Server: Transact-
SQL Basic Data Retrieval, and SQL Server: Common Query Tuning Problems and
Solutions. He returned to Microsoft in 2015.
Over the years Joe has published and edited several SQL Server books and
white papers. His first book, SQL Server 2000 Fast Answers for DBAs and Developers,
was published in 2003. He also started and maintained the T-SQL Recipe series,
including SQL Server 2005 T-SQL Recipes and SQL Server 2008 Transact-SQL Recipes.
His most popular white papers include “Optimizing Your Query Plans with the SQL
Server 2014 Cardinality Estimator” and “AlwaysOn Architecture Guide: Building a High
Availability and Disaster Recovery Solution by Using Failover Cluster Instances and
xxvii
About the Technical Reviewer
Availability Groups.” Currently he writes (along with his colleagues) on the SQL Server
Engine Blog. His classic posts can still be found at https://www.sqlskills.com/blogs/
joe/ and https://blogs.msdn.microsoft.com/joesack/.
His Twitter handle is @JoeSackMSFT, and you can find Joe speaking at most major
SQL Server conferences. He spends half his time between Minneapolis and Seattle,
meaning that he is either cold or wet at any given point in time.
xxviii
Acknowledgments
The poor editors at Apress have to put up with me and my bad scheduling, so first and
foremost, thanks to Jill Balzano and Jonathan Gennick. To say I couldn’t have done it
without you doesn’t begin to cover it. I’ve said it before and I’ll say it again here, publicly
and forever in print, Joe Sack is my hero. Thanks for everything, Joe.
xxix
Introduction
Technology is changing all the time, faster and faster. New functionality is introduced
in Azure SQL Database on an almost weekly schedule, and SQL Server itself has gone
through two releases since the last edition of this book was published. New styles of
databases are introduced all the time. With all this change, the question immediately in
front of you should be, do we still need to do query tuning?
The answer is a very short and resounding, yes.
With all the functionality and capability built into SQL Server and the Azure Data
Platform, not only is query tuning still an important skill, it actually becomes a way to
save your organization money. Knowing how to make a query run faster so that fewer
resources are needed literally becomes a way to reduce costs within a platform-as-a-
service offering such as Azure SQL Database.
However, it’s not just about money. The code generated by object-relational mapping
tools such as Entity Framework can be fantastic, until it isn’t. Then, you’ll be working on
creating custom scripts and generating data structures and indexes and all the rest of
traditional query performance tuning.
While technology has certainly moved fast and far, there is still a fundamental need
to get queries to run faster and do more with less overhead on your servers. That’s where
this book comes into play. This is a resource that you can use to ensure that you’re using
all the tools in your hands to ensure that the databases you build, develop, and maintain
will continue to run faster.
1
© Grant Fritchey 2018
G. Fritchey, SQL Server 2017 Query Performance Tuning, https://doi.org/10.1007/978-1-4842-3888-2_1
Another Random Scribd Document
with Unrelated Content
Paulina's prediction came true, and we were soon busy preparing for her
father's marriage with Miss Cottrell. It took place in our beautiful old church on
the thirty-first of July. The happy pair spent a week at Felixstowe and then
came back to "Gay Bowers" to fetch Pollie. It was with genuine regret that
Aunt Patty and I watched Mr. and Mrs. Dicks and Paulina take their departure.
How different were our feelings now from those with which we had received
the Americans and Miss Cottrell! The paying guests had become our friends.
"Au revoir!" cried Pollie as they drove away. "We are coming back some day.
And, Mr. Faulkner, please don't forget that you are going to bring Nan to
Indianapolis some time."
We watched them pass out of our sight with the sadness most partings
inevitably bring, for who could say whether we should all meet again?
Two days later, Alan's sisters came to spend their holidays at "Gay Bowers."
They were such nice, bright girls that I had no difficulty in making friends of
them, and I am thankful to say they seemed to take to me at once. The
brother, who was their guardian, was so great a hero in their eyes, that I
wonder they thought me good enough for him. It must have been, because
they thought he could not make a wrong choice.
Peggy joined us ere August was far advanced, and we became a very lively
party. By this time Jack had returned to the vicarage. I had the satisfaction of
seeing that Aunt Patty had rightly gauged the depth of his wound. If the news
of my engagement to Alan Faulkner hurt him, the blow was one from which he
quickly recovered. He and Peggy became good comrades; she wanted to
practise sketching during her stay in the country and he helped her to find
suitable "bits," and was her attendant squire on many of her expeditions.
I had heard nothing from Agneta since her return to Manchester, but the news
of my engagement brought me a kind though rather sad letter from her. She
said she thought that I and Professor Faulkner were exactly suited to each
other and she was glad I was going to be happy, for I deserved happiness and
she supposed she never had. She knew now that she had been utterly
deluded when she imagined that Ralph Marshman would make her happy.
She wanted me to know that she was convinced of his worthless character
and of what an escape she had had. She thanked me for the efforts I had
made to save her from her own folly, and she begged me to forgive her for
being so ungrateful at the time. She said she was sick of her life at home. She
wanted her parents to let her adopt a career of her own and live a more useful
life, but her mother refused to entertain the idea for a moment.
"I am trying to be patient," Agneta wrote; "You know you were always
preaching patience to me, Nan; and I mean to do some 'solid' reading every
day. Do send me a list of books you think I ought to read. I know, although you
never said so, that you thought me very ignorant when I was with you. I don't
forget either how you once said that I never should be happy as long as I
made myself the centre of my life. So I try to be unselfish and to think of other
people, but there is really very little I can do for others in the life I lead here. I
almost envy girls who have to work for themselves."
I felt very sorry for Agneta as I read her letter, and yet I should have been
glad, for, if her words were sincere, they augured for her happier days than
she had yet known. For what hope of happiness is there for any one who is
shut up in the prison-house of self? It was good for Agneta, as it had been for
me and for Paulina, to suffer, if her trouble had led her into a larger, fuller, and
more blessed life.
But the story of Aunt Patty's guests, as far as I have known them intimately,
must be brought to a close. After all, I did not stay quite twelve months at "Gay
Bowers." I went home for Christmas and I did not return. There was no longer
any thought of my going up for Matriculation. Even now I regret that I never
did so, but mother was bent upon my entering on a course of domestic
economy, and the value of that study I am daily proving.
Early in the New Year, Olive was married. It was a very pretty wedding and
everything went off charmingly; but her departure for India six weeks later left
us all with very sore hearts. Alan was duly appointed to the professorship at
Edinburgh, and now my home is in that beautiful old city, for in the following
year, at the beginning of the summer vacation, we were married.
I should like to write about that wedding, but Alan thinks I had better not begin.
My three sisters, Alan's two, and Cousin Agneta were my bridesmaids. Mr.
Upsher assisted at the ceremony, and Jack, such a handsome young soldier,
was one of the guests. He still showed himself devoted to Peggy, but I hope
he is not seriously attracted by her, for Peggy declares that she is wedded to
her art and is quite angry if any one suggests that she may marry. She is now
working hard in Paris and promises to develop into a first-rate artist in "black
and white."
Agneta made a very pretty bridesmaid and looked as happy as one could
wish. I say this on mother's authority, for really I cannot remember how any
one looked except Alan. The sun must have been in my eyes all the time, for
my recollection of everything is so vague and hazy. So it was wise of Alan to
advise me not to attempt to describe our wedding. Soon afterwards we heard
of Agneta's engagement, with her parents' approval, to a young medical man,
so I dare say she did look happy.
Alan and I always agree that "Gay Bowers" is the most delightful old country
house we have ever known. Apparently many are of the same opinion, for
aunt seldom has a room to spare in it.
THE END
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth in
paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
textbookfull.com