Use the database’s EXPLAIN or EXPLAIN ANALYZE (PostgreSQL), EXPLAIN PLAN (Oracle), or SHOW PLAN (SQL Server) to see how the database executes the query. This shows you where time is spent—scans, joins, or index usage.
Look for red flags like full table scans, expensive nested loops, or missing indexes.
Check Indexes
Ensure indexes exist on columns used in WHERE, JOIN, GROUP BY, or ORDER BY clauses. For example, if you’re filtering on customer_id, an index on that column can cut lookup time.
Avoid over-indexing—too many indexes slow down writes (INSERT/UPDATE/DELETE).
Simplify the Query
Break complex queries into smaller parts. Subqueries or excessive joins can often be rewritten as temporary tables or CTEs (Common Table Expressions) for clarity and speed.
Remove unnecessary columns in SELECT—fetching less data reduces I/O.
Tune Joins
Ensure join conditions use indexed columns. A mismatch in data types (e.g., joining a string to an integer) can force conversions and kill performance.
Prefer INNER JOIN over LEFT JOIN where possible, as outer joins can generate more rows.
Limit Data Early
Apply filters (WHERE) as early as possible to reduce the dataset before joins or aggregations.
Use LIMIT or TOP if you only need a subset of results.
Optimize Aggregations
For GROUP BY or COUNT, ensure the grouped columns are indexed. Pre-aggregate data in a materialized view or summary table if the query runs often.
Check Statistics
Databases rely on stats to pick the best execution plan. Run ANALYZE (PostgreSQL) or update statistics (SQL Server: UPDATE STATISTICS) to ensure they’re current.
Caching
If the query runs frequently with static data, cache the results using a tool like Redis or the database’s built-in caching (e.g., SQL Server Query Store).
MySQL: EXPLAIN or the Performance Schema to see slow query logs.
SQL Server: SQL Server Profiler or Query Store for real-time monitoring.
Oracle: SQL Trace or TKPROF for execution details.
Slow Query Logs
Enable slow query logging (e.g., log_slow_queries in MySQL) to catch queries exceeding a time threshold.
Third-Party Tools
SolarWinds Database Performance Analyzer: Visualizes wait times and bottlenecks.
pgAdmin or DBeaver: GUI tools with query profiling features.
New Relic or Datadog: For monitoring queries in production environments.
Resource Monitoring
Check CPU, memory, and disk I/O with tools like top, iostat, or database-specific views (e.g., pg_stat_activity in PostgreSQL). A query might be slow due to resource contention, not just bad design.