Controlling query growth by designing database access around the response contract.
One of the easiest ways to build a slow API is to let serialization trigger database queries silently.
A transaction endpoint may begin with one query. Each transaction can then reference a user, wallet, country, provider, recipient, payment method, and status history. If the serializer accesses those relationships without preloading them, a page containing 100 transactions can generate hundreds of additional queries.
This is the N+1 query problem, but the important operational detail is that it often remains invisible in development. A small local dataset responds quickly. Production data, concurrent staff requests, and distant database connections expose the real cost.
Treat the response contract as a query plan
Before changing the database query, map every field in the response to its data source.…