SlideShare a Scribd company logo
1 of 46
Download to read offline
© 2020 Brent Ozar Unlimited®. All rights reserved. 1
The New Robots in
SQL Server 2017 & 2019
99-05: dev, architect, DBA
05-08: DBA, VM, SAN admin
08-10: Microsoft Certified Master
Since: consulting DBA
www.BrentOzar.com
Help@BrentOzar.com
© 2020 Brent Ozar Unlimited®. All rights reserved. 2
I keep hearing
about these robots,
and about how they’re gonna take my job.
I have clients with
SQL Server 2019.
I’ve never heard any of them say,
“You’re fired.The robots fixed it allfor me.”
© 2020 Brent Ozar Unlimited®. All rights reserved. 3
You’re probably not going to
disable them, but…
2017 & 2019’s robots are kinda
like your phone’s AutoCorrect.
© 2020 Brent Ozar Unlimited®. All rights reserved. 4
Batch Mode
Execution
Reporting-style query on 2017:
© 2020 Brent Ozar Unlimited®. All rights reserved. 5
Lots of reads, lots of CPU work.
Change the compat level to 2019:
© 2020 Brent Ozar Unlimited®. All rights reserved. 6
Same reads, but less CPU & time!
2017 was:
2019:
© 2020 Brent Ozar Unlimited®. All rights reserved. 7
It’s not just a simpler plan:
2017 plan has two parallelism zones:
2019 plan – only one parallelism zone:
It’s different operators, really:
2017’s table scan:
2019’s:
© 2020 Brent Ozar Unlimited®. All rights reserved. 8
2017 & prior: row mode execution
Each plan operator is a mini-program
It runs in isolation
It passes data to the next operator in the form of rows
Then came columnstore indexes.
The data is stored differently.
I haven’t written a class on these yet, but start here:
Paper: Query Execution in Column-Oriented Database
Systems by Daniel J. Abadi:
http://www.cs.umd.edu/~abadi/papers/abadiphd.pdf
Niko Neugebauer 100+part blog series:
http://www.nikoport.com/columnstore/
© 2020 Brent Ozar Unlimited®. All rights reserved. 9
Why this mattered
Think of it as a separate index on each column:
But here’s the trick question:
when do you reassemble these columns
into their original rows?
Batch mode execution means
This operator isn’t rebuilding the rows yet:
it’s passing groups of columns to the next operator.
© 2020 Brent Ozar Unlimited®. All rights reserved. 10
Like an index on every column
Think of it as a separate index on each column:
But here’s the trick question:
when do you reassemble these columns
into their original rows?
So many cool implications
Batches of columnar data can be:
• Way smaller due to duplication
(only unique values are stored)
• Way easier to group/sum/sort on
(since so much is duplicated)
Result: massively less CPU work
for reporting queries (table scans
with grouping, summing, formulas.)
© 2020 Brent Ozar Unlimited®. All rights reserved. 11
The first challenge
Which of these plans is getting batch mode?
And which of the operators are getting it?
More challenges
If you got batch mode on an operator:
• Is it working out in your favor?
• Why was it chosen?
• How can you change it?
If you didn’t get batch mode:
• Why not?
• Can you change things to get it?
© 2020 Brent Ozar Unlimited®. All rights reserved. 12
Still, I like it.
A lot.
Columnstore gets it in Standard, but it just works on
columnstore indexes, period. No big gotchas there.
Rowstore tables only get it in Enterprise Edition.
I think that’s a perfect fit for batch mode:
• It makes the most sense in reports
• It makes the least sense in OLTP
© 2020 Brent Ozar Unlimited®. All rights reserved. 13
TVF Interleaved
Execution
Multi-Statement Table-Valued Functions
We covered why these suck:
• Hard to see impact in the query plan, stats IO
• Can run row-by-row
• Estimates are hard-coded garbage (100 or 1)
2017 can fix the latter by running the MSTVF first,
then passing the row estimates to the rest of the plan.
© 2020 Brent Ozar Unlimited®. All rights reserved. 14
It takes 42 seconds
© 2020 Brent Ozar Unlimited®. All rights reserved. 15
2017 compat mode: 7 seconds!
The difference
2016 estimates a hard-coded 100 rows from the TVF:
But 2017+ executes it,
and knows 20,057
rows will come out:
© 2020 Brent Ozar Unlimited®. All rights reserved. 16
Interleaved Execution
is like phone-a-friend.
Itgivesaheadsuptotherestoftheplanthata
wholelotofrowsareincoming,therebyimproving
memorygrantaccuracy.That’sgreat!
You still shouldn’t be
using MSTVFs though.
Whileweknowthenumberofrowscomingout,
westilldon’thavegreatstatsontheircontents.
© 2020 Brent Ozar Unlimited®. All rights reserved. 17
The sort & hash match both spill.
BadBad
TVF interleaved execution is fine.
It makes a bad thing (TVFs) suck less.
Scalar function inlining, that’s different.
© 2020 Brent Ozar Unlimited®. All rights reserved. 18
Scalar Function Inlining
OneofthemostambitiousthingsI’ve
everseeninadatabaseserver.
The idea
Take scalar user-defined functions that have always been
designed to run on a row-by-row basis
1. Rewrite the UDFs automatically into set-based
functions (think: turn them into a view you can join to)
2. Rewrite queries calling the UDFs so that they can
leverage the set-based version
White paper: https://arxiv.org/abs/1712.00498
(Click the PDF link in the Download box)
© 2020 Brent Ozar Unlimited®. All rights reserved. 19
This paper is fun (for me) to read
© 2020 Brent Ozar Unlimited®. All rights reserved. 20
I was amazed that this shipped.
It’s the most ambitious thing I’ve seen in SQL Server.
Other features were harder and more complex:
CLR, Hekaton, Polybase, Big Data Clusters.
But those had a limited blast radius because they
required you to change your code, tables, indexes.
This feature was supposed to work
without you changing anything at all.
Well, that was the theory.
But they keep finding bugs with it, and disabling it:
© 2020 Brent Ozar Unlimited®. All rights reserved. 21
At first, they fixed bugs…
CU2 had bug fixes, but after that, they just started
disabling scalar function inlining in more scenarios…
© 2020 Brent Ozar Unlimited®. All rights reserved. 22
And even when it “works”
It doesn’t always speed up queries, as we saw earlier.
Thus my verdict:
It might work for you, but…
I’ve seen it backfire, HARD.
Taryn is Stack
Overflow’s DBA.
It’s been rough seeing
them struggle with 2019
performance issues.
© 2020 Brent Ozar Unlimited®. All rights reserved. 23
Adaptive
Memory Grants
Query workspace memory grants
It’s really hard for SQL Server to get estimates right.
It’s not SQL Server’s fault:
it’s usually our crappy queries and indexes.
SQL Server does track how often spills occur for a
given query plan, but it just doesn’t do anything with
that data.
© 2020 Brent Ozar Unlimited®. All rights reserved. 24
https://docs.microsoft.com/en-us/sql/relational-
databases/performance/adaptive-query-processing
© 2020 Brent Ozar Unlimited®. All rights reserved. 25
Seeing it in action
Build our stored proc that is vulnerable to sniffing:
Then run it back & forth…
© 2020 Brent Ozar Unlimited®. All rights reserved. 26
That’s a big spill.
2017 compat level:
83,481 pages spilled to TempDB.
(And the query’s slow, duh.)
Let’s try 2019 compat level.
© 2020 Brent Ozar Unlimited®. All rights reserved. 27
2019? Bad news.
@Rep = 1 still gets the tiny
data plan – that’s fair, MS
didn’t say they fixed that.
But it also gets the tiny
grant at first, as you can
see by hovering over the
sort to measure spills.
IT GOT WORSE
Spills 167,303 pages to
TempDB!
But hang on: this is where it
gets better – run it again
with @Rep = 1.
© 2020 Brent Ozar Unlimited®. All rights reserved. 28
No spills!
Adaptive memory
grants started raising
the memory grant.
How it works
After a query finishes, SQL reviews its memory use.
• If it spilled to disk, it didn’t get enough RAM.
Raise its desired memory grant for next time.
• If it left a lot of RAM unused, it got too much.
Lower its desired memory grant for next time.
© 2020 Brent Ozar Unlimited®. All rights reserved. 29
How it works, translated
The amount of memory your query gets is based on
the parameters that the last person used.
If the last person asked for big data, you get big RAM.
If they asked for small data, you get small RAM.
So what’s gonna happen now?
I’m about to run it for reputation = 2. What happens?
© 2020 Brent Ozar Unlimited®. All rights reserved. 30
Runs fast...
Uh oh
© 2020 Brent Ozar Unlimited®. All rights reserved. 31
You see this coming, don’t you?
Back to spilling
The “adaptive” grant
doesn’t actually remember
anything – it just adapts to
the LAST person’s params.
And how big is the spill?
© 2020 Brent Ozar Unlimited®. All rights reserved. 32
167,115 pages.
Worse than it was before.
<sigh>
© 2020 Brent Ozar Unlimited®. All rights reserved. 33
Adaptive grants for rowstore are rough right now:
• No proactive warning when it’s happening
• Minimal instrumentation
• Monitoring tools don’t cover it
• Resets constantly on index rebuilds, stats
updates, failovers, restarts
Yet they’re on by default in compat 150.
ALTER DATABASE SCOPED CONFIGURATION
SET BATCH_MODE_MEMORY_GRANT_FEEDBACK = OFF;
ALTER DATABASE SCOPED CONFIGURATION
SET ROW_MODE_MEMORY_GRANT_FEEDBACK = OFF;
Because I know you’re gonna ask
© 2020 Brent Ozar Unlimited®. All rights reserved. 34
Adaptive Joins
Adaptive join example
You need to join between two tables.
(Not 2 indexes: this doesn’t fix key lookups.)
When the driver table returns just a few rows,
then it makes sense to do nested loops:
one operation per row that it finds.
When the driver table returns a lot of rows,
then it makes sense to do a hash join:
scan the other table and check all the rows.
© 2020 Brent Ozar Unlimited®. All rights reserved. 35
Sample query
1: the robot knows output rows might vary.
2: so nested loops OR hash.
© 2020 Brent Ozar Unlimited®. All rights reserved. 36
Both operations are shown,
but only one will be executed.
Hash? Then we’ll scan all the data.
Nested loops? We’ll just seek a few rows.
© 2020 Brent Ozar Unlimited®. All rights reserved. 37
Try reputation 2,
and the seek runs.
Adaptive joins are rarely seen.
In 2017: requires a columnstore index, compat 140
In 2019: works with rowstore indexes, compat 150
Requires batch mode execution (which in 2017 means
a columnstore index somewhere in your query,
anywhere, even an empty one)
SELECT queries only (no DUIs)
A join that can work with nested loops or hash
© 2020 Brent Ozar Unlimited®. All rights reserved. 38
Adaptive Joins are OK.
They’rereallyhardtogettoday,andbythemselves,
theydon’tusuallygetyouacrossthefinishlinewith
nootherquerychanges.
Butthey’reOK.
But run them back to back…
Action Duration
DBCC FREEPROCCACHE
Run it for @Reputation = 1 1 second
Run it for @Reputation = 2 0 seconds
Run it for @Reputation = 1 again 1 minute 15 seconds
Why do you suppose that is?
© 2020 Brent Ozar Unlimited®. All rights reserved. 39
Ah, our “friend” adaptive grants.
25 second spill
22 second spill
Parameter sniffing is still hard.
One new feature (adaptive grants)
can wreak havoc on other new features.
This is called “feature interoperability.”
You’re lucky if new features are tested in isolation,
let alone combined with other features.
Plus, we still have good ol’ parameter sniffing:
adaptive joins may not show up for all params.
© 2020 Brent Ozar Unlimited®. All rights reserved. 40
© 2020 Brent Ozar Unlimited®. All rights reserved. 41
And there’s yet another plan:
Parameter sniffing is HARD now.
This is a very simple stored procedure, but:
Depending on which @Reputation it’s called with first
(1, 2, or 4), I can get 3 different plans in cache.
And then every single time it runs, it can set different
memory grants for the very next execution.
Plan changes are a huge problem in 2019.
© 2020 Brent Ozar Unlimited®. All rights reserved. 42
Automatic Tuning
(aka plan regression)
This feature name is a flat out lie.
Automatic Tuning implies that SQL Server is actively
doing something to make your query better.
It’s not even close.
© 2020 Brent Ozar Unlimited®. All rights reserved. 43
The sales pitch
Turn on Query Store (which is a story to begin with.)
Get a good query plan.
Have something go wrong: stats update, data change.
Get a bad query plan.
After a while, SQL Server will realize it’s a bad plan,
and try to revert to the good plan that was stored in
Query Store. If it’s faster, keep it permanently.
Scenarios it doesn’t help
If you never get a good plan in the first place
If you didn’t have Query Store already enabled when the
good plan was in effect
If the query changes (even by one space, like if you do a
deployment and just changed a comment)
If there’s no one good plan for all parameters (common)
This is not Automatic Tuning
in any way, shape, or form.
© 2020 Brent Ozar Unlimited®. All rights reserved. 44
Anyway, Microsoft
is off to a good start.
© 2020 Brent Ozar Unlimited®. All rights reserved. 45
I have high hopes. Done right:
This can reduce Microsoft’s hardware spend in Azure.
They might cut hosting costs to be more competitive
with Amazon RDS Aurora MySQL & PostgreSQL.
This can give Microsoft a very competitive advantage
over other on-premises databases, too.
Done right, there’s really no downside
(other than Microsoft’s costs to build it.)
© 2020 Brent Ozar Unlimited®. All rights reserved. 46
But today, most require
Enterprise Edition.
(Andtoday,Idon’thaveaproblemwiththatbecausethefeatures
aresolimited.If/whenopensourcecompetitorsstarttoeat
Microsoft’slunchintheStandardEditionmarket,theycanopenup
someofthesefeaturesatlowerpricetiers.)

More Related Content

More from IDERA Software

Idera live 2021: Keynote Presentation The Future of Data is The Data Cloud b...
Idera live 2021:  Keynote Presentation The Future of Data is The Data Cloud b...Idera live 2021:  Keynote Presentation The Future of Data is The Data Cloud b...
Idera live 2021: Keynote Presentation The Future of Data is The Data Cloud b...
IDERA Software
 
Idera live 2021: Database Auditing - on-Premises and in the Cloud by Craig M...
Idera live 2021:  Database Auditing - on-Premises and in the Cloud by Craig M...Idera live 2021:  Database Auditing - on-Premises and in the Cloud by Craig M...
Idera live 2021: Database Auditing - on-Premises and in the Cloud by Craig M...
IDERA Software
 

More from IDERA Software (20)

Idera live 2021: Why Data Lakes are Critical for AI, ML, and IoT By Brian Flug
Idera live 2021:  Why Data Lakes are Critical for AI, ML, and IoT  By Brian FlugIdera live 2021:  Why Data Lakes are Critical for AI, ML, and IoT  By Brian Flug
Idera live 2021: Why Data Lakes are Critical for AI, ML, and IoT By Brian Flug
 
Idera live 2021: Will Data Vault add Value to Your Data Warehouse? 3 Signs th...
Idera live 2021: Will Data Vault add Value to Your Data Warehouse? 3 Signs th...Idera live 2021: Will Data Vault add Value to Your Data Warehouse? 3 Signs th...
Idera live 2021: Will Data Vault add Value to Your Data Warehouse? 3 Signs th...
 
Idera live 2021: Managing Digital Transformation on a Budget by Bert Scalzo
Idera live 2021:  Managing Digital Transformation on a Budget by Bert ScalzoIdera live 2021:  Managing Digital Transformation on a Budget by Bert Scalzo
Idera live 2021: Managing Digital Transformation on a Budget by Bert Scalzo
 
Idera live 2021: Keynote Presentation The Future of Data is The Data Cloud b...
Idera live 2021:  Keynote Presentation The Future of Data is The Data Cloud b...Idera live 2021:  Keynote Presentation The Future of Data is The Data Cloud b...
Idera live 2021: Keynote Presentation The Future of Data is The Data Cloud b...
 
Idera live 2021: Managing Databases in the Cloud - the First Step, a Succes...
Idera live 2021:   Managing Databases in the Cloud - the First Step, a Succes...Idera live 2021:   Managing Databases in the Cloud - the First Step, a Succes...
Idera live 2021: Managing Databases in the Cloud - the First Step, a Succes...
 
Idera live 2021: Database Auditing - on-Premises and in the Cloud by Craig M...
Idera live 2021:  Database Auditing - on-Premises and in the Cloud by Craig M...Idera live 2021:  Database Auditing - on-Premises and in the Cloud by Craig M...
Idera live 2021: Database Auditing - on-Premises and in the Cloud by Craig M...
 
Idera live 2021: Performance Tuning Azure SQL Database by Monica Rathbun
Idera live 2021:  Performance Tuning Azure SQL Database by Monica RathbunIdera live 2021:  Performance Tuning Azure SQL Database by Monica Rathbun
Idera live 2021: Performance Tuning Azure SQL Database by Monica Rathbun
 
Geek Sync | How to Be the DBA When You Don't Have a DBA - Eric Cobb | IDERA
Geek Sync | How to Be the DBA When You Don't Have a DBA - Eric Cobb | IDERAGeek Sync | How to Be the DBA When You Don't Have a DBA - Eric Cobb | IDERA
Geek Sync | How to Be the DBA When You Don't Have a DBA - Eric Cobb | IDERA
 
How Users of a Performance Monitoring Tool Can Benefit from an Inventory Mana...
How Users of a Performance Monitoring Tool Can Benefit from an Inventory Mana...How Users of a Performance Monitoring Tool Can Benefit from an Inventory Mana...
How Users of a Performance Monitoring Tool Can Benefit from an Inventory Mana...
 
Benefits of Third Party Tools for MySQL | IDERA
Benefits of Third Party Tools for MySQL | IDERABenefits of Third Party Tools for MySQL | IDERA
Benefits of Third Party Tools for MySQL | IDERA
 
Achieve More with Less Resources | IDERA
Achieve More with Less Resources | IDERAAchieve More with Less Resources | IDERA
Achieve More with Less Resources | IDERA
 
Benefits of SQL Server 2017 and 2019 | IDERA
Benefits of SQL Server 2017 and 2019 | IDERABenefits of SQL Server 2017 and 2019 | IDERA
Benefits of SQL Server 2017 and 2019 | IDERA
 
Be Proactive: A Good DBA Goes Looking for Signs of Trouble | IDERA
Be Proactive: A Good DBA Goes Looking for Signs of Trouble | IDERABe Proactive: A Good DBA Goes Looking for Signs of Trouble | IDERA
Be Proactive: A Good DBA Goes Looking for Signs of Trouble | IDERA
 
Advanced SQL Server Performance Tuning | IDERA
Advanced SQL Server Performance Tuning | IDERAAdvanced SQL Server Performance Tuning | IDERA
Advanced SQL Server Performance Tuning | IDERA
 
Geek Sync | Planning a SQL Server to Azure Migration in 2021 - Brent Ozar
Geek Sync | Planning a SQL Server to Azure Migration in 2021 - Brent OzarGeek Sync | Planning a SQL Server to Azure Migration in 2021 - Brent Ozar
Geek Sync | Planning a SQL Server to Azure Migration in 2021 - Brent Ozar
 
Geek Sync | Performance Tuning: Getting the Biggest Bang for Your Buck - Moni...
Geek Sync | Performance Tuning: Getting the Biggest Bang for Your Buck - Moni...Geek Sync | Performance Tuning: Getting the Biggest Bang for Your Buck - Moni...
Geek Sync | Performance Tuning: Getting the Biggest Bang for Your Buck - Moni...
 
Geek Sync | Meeting Security Benchmarks and Compliance with Microsoft SQL Ser...
Geek Sync | Meeting Security Benchmarks and Compliance with Microsoft SQL Ser...Geek Sync | Meeting Security Benchmarks and Compliance with Microsoft SQL Ser...
Geek Sync | Meeting Security Benchmarks and Compliance with Microsoft SQL Ser...
 
Geek Sync | How to Think Like the SQL ServerEngine - Brent Ozar
Geek Sync | How to Think Like the SQL ServerEngine - Brent OzarGeek Sync | How to Think Like the SQL ServerEngine - Brent Ozar
Geek Sync | How to Think Like the SQL ServerEngine - Brent Ozar
 
Geek Sync | Data Integrity Demystified - Deborah Melkin | IDERA
Geek Sync | Data Integrity Demystified - Deborah Melkin | IDERAGeek Sync | Data Integrity Demystified - Deborah Melkin | IDERA
Geek Sync | Data Integrity Demystified - Deborah Melkin | IDERA
 
Geek Sync | Breaking Bad Habits: Solutions for Common Query Antipatterns - Je...
Geek Sync | Breaking Bad Habits: Solutions for Common Query Antipatterns - Je...Geek Sync | Breaking Bad Habits: Solutions for Common Query Antipatterns - Je...
Geek Sync | Breaking Bad Habits: Solutions for Common Query Antipatterns - Je...
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Geek Sync | The New Robot DBAs in SQL Server 2017, 2019, and Azure

  • 1. © 2020 Brent Ozar Unlimited®. All rights reserved. 1 The New Robots in SQL Server 2017 & 2019 99-05: dev, architect, DBA 05-08: DBA, VM, SAN admin 08-10: Microsoft Certified Master Since: consulting DBA www.BrentOzar.com Help@BrentOzar.com
  • 2. © 2020 Brent Ozar Unlimited®. All rights reserved. 2 I keep hearing about these robots, and about how they’re gonna take my job. I have clients with SQL Server 2019. I’ve never heard any of them say, “You’re fired.The robots fixed it allfor me.”
  • 3. © 2020 Brent Ozar Unlimited®. All rights reserved. 3 You’re probably not going to disable them, but… 2017 & 2019’s robots are kinda like your phone’s AutoCorrect.
  • 4. © 2020 Brent Ozar Unlimited®. All rights reserved. 4 Batch Mode Execution Reporting-style query on 2017:
  • 5. © 2020 Brent Ozar Unlimited®. All rights reserved. 5 Lots of reads, lots of CPU work. Change the compat level to 2019:
  • 6. © 2020 Brent Ozar Unlimited®. All rights reserved. 6 Same reads, but less CPU & time! 2017 was: 2019:
  • 7. © 2020 Brent Ozar Unlimited®. All rights reserved. 7 It’s not just a simpler plan: 2017 plan has two parallelism zones: 2019 plan – only one parallelism zone: It’s different operators, really: 2017’s table scan: 2019’s:
  • 8. © 2020 Brent Ozar Unlimited®. All rights reserved. 8 2017 & prior: row mode execution Each plan operator is a mini-program It runs in isolation It passes data to the next operator in the form of rows Then came columnstore indexes. The data is stored differently. I haven’t written a class on these yet, but start here: Paper: Query Execution in Column-Oriented Database Systems by Daniel J. Abadi: http://www.cs.umd.edu/~abadi/papers/abadiphd.pdf Niko Neugebauer 100+part blog series: http://www.nikoport.com/columnstore/
  • 9. © 2020 Brent Ozar Unlimited®. All rights reserved. 9 Why this mattered Think of it as a separate index on each column: But here’s the trick question: when do you reassemble these columns into their original rows? Batch mode execution means This operator isn’t rebuilding the rows yet: it’s passing groups of columns to the next operator.
  • 10. © 2020 Brent Ozar Unlimited®. All rights reserved. 10 Like an index on every column Think of it as a separate index on each column: But here’s the trick question: when do you reassemble these columns into their original rows? So many cool implications Batches of columnar data can be: • Way smaller due to duplication (only unique values are stored) • Way easier to group/sum/sort on (since so much is duplicated) Result: massively less CPU work for reporting queries (table scans with grouping, summing, formulas.)
  • 11. © 2020 Brent Ozar Unlimited®. All rights reserved. 11 The first challenge Which of these plans is getting batch mode? And which of the operators are getting it? More challenges If you got batch mode on an operator: • Is it working out in your favor? • Why was it chosen? • How can you change it? If you didn’t get batch mode: • Why not? • Can you change things to get it?
  • 12. © 2020 Brent Ozar Unlimited®. All rights reserved. 12 Still, I like it. A lot. Columnstore gets it in Standard, but it just works on columnstore indexes, period. No big gotchas there. Rowstore tables only get it in Enterprise Edition. I think that’s a perfect fit for batch mode: • It makes the most sense in reports • It makes the least sense in OLTP
  • 13. © 2020 Brent Ozar Unlimited®. All rights reserved. 13 TVF Interleaved Execution Multi-Statement Table-Valued Functions We covered why these suck: • Hard to see impact in the query plan, stats IO • Can run row-by-row • Estimates are hard-coded garbage (100 or 1) 2017 can fix the latter by running the MSTVF first, then passing the row estimates to the rest of the plan.
  • 14. © 2020 Brent Ozar Unlimited®. All rights reserved. 14 It takes 42 seconds
  • 15. © 2020 Brent Ozar Unlimited®. All rights reserved. 15 2017 compat mode: 7 seconds! The difference 2016 estimates a hard-coded 100 rows from the TVF: But 2017+ executes it, and knows 20,057 rows will come out:
  • 16. © 2020 Brent Ozar Unlimited®. All rights reserved. 16 Interleaved Execution is like phone-a-friend. Itgivesaheadsuptotherestoftheplanthata wholelotofrowsareincoming,therebyimproving memorygrantaccuracy.That’sgreat! You still shouldn’t be using MSTVFs though. Whileweknowthenumberofrowscomingout, westilldon’thavegreatstatsontheircontents.
  • 17. © 2020 Brent Ozar Unlimited®. All rights reserved. 17 The sort & hash match both spill. BadBad TVF interleaved execution is fine. It makes a bad thing (TVFs) suck less. Scalar function inlining, that’s different.
  • 18. © 2020 Brent Ozar Unlimited®. All rights reserved. 18 Scalar Function Inlining OneofthemostambitiousthingsI’ve everseeninadatabaseserver. The idea Take scalar user-defined functions that have always been designed to run on a row-by-row basis 1. Rewrite the UDFs automatically into set-based functions (think: turn them into a view you can join to) 2. Rewrite queries calling the UDFs so that they can leverage the set-based version White paper: https://arxiv.org/abs/1712.00498 (Click the PDF link in the Download box)
  • 19. © 2020 Brent Ozar Unlimited®. All rights reserved. 19 This paper is fun (for me) to read
  • 20. © 2020 Brent Ozar Unlimited®. All rights reserved. 20 I was amazed that this shipped. It’s the most ambitious thing I’ve seen in SQL Server. Other features were harder and more complex: CLR, Hekaton, Polybase, Big Data Clusters. But those had a limited blast radius because they required you to change your code, tables, indexes. This feature was supposed to work without you changing anything at all. Well, that was the theory. But they keep finding bugs with it, and disabling it:
  • 21. © 2020 Brent Ozar Unlimited®. All rights reserved. 21 At first, they fixed bugs… CU2 had bug fixes, but after that, they just started disabling scalar function inlining in more scenarios…
  • 22. © 2020 Brent Ozar Unlimited®. All rights reserved. 22 And even when it “works” It doesn’t always speed up queries, as we saw earlier. Thus my verdict: It might work for you, but… I’ve seen it backfire, HARD. Taryn is Stack Overflow’s DBA. It’s been rough seeing them struggle with 2019 performance issues.
  • 23. © 2020 Brent Ozar Unlimited®. All rights reserved. 23 Adaptive Memory Grants Query workspace memory grants It’s really hard for SQL Server to get estimates right. It’s not SQL Server’s fault: it’s usually our crappy queries and indexes. SQL Server does track how often spills occur for a given query plan, but it just doesn’t do anything with that data.
  • 24. © 2020 Brent Ozar Unlimited®. All rights reserved. 24 https://docs.microsoft.com/en-us/sql/relational- databases/performance/adaptive-query-processing
  • 25. © 2020 Brent Ozar Unlimited®. All rights reserved. 25 Seeing it in action Build our stored proc that is vulnerable to sniffing: Then run it back & forth…
  • 26. © 2020 Brent Ozar Unlimited®. All rights reserved. 26 That’s a big spill. 2017 compat level: 83,481 pages spilled to TempDB. (And the query’s slow, duh.) Let’s try 2019 compat level.
  • 27. © 2020 Brent Ozar Unlimited®. All rights reserved. 27 2019? Bad news. @Rep = 1 still gets the tiny data plan – that’s fair, MS didn’t say they fixed that. But it also gets the tiny grant at first, as you can see by hovering over the sort to measure spills. IT GOT WORSE Spills 167,303 pages to TempDB! But hang on: this is where it gets better – run it again with @Rep = 1.
  • 28. © 2020 Brent Ozar Unlimited®. All rights reserved. 28 No spills! Adaptive memory grants started raising the memory grant. How it works After a query finishes, SQL reviews its memory use. • If it spilled to disk, it didn’t get enough RAM. Raise its desired memory grant for next time. • If it left a lot of RAM unused, it got too much. Lower its desired memory grant for next time.
  • 29. © 2020 Brent Ozar Unlimited®. All rights reserved. 29 How it works, translated The amount of memory your query gets is based on the parameters that the last person used. If the last person asked for big data, you get big RAM. If they asked for small data, you get small RAM. So what’s gonna happen now? I’m about to run it for reputation = 2. What happens?
  • 30. © 2020 Brent Ozar Unlimited®. All rights reserved. 30 Runs fast... Uh oh
  • 31. © 2020 Brent Ozar Unlimited®. All rights reserved. 31 You see this coming, don’t you? Back to spilling The “adaptive” grant doesn’t actually remember anything – it just adapts to the LAST person’s params. And how big is the spill?
  • 32. © 2020 Brent Ozar Unlimited®. All rights reserved. 32 167,115 pages. Worse than it was before. <sigh>
  • 33. © 2020 Brent Ozar Unlimited®. All rights reserved. 33 Adaptive grants for rowstore are rough right now: • No proactive warning when it’s happening • Minimal instrumentation • Monitoring tools don’t cover it • Resets constantly on index rebuilds, stats updates, failovers, restarts Yet they’re on by default in compat 150. ALTER DATABASE SCOPED CONFIGURATION SET BATCH_MODE_MEMORY_GRANT_FEEDBACK = OFF; ALTER DATABASE SCOPED CONFIGURATION SET ROW_MODE_MEMORY_GRANT_FEEDBACK = OFF; Because I know you’re gonna ask
  • 34. © 2020 Brent Ozar Unlimited®. All rights reserved. 34 Adaptive Joins Adaptive join example You need to join between two tables. (Not 2 indexes: this doesn’t fix key lookups.) When the driver table returns just a few rows, then it makes sense to do nested loops: one operation per row that it finds. When the driver table returns a lot of rows, then it makes sense to do a hash join: scan the other table and check all the rows.
  • 35. © 2020 Brent Ozar Unlimited®. All rights reserved. 35 Sample query 1: the robot knows output rows might vary. 2: so nested loops OR hash.
  • 36. © 2020 Brent Ozar Unlimited®. All rights reserved. 36 Both operations are shown, but only one will be executed. Hash? Then we’ll scan all the data. Nested loops? We’ll just seek a few rows.
  • 37. © 2020 Brent Ozar Unlimited®. All rights reserved. 37 Try reputation 2, and the seek runs. Adaptive joins are rarely seen. In 2017: requires a columnstore index, compat 140 In 2019: works with rowstore indexes, compat 150 Requires batch mode execution (which in 2017 means a columnstore index somewhere in your query, anywhere, even an empty one) SELECT queries only (no DUIs) A join that can work with nested loops or hash
  • 38. © 2020 Brent Ozar Unlimited®. All rights reserved. 38 Adaptive Joins are OK. They’rereallyhardtogettoday,andbythemselves, theydon’tusuallygetyouacrossthefinishlinewith nootherquerychanges. Butthey’reOK. But run them back to back… Action Duration DBCC FREEPROCCACHE Run it for @Reputation = 1 1 second Run it for @Reputation = 2 0 seconds Run it for @Reputation = 1 again 1 minute 15 seconds Why do you suppose that is?
  • 39. © 2020 Brent Ozar Unlimited®. All rights reserved. 39 Ah, our “friend” adaptive grants. 25 second spill 22 second spill Parameter sniffing is still hard. One new feature (adaptive grants) can wreak havoc on other new features. This is called “feature interoperability.” You’re lucky if new features are tested in isolation, let alone combined with other features. Plus, we still have good ol’ parameter sniffing: adaptive joins may not show up for all params.
  • 40. © 2020 Brent Ozar Unlimited®. All rights reserved. 40
  • 41. © 2020 Brent Ozar Unlimited®. All rights reserved. 41 And there’s yet another plan: Parameter sniffing is HARD now. This is a very simple stored procedure, but: Depending on which @Reputation it’s called with first (1, 2, or 4), I can get 3 different plans in cache. And then every single time it runs, it can set different memory grants for the very next execution. Plan changes are a huge problem in 2019.
  • 42. © 2020 Brent Ozar Unlimited®. All rights reserved. 42 Automatic Tuning (aka plan regression) This feature name is a flat out lie. Automatic Tuning implies that SQL Server is actively doing something to make your query better. It’s not even close.
  • 43. © 2020 Brent Ozar Unlimited®. All rights reserved. 43 The sales pitch Turn on Query Store (which is a story to begin with.) Get a good query plan. Have something go wrong: stats update, data change. Get a bad query plan. After a while, SQL Server will realize it’s a bad plan, and try to revert to the good plan that was stored in Query Store. If it’s faster, keep it permanently. Scenarios it doesn’t help If you never get a good plan in the first place If you didn’t have Query Store already enabled when the good plan was in effect If the query changes (even by one space, like if you do a deployment and just changed a comment) If there’s no one good plan for all parameters (common) This is not Automatic Tuning in any way, shape, or form.
  • 44. © 2020 Brent Ozar Unlimited®. All rights reserved. 44 Anyway, Microsoft is off to a good start.
  • 45. © 2020 Brent Ozar Unlimited®. All rights reserved. 45 I have high hopes. Done right: This can reduce Microsoft’s hardware spend in Azure. They might cut hosting costs to be more competitive with Amazon RDS Aurora MySQL & PostgreSQL. This can give Microsoft a very competitive advantage over other on-premises databases, too. Done right, there’s really no downside (other than Microsoft’s costs to build it.)
  • 46. © 2020 Brent Ozar Unlimited®. All rights reserved. 46 But today, most require Enterprise Edition. (Andtoday,Idon’thaveaproblemwiththatbecausethefeatures aresolimited.If/whenopensourcecompetitorsstarttoeat Microsoft’slunchintheStandardEditionmarket,theycanopenup someofthesefeaturesatlowerpricetiers.)