DrillsHarbor’s invoices page takes 2.4 seconds
Harbor’s invoices page takes 2.4 seconds
What happened
Harbor’s finance view lists the latest invoices with each team’s name. Claude Code wrote the loader last year, when a page held a dozen invoices. Finance now shows 100 per page, and the route has become the slowest in Harbor. Nobody has touched the code since.
Kofi, staff engineerGET /api/invoices is our slowest route this week, 2.4 s at p95. Postgres says every query on it takes under a millisecond. It’s how many there are that worries me.
What you seeServer log
GET /api/invoices?limit=100 200 2391ms
db: 101 queries, 2,272 ms waiting on the database
select id, name from teams where id = $1 ×100, 0.4 ms each
The MagisterRun it first and read what comes back. Then change the code if you like: your edits go to the review too.
Run it. The practice database counts queries and adds about 22 ms per round trip, like Harbor’s production database.
// The finance view's invoice list: GET /api/invoices?limit=50
// db.query(sql, params) sends one query to Postgres and resolves to { rows }.
async function listInvoices(db, limit) {
const { rows: invoices } = await db.query(
"select id, number, team_id, total_cents, status, issued_at from invoices order by issued_at desc, id desc limit $1",
[limit],
);
// The page shows each invoice's team name.
for (const invoice of invoices) {
const { rows } = await db.query("select id, name from teams where id = $1", [invoice.team_id]);
invoice.team = rows[0];
}
return invoices;
}
const page = await listInvoices(db, 50);
console.log(`${page.length} invoices, ${db.queryCount} queries, about ${db.elapsedMs} ms waiting on the database`);
The code runs in your browser against stand-in data. Nothing is sent anywhere.
Your answer
Reviewed by AI against what a good fix needs. About 10 seconds.Sign in to get your answer reviewed
It's free. Anything you've typed here stays in this browser and is waiting when you come back.
Sign in with email