Advanced SQL for Query Tuning and Performance Optimization

Advanced SQL for Query Tuning and Performance Optimization

2h 10mAdvanced2023-10-11

Authors

Dan Sullivan

Dan Sullivan

Enterprise Architect, Big Data Expert

Course details

SQL queries can be fast and highly efficient, but they can also be slow and demand excessive CPU and memory resources. For many SQL programmers, occasional bouts with long-running queries and poor performance are simply par for the course. But by gaining a better understanding of how databases translate SQL queries into execution plans, you can take steps to avoid these issues. In this course, Dan Sullivan shows you how to analyze query execution plans and use data modeling strategies to boost query performance. Dan describes how SQL queries are executed, highlights different types of indexes and how they factor in query tuning, covers several methods for performing joins, and discusses how to use partitioning and materialized views to improve performance. Plus, Dan shows you how to run PostgreSQL in GitHub Codespaces so you can get started learning faster.

Skills covered

SQLDatabase AdministrationAdvancedDatabase DevelopmentDatabase ManagementData AnalysisProgramming LanguagesData ScienceBusiness Analysis and StrategyBusiness Software and ToolsOpen SourceSoftware Development

Concepts

Introduction

  • Reduce query response time with query tuning
  • What you should know

How SQL Executes a Query

  • From declarative SQL to a procedural execution plan
  • Scanning tables and indexes
  • Joining tables
  • Partitioning data
  • Challenge - Choosing how to partition a table
  • Solution - Choosing how to partition a table

PostgreSQL Tools for Tuning

  • Using PostgreSQL in Codespaces
  • Explain and analyze
  • Example plan - Selecting with a WHERE clause
  • Indexes
  • Challenge - Generating a query execution plan
  • Solution - Generating a query execution plan

Types of Indexes

  • Indexing
  • B-tree indexes
  • B-tree index example plan
  • Bitmap indexes
  • Bitmap index example plan
  • Hash indexes
  • Hash index example plan
  • Bloom filter indexes
  • PostgreSQL-specific indexes
  • Challenge - Choosing an index
  • Solution - Choosing an index

Tuning Joins

  • Types of joins
  • Nested loops
  • Nested loop example plan
  • Hash joins
  • Hash join example plan
  • Merge joins
  • Merge join example
  • Subqueries vs. joins
  • Challenge - Designing a join
  • Solution - Designing a join

Partitioning Data

  • Horizontal vs. vertical partitioning
  • Partition by range
  • Partition by range example
  • Partition by list
  • Partition by list example
  • Partition by hash
  • Partition by hash example
  • Challenge - Partitioning a table
  • Solution - Partitioning a table

Materialized Views

  • Materialized views
  • Creating materialized views
  • Refreshing materialized views
  • Challenge - Creating a materialized view
  • Solution - Creating a materialized view

Database Statistics and Identifying Slow Queries

  • Collect statistics about data in tables
  • Analyzing execution statistics with pg stat statements
  • Reviewing execution plans with the auto explain module
  • Additional analysis with other pg stats data
  • Challenge - Analyze schema statistics
  • Solution - Analyze schema statistics

Other Optimization Techniques

  • Using common table expressions to avoid repetitive computation
  • Hints to the Query Optimizer
  • Parallel query execution
  • Improving cache utilization
  • Miscellaneous tips
  • Challenge - Design a common table expression
  • Solution - Design a common table expression

Conclusion

  • Next steps
80,000 Toman