Skip to main content

Introduction to Performance Tips

Introduction

Writing SQL that works is one thing. Writing SQL that works fast is another. As your data grows from thousands of rows to millions or even billions, poorly written queries can take minutes instead of milliseconds. The difference between a junior and senior SQL developer often comes down to knowing how to write efficient queries.

In this tutorial, you will learn:

  • Why SQL performance matters in real-world applications
  • The most common causes of slow queries
  • The general mindset for writing efficient SQL
  • An overview of what we will cover in the Performance Best Practices section
  • How to think about query optimization systematically

Why Performance Matters

In a small database with a few hundred rows, almost any query runs instantly. But real production databases are a different world:

EnvironmentTypical SizeTolerance for Slow Queries
Learning/TutorialHundreds of rowsAny query is fast
Small Business AppThousands of rowsSeconds are acceptable
Medium EnterpriseMillions of rowsMust respond in < 1 second
Large EnterpriseHundreds of millionsMust respond in < 100ms
Big Data / AnalyticsBillions of rowsRequires specialized strategies

When a query takes too long, the consequences cascade:

  • User experience degrades — pages take seconds to load, users leave
  • Server resources are consumed — slow queries hold locks, block other queries
  • Costs increase — more CPU, memory, and disk I/O means higher cloud bills
  • Timeouts occur — applications give up waiting and show errors
Generated visual representation from text diagram for Why Performance Matters.
Generated visual representation from text diagram for Why Performance Matters.

Common Causes of Slow Queries

Before diving into specific optimization techniques, it helps to understand the most frequent reasons queries run slowly:

1. Fetching Too Much Data

The most common performance mistake is retrieving more data than you need. Using SELECT * when you only need two columns forces the database to read and transfer unnecessary data.

2. Missing or Incorrect Indexes

Without proper indexes, the database must scan every row in a table to find matching data. This is called a full table scan, and it gets slower as the table grows.

3. Inefficient Filtering

Putting functions on columns in WHERE clauses, using leading wildcards in LIKE patterns, or writing non-sargable predicates prevents the database from using indexes effectively.

4. Poor Join Strategies

Joining large tables without proper indexes on the join columns, joining more tables than necessary, or using the wrong type of join can multiply the work the database must do.

5. Suboptimal Aggregations

Grouping by high-cardinality columns, aggregating before filtering, or performing unnecessary calculations inside aggregate functions wastes resources.

6. Unnecessary Subqueries

Using correlated subqueries that execute once per row in the outer query, or nesting subqueries deeply, can turn a simple query into an exponentially slow one.

Overview diagram showing common causes of slow SQL queries and the performance optimization mindset
Overview diagram showing common causes of slow SQL queries and the performance optimization mindset

The Performance Mindset

Optimizing SQL is not about memorizing tricks. It is about developing a systematic way of thinking:

Think Like the Database Engine

Every time you write a query, imagine you are the database engine. Ask yourself:

  1. How much data do I need to read? — Can I reduce it?
  2. Can I use an index? — Or am I forcing a full table scan?
  3. Am I doing unnecessary work? — Sorting, grouping, or computing things I do not need?
  4. Am I reading the same data multiple times? — Can I restructure the query?

The Optimization Hierarchy

When trying to improve a slow query, follow this priority order:

PriorityActionImpact
1Reduce data volume — fetch only what you needHighest
2Add proper indexes — let the engine find data fastHigh
3Rewrite the query — use better SQL patternsHigh
4Adjust database design — denormalize, partitionMedium
5Tune server settings — memory, parallelismLow
6Upgrade hardware — more CPU, faster disksLast resort

Always start from the top. Adding more hardware to compensate for a bad query is like buying a faster car because you keep taking the wrong route — the right route is always better.

Write Minimal SQL

The best-performing query is the one that does the least amount of work to produce the correct result. Every column you select, every row you scan, every join you add, and every sort you request has a cost. Your goal is to minimize that total cost.

What We Will Cover

This Performance Best Practices section is organized around the major SQL operations you have already learned. For each operation, we will identify the most common mistakes and show you how to fix them:

TutorialTopics Covered
Performance Tips for Fetching DataSELECT *, column selection, LIMIT, data types
Performance Tips for FilteringSargable predicates, function-free WHERE clauses, data type matching
Performance Tips for JoiningJoin order, index placement, reducing join scope
Performance Tips for AggregationFilter before aggregating, efficient GROUP BY, HAVING vs WHERE
Performance Tips for SubqueriesCorrelated vs non-correlated, EXISTS vs IN, replacing with JOINs
Performance Tips for Table DDLChoosing data types, normalization trade-offs, temp tables
Performance Tips for IndexingIndex strategy recap, covering indexes, index maintenance
Performance Tips SummaryComplete checklist and quick reference

Each tutorial will follow a consistent pattern:

  1. Show the slow version — the common mistake
  2. Explain why it is slow — what the database engine is doing
  3. Show the fast version — the optimized approach
  4. Measure the difference — quantify the improvement

How to Measure Query Performance

Before optimizing, you need to know how to measure. Here are the primary tools:

Execution Plan

The execution plan shows you exactly how the database engine processes your query — which tables it scans, which indexes it uses, and where the most time is spent.

Key Metrics to Watch

MetricWhat It Tells YouGood Value
Logical ReadsNumber of data pages read from memoryLower is better
Physical ReadsNumber of data pages read from diskShould be 0 for cached data
Elapsed TimeTotal wall-clock timeDepends on context
CPU TimeTime spent on computationLower is better
Row CountNumber of rows processedShould match what you need
Scan CountNumber of times a table was scanned1 is ideal, 0 for seeks

The 80/20 Rule

In most databases, 20% of the queries consume 80% of the resources. Focus your optimization efforts on the most expensive queries first — they will give you the biggest return on investment.

Generated visual representation from text diagram for How to Measure Query Performance.
Generated visual representation from text diagram for How to Measure Query Performance.

Summary

Here is what you should take away from this introduction:

ConceptKey Takeaway
Why it mattersSlow queries degrade user experience, waste resources, and increase costs
Common causesSELECT *, missing indexes, bad WHERE clauses, inefficient joins, correlated subqueries
MindsetThink like the database engine — minimize the work required
PriorityReduce data first, add indexes second, rewrite queries third
MeasurementAlways measure before and after — use execution plans and I/O statistics
80/20 ruleFocus on the most expensive queries for the biggest impact

In the following tutorials, we will dive deep into specific optimization techniques for each type of SQL operation. Each tutorial will give you practical, actionable tips that you can apply immediately to your own queries.