Leanpub Header

Skip to main content

Mastering SQL Performance Optimization

From Query Fundamentals to Production-Grade Tuning Across Database Platforms

This book is 100% completeLast updated on 2026-07-17

Slow SQL queries waste time, increase costs, and limit scalability. Mastering SQL Performance Optimization teaches you how to analyze execution plans, optimize queries and indexes, and solve real-world performance bottlenecks across today's leading database platforms.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
253
Pages
About

About

About the Book

Slow SQL queries cost real money. Every millisecond of latency compounds across thousands of concurrent users, driving up infrastructure bills and degrading user experience. This book teaches you how databases execute queries internally, so you can systematically diagnose and resolve performance problems across PostgreSQL, MySQL, SQL Server, Oracle, and SQLite. Through execution plan analysis, indexing strategies, join optimization, schema design principles, concurrency management, and real-world case studies, you will build the skills to transform sluggish queries into high-performance operations that scale under production load.

Bundle

Bundles that include this book

Author

About the Author

Steve Publications

Steve is a technology professional with more than 20 years of experience in software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. Throughout his career, he has designed, secured, analyzed and tested complex software and infrastructure, with a particular focus on understanding how systems fail and how they can be made more secure.

He currently works in the advanced research division of a leading cybersecurity company, where he performs vulnerability research alongside a team of experienced researchers and engineers. His work includes discovering security vulnerabilities, reverse engineering software and malware, analyzing emerging threats and developing new techniques to improve the security of modern computing environments.

Outside of work, Steve enjoys sharing knowledge with the technology community. He collaborates with researchers, industry experts and technology professionals to write practical books covering software development, cybersecurity, cloud computing, networking, DevOps, artificial intelligence and enterprise technologies. His books focus on practical learning through clear explanations, real-world examples and hands-on exercises. With more than two decades of industry experience, his goal is to help IT professionals, students and technology enthusiasts build useful skills and stay current in a rapidly changing industry.

We believe readers deserve to know how our books are created. Most of our authors are not native English speakers, so we use AI to help translate, proofread manuscripts, fix grammar, improve sentence structure and make technical explanations easier to read. AI is used as an editing tool only. It does not replace the research, technical knowledge or hands-on experience behind our books.

Some of our authors also prefer to remain anonymous for privacy or professional reasons. In those cases, we publish their work under the a different name. The author's name may be different, but the quality of the content and our review process remain the same.

Every book is written, reviewed and maintained by experienced technology professionals, with contributions from our private technical community of more than 400 engineers and researchers from Ukraine, Belarus and Russia. We spend far more time validating technical accuracy and keeping our content up to date than generating text.

If you look through the contents of our books, you'll see practical examples, detailed explanations and material that is regularly updated. Our goal is to publish books that professionals can actually rely on, not low-effort AI-generated content. If you ever feel that one of our books does not meet that standard, Leanpub offers a 60-day money-back guarantee. Feel free to request a refund if you are not satisfied with your purchase.

Contents

Table of Contents

From Query Fundamentals to Production-Grade Tuning Across Database Platforms

Introduction: The Performance Imperative

  1. The Hidden Cost of Slow Queries
  2. What This Book Will Teach You
  3. The Book’s Structure
  4. How to Use This Book
  5. Setting Up Your Environment
  6. A Note on Benchmarks and Numbers

Chapter 1: How Databases Execute Queries

  1. From Text to Results: The Query Pipeline
  2. Parsing and Validation
  3. The Query Optimizer: Cost-Based Decisions
  4. Execution Engines and Memory Architecture
  5. SQLite: A Different Architecture for Edge and Embedded Workloads
  6. Understanding Buffer Pools and Working Sets
  7. Chapter Summary
  8. Hands-On Exercise
  9. Common Pitfalls
  10. Interview Questions

Chapter 2: Reading Execution Plans

  1. What an Execution Plan Tells You
  2. PostgreSQL EXPLAIN and EXPLAIN ANALYZE
  3. MySQL EXPLAIN Output Decoded
  4. SQL Server Execution Plans
  5. Oracle Explain Plan Fundamentals
  6. SQLite EXPLAIN and Query Plan Reading
  7. Common Patterns and Red Flags
  8. Hands-On Exercise
  9. Common Pitfalls
  10. Interview Questions
  11. Chapter Summary

Chapter 3: Indexing Strategies

  1. How B-Tree Indexes Work Internally
  2. When Indexes Help (and When They Hurt)
  3. Composite and Covering Indexes
  4. Specialized Index Types: Hash, GiST, GIN, BRIN
  5. Index Maintenance and Fragmentation
  6. Cross-Platform Index Comparisons
  7. Hands-On Exercise
  8. Common Pitfalls
  9. Interview Questions
  10. Chapter Summary

Chapter 4: Query Patterns and Anti-Patterns

  1. SELECT * and Column Selection Discipline
  2. The LEFT JOIN Trap
  3. Correlated Subqueries vs. EXISTS
  4. OR Conditions and Index Inefficiency
  5. Functions on Indexed Columns
  6. Implicit Type Conversion Pitfalls
  7. The N+1 Query Pattern
  8. Pagination Anti-Patterns
  9. Side-by-Side Comparison Table
  10. Hands-On Exercise
  11. Common Pitfalls
  12. Interview Questions
  13. Chapter Summary

Chapter 5: Joins and Their Performance

  1. Nested Loop Joins: When Small Meets Big
  2. Hash Joins for Large Data Sets
  3. Merge Joins and Sorted Inputs
  4. Join Order Optimization
  5. Self-Joins and Multi-Table Complexities
  6. Cross Joins and Cartesian Product Dangers
  7. Advanced Join Topics: Batch Index Probe and Vectorized Execution
  8. Join Algorithm Selection Summary
  9. Hands-On Exercise
  10. Common Pitfalls
  11. Interview Questions
  12. Chapter Summary

Chapter 6: Aggregation, Sorting, and Filtering

  1. GROUP BY Implementation Strategies
  2. Window Functions vs. Self-Joins
  3. ORDER BY and Sort Spill
  4. Filtering Early: Pushdown Optimization
  5. DISTINCT and Deduplication Costs
  6. Partial Aggregations and Precomputation
  7. Hands-On Exercise
  8. Common Pitfalls
  9. Interview Questions
  10. Chapter Summary

Chapter 7: Subqueries, CTEs, and Derived Tables

  1. Correlated vs. Non-Correlated Subqueries
  2. Common Table Expressions: Readability vs. Performance
  3. Materialized vs. Inlined CTEs
  4. Lateral Joins and Cross Apply
  5. Temporal Tables and Time-Series Queries
  6. Query Refactoring Patterns
  7. Hands-On Exercise
  8. Common Pitfalls
  9. Interview Questions
  10. Chapter Summary

Chapter 8: Schema Design for Performance

  1. Normalization vs. Denormalization Trade-offs
  2. Data Modeling Fundamentals: ER Design and Dimensional Models
  3. Data Types and Their Storage Cost
  4. Partitioning Strategies and Query Routing
  5. Table Inheritance and Sharding
  6. Columnar vs. Row-Oriented Storage
  7. Schema Evolution and Migration Strategies
  8. Hands-On Exercise
  9. Common Pitfalls
  10. Interview Questions
  11. Chapter Summary

Chapter 9: Transactions, Locking, and Concurrency

  1. Isolation Levels and Their Performance Cost
  2. Row-Level vs. Table-Level Locking
  3. Deadlock Detection and Prevention
  4. MVCC and Snapshot Isolation
  5. Connection Pooling and Resource Limits
  6. Write Contention and Hot Rows
  7. SQLite Locking Model: File-Level Locks and WAL Mode
  8. Hands-On Exercise
  9. Common Pitfalls
  10. Interview Questions
  11. Chapter Summary

Chapter 10: Memory, Caching, and I/O Optimization

  1. Buffer Pool Sizing and Hit Ratios
  2. Query Result Caching Strategies
  3. Sequential vs. Random I/O Patterns
  4. SSD Impact on Database Performance
  5. Temp Tables and Disk Spill
  6. OS-Level I/O Interactions: Linux Readahead, fsync, and NUMA
  7. Memory-Optimized Tables
  8. Hands-On Exercise
  9. Common Pitfalls
  10. Interview Questions
  11. SQLite I/O Architecture and Performance Tuning
  12. Chapter Summary
  13. Hands-On Exercise

Chapter 11: Advanced Optimization Techniques

  1. Query Rewriting and Transformation
  2. Materialized Views and Refresh Strategies
  3. Partition Pruning and Predicate Pushdown
  4. Adaptive Query Processing
  5. Cardinality Estimation: The Math Behind Plan Quality
  6. Database-Specific Tuning Features
  7. Custom Aggregates and Extensions
  8. Hands-On Exercise
  9. Common Pitfalls
  10. Interview Questions
  11. Chapter Summary

Chapter 12: Monitoring, Troubleshooting, and Benchmarking

  1. Building a Performance Baseline
  2. Slow Query Log Analysis
  3. Real-Time Monitoring Dashboards
  4. Load Testing and Benchmark Design
  5. Production Incident Response
  6. Performance Regression Prevention
  7. Hands-On Exercise
  8. Common Pitfalls
  9. Interview Questions
  10. Chapter Summary

Chapter 13: Case Studies in Performance Optimization

  1. Case Study 1: E-Commerce Search and Cart Performance
  2. Analytics Dashboard Timeout Crisis
  3. Case Study 3: Low-Latency Time-Series Data Access
  4. Case Study 4: Multi-Tenant SaaS Database Isolation
  5. Case Study 5: Database Migration Performance Parity
  6. Lessons Learned Across Industries
  7. Hands-On Exercise
  8. Common Pitfalls
  9. Interview Questions
  10. Chapter Summary

Chapter 14: Production Best Practices and Checklists

  1. The Performance Review Checklist
  2. CI/CD Integration for Query Validation
  3. Capacity Planning and Growth Patterns
  4. Database Migration Performance Considerations
  5. Team Training and Knowledge Sharing
  6. Staying Current: What to Watch For
  7. Performance Optimization Cheat Sheet
  8. Hands-On Exercise
  9. Common Pitfalls
  10. Interview Questions
  11. Chapter Summary

Conclusion: The Optimizer’s Mindset

References

Glossary

Appendix A: Sample Database Schema Evolution

  1. Phase 1: Base Schema (Chapters 1-5)
  2. Phase 2: Indexing Layer (Chapter 3)
  3. Phase 3: Partitioning (Chapter 8)
  4. Phase 4: Materialized Views and Aggregates (Chapters 6, 11)
  5. Phase 5: Dimensional Model (Chapter 8 data modeling)
  6. Data Generation Script (PostgreSQL)

Appendix B: Platform-Specific Configuration Templates

  1. PostgreSQL 16 Performance Configuration
  2. MySQL 8.0 InnoDB Configuration
  3. SQL Server Configuration Guidelines
  4. PgBouncer Configuration

Appendix C: Exercise Solutions

  1. Chapter 1 Exercise Solution
  2. Chapter 2 Exercise Solution
  3. Chapter 3 Exercise Solution
  4. Chapter 4 Exercise Solution
  5. Chapter 5 Exercise Solution
  6. Chapter 6 Exercise Solution
  7. Chapter 7 Exercise Solution
  8. Chapter 8 Exercise Solution
  9. Chapter 9 Exercise Solution
  10. Chapter 10 Exercise Solution
  11. Chapter 11 Exercise Solution
  12. Chapter 12 Exercise Solution
  13. Chapter 13 Exercise Solution
  14. Chapter 14 Exercise Solution

Index

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub