Advanced Query Store - SQL Server 2017
Advanced Query Store - SQL Server 2017
Operational Analytics Always Encrypted SQL Server 2017 on Linux Machine Learning Services
Insights on operational data; Works with in- Sensitive data remains encrypted at all times Enhanced AlwaysOn R Scripting along with Python scripting from
memory OLTP and disk-based OLTP with ability to query Three synchronous replicas for auto failover the SQL Server Engine
In-memory OLTP Enhancements across domains
Row-Level Security Round robin load balancing of replicas
Greater T-SQL surface area, terabytes of Apply fine-grained access control to table Graph DB Support
memory supported, and greater number of rows Automatic failover based on database health
parallel CPUs For modeling many-to-many relationships
DTC for transactional integrity across
Dynamic Data Masking database instances with AlwaysOn
Live Query Statistics Real-time obfuscation of data to prevent Enhanced Database Caching
unauthorized access Support for SSIS with AlwaysOn
Cache data with automatic, multiple TempDB
files per instance in multi-core environments
Query Store Advanced Threat Detection Stretch Database
Monitor and optimize query plans Ability to find unusual login patterns, track Archive historical data transparently and
usage behavior in an auditing database, track securely to Azure New Programmatic Improvements
SQL injection vulnerability, and more New TSQL Functionality, Maintenance Plan
Automatic Database Tuning Queries stretch across local data as well as Improvements, New ALTER DATABASE
Other Enhancements Azure data
Provides insight into potential query Options
performance problems, recommends Audit success/failure of database operations Expanded support for JSON data
solutions, and can automatically fix identified TDE support for storage of in-memory OLTP New PolyBase query engine integrates SQL
problems tables Server with external data in Hadoop or Azure
Enhanced auditing for OLTP with ability to Blob storage
DMV Improvements track history of record changes
Temporal Database Support
Query data as points in time
Adaptive Query Processing
A feature family that introduces a new
generation of query processing
improvements
New in SQL Server 2017
SQL Server 2016/2017 Monitoring and Tooling
Traditional Troubleshooting
Extended Events is scalable
Hints
OPTION
Plan Guides Stored Procedures
sp_create_plan_guide
sp_control_plan_guide
sys.plan_guides
Common Query Hints Used in Plan Guides
• OPTIMIZE FOR (Value, Unknown)
• RECOMPILE
• NULL
Plan Guides from Cache
• Also known as ‘Plan Freezing’
• Prevents a current cached plan from changing
• Example: -- Create a plan guide for the query by specifying the query plan in the plan
cache.
DECLARE @plan_handle varbinary(64);
DECLARE @offset int;
SELECT @plan_handle = plan_handle, @offset = qs.statement_start_offset
FROM sys.dm_exec_query_stats AS qs
sp_create_plan_guide_from_handle
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS st
[ @name = ] N'plan_guide_name'
CROSS APPLY sys.dm_exec_text_query_plan(qs.plan_handle,
, [ @plan_handle = ] plan_handle
qs.statement_start_offset,
, [ [ @statement_start_offset
qs.statement_end_offset) AS qp = ] {
statement_start_offset
WHERE | NULL } ]p.Name, OrderQty, DueDate%';
text LIKE N'SELECT WorkOrderID,
EXECUTE sp_create_plan_guide_from_handle
@name = N'Guide1',
@plan_handle = @plan_handle,
@statement_start_offset = @offset;
GO
-- Verify that the plan guide is created.
SELECT * FROM sys.plan_guides
WHERE scope_batch LIKE N'SELECT WorkOrderID, p.Name, OrderQty, DueDate%';
GO
USE PLAN
SELECT *
FROM Sales.SalesOrderHeader h, Sales.SalesOrderDetail
OPTION (USE PLAN N'<ShowPlanXML
xmlns="http://schemas.microsoft.com/sqlserver/2004/07/showplan"
Version="0.5"
Build="9.00.1187.07">
<BatchSequence>
<Batch>
<Statements>
…
</Statements>
</Batch>
</BatchSequence>
</ShowPlanXML>
')
Query and Query Plan Fingerprints
• •
•
When performance is not good…
• Database is not • Impossible to • Regression
working predict / root caused by
cause upgrade
Query Plan
…Upgraded an application to the latest SQL Server version
choice changes
and had an issue with a plan change slowing your application down?
can cause all of
these problems!
…Had a problem with your SQL database performance and been unable
to determine what was going wrong?
What are you doing today?
• Most solutions are reactive in nature
• Flush the bad plan from the cache with sp_recompile
• Flush the entire plan cache with DBCC FREEPROCCACHE
• Force the plan to recompile every time
• Restart OS / SQL Server (It works for some reason?)
Not
Found
Get forced plan
Compile and
Fetch Plan from Cache
Optimize Query
Send text and plan
Query Store
Recompile
Check for
Recompile
Not Needed
Execute Query
Finish Execution
Query 1 - n 1 - n Runtime
Query Plan Stats
Text
Compile stats:
query_store_query_text
query_context_settings
Context Runtime Stats
Interval query_store_query
Settings query_store_plan
sys.query_store_wait_stats (2017)
Runtime stats:
One Row Per Query Text Per One Row Per Plan One Row Per
Plan Affecting Option (for each query) Plan Per Time query_store_runtime_stats_interval
(example: ANSI NULLS on/off) Interval query_store_runtime_stats
(example: 5 min)
Key DMVs for Query Store
SELECT * FROM sys.query_store_query_text ▪ The DMVs shown here are
SELECT * FROM sys.query_store_query enabled and populated for
SELECT * FROM sys.query_store_plan each database when Query
Store is turned on
SELECT * FROM sys.query_store_runtime_stats
ORDER BY runtime_stats_id
SELECT * FROM
sys.query_store_runtime_stats_interval
Upgrade to
SQL vNext Run Query Move to 130 Monitor perf.
Keep 110/120 Store Compat Level and fix
CompatLevel (establish perf. and unfreeze regressions with
Freeze plans baseline) plans plan forcing
(optional)
Troubleshooting with Query Store
Enable Set
Let Query Search for
Query FORCE
Store “Problem”
Store PLAN
collect queries
(ALTER policies
the data
DB)
Monitoring Performance with Query Store
• The Query Store
feature provides
DBAs with insight
on query plan
choice and
performance
Working with Query Store
• DB-level feature exposed
/* (6)
(1) Performance
Turn ON Query
analysis
Store */
using Query Store views*/
SELECT q.query_id, qt.query_text_id, qt.query_sql_text,
• ALTER DATABASE
q.query_text_id JOIN
/* (3) Set new parameter
sys.query_store_plan p ONvalues
q.query_id
*/ = p.query_id JOIN
ALTER DATABASE MyDB
sys.query_store_runtime_stats rs ON p.plan_id = rs.plan_id
SET QUERY_STORE
GROUP BY q.query_id,
( qt.query_text_id, qt.query_sql_text
query/plan/stats cleanup)
/* (4) Clear all Query Store data */
ALTER DATABASE MyDB SET QUERY_STORE CLEAR;
Waits Reports
Coming Soon!
Demonstration:
Query Store in SQL Server 2017
• sys.query_store_wait_stats
• sys.dm_db_tuning_recommendations
• sys.database_automatic_tuning_mode
• sys.database_automatic_tuning_options
SQL Server 2017 Automatic Tuning
ALTER DATABASE CURRENT
SET AUTOMATIC_TUNING (FORCE_LAST_GOOD_PLAN = ON);
Reverts back to
“Last Known Good”
• Interleaved Execution
• Batch-Mode Memory Grant Feedback
• Batch-Mode Adaptive Join