SQL Server Query Tuning and Optimization

Book description

Get well-versed with ready-to-use techniques for creating high-performance queries and applications

Key Features

  • Speed up queries and dramatically improve application performance by both understanding query engine internals and practical query optimization
  • Understand how the query optimizer works
  • Learn about intelligent query processing and what is new in SQL Server 2022

Book Description

SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications.

This book starts by describing the inner workings of the query optimizer, and will enable you to use this knowledge to write better queries and provide the query engine with all the information it needs to produce efficient execution plans. As you progress, you'll get practical query optimization tips for troubleshooting underperforming queries. The book will also guide you through intelligent query processing and what is new in SQL Server 2022. Query performance topics such as the Query Store, In-Memory OLTP and columnstore indexes are covered as well.

By the end of this book, you'll be able to get the best possible performance for your queries and applications.

What you will learn

  • Troubleshoot queries using methods including extended events, SQL Trace, and dynamic management views
  • Understand how the execution engine and query operators work
  • Speed up queries and improve app performance by creating the right indexes
  • Detect and fix cardinality estimation errors by examining query optimizer statistics
  • Monitor and promote both plan caching and plan reuse to improve app performance
  • Troubleshoot and improve query performance by using the Query Store
  • Improve the performance of data warehouse queries by using columnstore indexes
  • Handle query processor limitations with hints and other methods

Who this book is for

This book is for SQL Server developers who are struggling with slow query execution, database administrators who are tasked with troubleshooting slow application performance, and database architects who design SQL Server databases in support of line-of-business and data warehousing applications.

Table of contents

  1. SQL Server Query Tuning and Optimization
  2. Contributors
  3. About the author
  4. About the reviewers
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. Download the example code files
    4. Download the color images
    5. Conventions used
    6. Get in touch
    7. Share Your Thoughts
  6. Chapter 1: An Introduction to Query Tuning and Optimization
    1. Query Processor Architecture
      1. Parsing and binding
      2. Query optimization
      3. Generating candidate execution plans
      4. Plan cost evaluation
      5. Query execution and plan caching
    2. Analyzing execution plans
      1. Graphical plans
      2. XML plans
      3. Text plans
      4. Plan properties
      5. Warnings on execution plans
    3. Getting plans from a trace or the plan cache
      1. sys.dm_exec_query_plan DMF
      2. SQL Trace/Profiler
      3. Extended events
      4. Removing plans from the plan cache
    4. SET STATISTICS TIME and IO statements
    5. Summary
  7. Chapter 2: Troubleshooting Queries
    1. DMVs and DMFs
      1. sys.dm_exec_requests and sys.dm_exec_sessions
      2. Sys.dm_exec_query_stats
      3. Understanding statement_start_offset and statement_end_offset
      4. sql_handle and plan_handle
      5. query_hash and plan_hash
      6. Finding expensive queries
      7. Blocking and waits
    2. SQL Trace
    3. Extended events
      1. Mapping SQL Trace events to extended events
      2. Working with extended events
    4. The Data Collector
      1. Configuration
      2. Using the Data Collector
      3. Querying the Data Collector tables
    5. Summary
  8. Chapter 3: The Query Optimizer
    1. Query optimization research
    2. Introduction to query processing
    3. The sys.dm_exec_query_optimizer_info DMV
    4. Parsing and binding
    5. Simplification
      1. Contradiction detection
      2. Foreign Key Join elimination
    6. Trivial plan optimization
    7. Joins
    8. Transformation rules
    9. The Memo
    10. Statistics
    11. Full optimization
      1. Search 0
      2. Search 1
      3. Search 2
    12. Summary
  9. Chapter 4: The Execution Engine
    1. Data access operators
      1. Scans
      2. Seeks
      3. Bookmark lookup
    2. Aggregations
      1. Sorting and hashing
      2. Stream Aggregate
      3. Hash Aggregate
      4. Distinct Sort
    3. Joins
      1. Nested Loops Join
      2. Merge Join
      3. Hash Join
    4. Parallelism
      1. The exchange operator
      2. Limitations
    5. Updates
      1. Per-row and per-index plans
      2. Halloween protection
    6. Summary
  10. Chapter 5: Working with Indexes
    1. Introduction to indexes
    2. Creating indexes
      1. Clustered indexes versus heaps
      2. Clustered index key
      3. Covering indexes
      4. Filtered indexes
    3. Understanding index operations
    4. The Database Engine Tuning Advisor
      1. Tuning a workload using the plan cache
      2. Offload of tuning overhead to test server
    5. Missing indexes
    6. Index fragmentation
    7. Unused indexes
    8. Summary
  11. Chapter 6: Understanding Statistics
    1. Exploring statistics
      1. Creating and updating statistics
      2. Inspecting statistics objects
      3. The density vector
    2. Histograms
    3. A new cardinality estimator
      1. Trace flag 4137
    4. Cardinality estimation errors
    5. Incremental statistics
    6. Statistics on computed columns
    7. Filtered statistics
    8. Statistics on ascending keys
      1. Trace flag 2389
    9. UPDATE STATISTICS with ROWCOUNT and PAGECOUNT
    10. Statistics maintenance
    11. Cost estimation
    12. Summary
  12. Chapter 7: In-Memory OLTP
    1. In-memory OLTP architecture
    2. Tables and indexes
      1. Creating in-memory OLTP tables
      2. Hash indexes
      3. Nonclustered or range indexes
    3. Natively compiled stored procedures
      1. Creating natively compiled stored procedures
      2. Inspecting DLLs
    4. Limitations and later enhancements
    5. Summary
  13. Chapter 8: Understanding Plan Caching
    1. Batch compilation and recompilation
    2. Exploring the plan cache
      1. How to remove plans from memory
    3. Understanding parameterization
      1. Autoparameterization
      2. The Optimize for Ad Hoc Workloads option
      3. Forced parameterization
      4. Stored procedures
    4. Parameter sniffing
      1. Optimizing for a typical parameter
      2. Optimizing on every execution
      3. Local variables and the OPTIMIZE FOR UNKNOWN hint
      4. Disabling parameter sniffing
      5. Parameter sniffing and SET options affecting plan reuse
    5. Parameter-sensitive plan optimization
    6. Summary
  14. Chapter 9: The Query Store
    1. Using the Query Store
    2. Querying the Query Store
    3. Summary
  15. Chapter 10: Intelligent Query Processing
    1. Overview of intelligent query processing
      1. Parameter-sensitive plan optimization
    2. Memory grant feedback
      1. Persistence and percentile
    3. Cardinality estimation feedback
    4. Degree of parallelism feedback
    5. Interleaved execution
    6. Table variable deferred compilation
    7. Adaptive joins
    8. Summary
  16. Chapter 11: An Introduction to Data Warehouses
    1. Data warehouses
    2. Star join query optimization
    3. Columnstore indexes
      1. Performance benefits
      2. Batch mode processing
      3. Creating columnstore indexes
      4. Hints
    4. Summary
  17. Chapter 12: Understanding Query Hints
    1. Breaking down complex queries
      1. OR logic in the WHERE clause
      2. Joins on aggregated datasets
    2. Hints
      1. When to use hints
      2. Types of hints
    3. Joins
    4. Aggregations
    5. FORCE ORDER
    6. The INDEX, FORCESCAN, and FORCESEEK hints
    7. FAST N
    8. The NOEXPAND and EXPAND VIEWS hints
    9. Plan guides
    10. USE PLAN
    11. Summary
    12. Why subscribe?
  18. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: SQL Server Query Tuning and Optimization
  • Author(s): Benjamin Nevarez
  • Release date: August 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781803242620