An orders page feels slow, so the first proposal is a larger database server. That might eventually help, but it does not explain the delay. The request could be running hundreds of small queries, waiting for a connection, scanning unnecessary rows, or sitting behind a long transaction. Each cause needs a different response.
A useful performance review follows one customer workflow from the application into the database. Establish what is slow, find where time is spent, and change one thing you can measure. The example below uses a tenant's orders page to explain that process; it describes an investigation, not a benchmark or a claim about client results.
The key ideas
- Measure request time, query frequency, and waiting separately.
- Design indexes around a real query and verify the execution plan.
- Budget database connections across every application instance and worker.
Locate the delay in one real workflow
Choose a specific operation: opening the latest 50 orders for one tenant during the morning import. Record typical and slow request durations, query count, time waiting for a connection, and database execution time. Note the dataset size, concurrent users, and whether an import was running. An average alone can hide the requests customers complain about.
Use application tracing to connect queries to the request that issued them. In PostgreSQL, pg_stat_statements can help identify frequently executed statements and cumulative execution time when the extension is configured. A moderately slow query called thousands of times can deserve attention before an occasional expensive report. Keep sensitive values out of diagnostic logs and give collected evidence a retention limit.
Reference: PostgreSQL: pg_stat_statements
Remove unnecessary round trips and rows
Suppose the orders page first fetches 50 orders and then issues a separate customer lookup for every row. That pattern creates 51 database calls for one page. A suitable join or batched customer lookup can reduce the trips, but check the returned data: joining several one-to-many relationships can multiply rows and change pagination behavior.
Fetch the fields the page needs, cap result sizes, and check whether the application is loading a full history to calculate a summary. Keep tenant and permission conditions in every replacement query. Test empty results, duplicate customer references, and a tenant with a much larger order history. A faster response only counts as an improvement if it still returns the correct authorized data.
Read the execution plan before adding an index
For PostgreSQL, EXPLAIN shows the planner's chosen approach. EXPLAIN ANALYZE executes the statement and reports actual execution observations; adding BUFFERS helps examine buffer activity. Start with a controlled environment and representative data. Even a read query can create substantial load, and an analyzed write really performs its write operations unless you take appropriate precautions.
Compare estimated and actual row counts, repeated work inside loops, filters that discard many rows, and sorting. A sequential scan is not automatically a problem: reading much of a small table can be a reasonable plan. Ask which part of this query performs avoidable work. Save the original plan so a later change can be assessed against the same parameters and workload.
Reference: PostgreSQL: using EXPLAIN
Match a candidate index to the access pattern
Imagine a query that filters orders by tenant_id and status, sorts by created_at descending, and returns 50 rows. A B-tree index beginning with tenant_id and status, followed by created_at, is a candidate to evaluate for that pattern. PostgreSQL's multicolumn index guidance explains why leading columns and their conditions affect which portion of an index must be scanned. This is a hypothesis to test against your actual distribution and other queries.
Compare the plan and request timings after the candidate index is available. Include the cost of order inserts and status updates, plus index storage and the operational effect of building it. If a stable sort needs an order ID as a tie-breaker, include that requirement in the query and index review. Do not create an index for every column simply because a page mentions it.
| Observation | Candidate change | Evidence to compare |
|---|---|---|
| One customer query per order | Join or batch customer retrieval | Query count, response correctness, request duration |
| Many rows examined for a small result | Evaluate an index matching filters and ordering | Execution plan, rows examined, write cost |
| Requests wait before executing SQL | Review pool usage and transaction duration | Connection wait time, active sessions, throughput |
Reference: PostgreSQL: multicolumn indexes
Treat connections as a shared capacity budget
A pool reuses database connections, but its limit usually applies to one process or pool. Six application instances allowed 20 connections each can consume 120 connections before background workers, deployments, and administration are counted. Write down the total potential demand and leave operating headroom. Increasing every pool limit can make an overloaded database do more competing work.
Go's sql.DB already manages a connection pool. SetMaxOpenConns can bound open connections, and DB.Stats exposes information useful for observing pool behavior. A bound also means callers may wait for a connection. Choose limits using measured demand and server capacity; investigate unreleased results, long transactions, and worker concurrency before assuming a larger pool will help.
Reference: Go: managing database connections
Verify the whole request under representative load
Repeat the workflow using the same data shape and concurrency, then compare typical and tail latency, errors, query volume, and database resource use. Include the busiest tenant and the background import that accompanied the original complaint. Check lock waits if requests remain blocked even after query execution improves; a transaction waiting on another transaction needs a different investigation from a table scan.
Document the change, the measurements, and the conditions that would justify further work. Release with a way to observe customer behavior and recover if the change causes trouble. Query changes, indexes, connection settings, caching, and larger instances are all tools; the evidence should determine which tool earns the next investment.
Sources & further reading
The guidance above combines our engineering recommendations with the official references below. Service features and platform requirements can change; check the current documentation when implementing.