Stop waiting on the network eleven times per page
The app got slower as pages grew, and the reason was not the queries. It was their number. A transaction is pinned to one connection, and a connection runs queries one after another. Every Promise.all in a withUser block looked like concurrency and was a queue. Measured against the real database: the round trip is ~36 ms, ten trivial `select 1` over one connection take 343 ms, over ten connections 39 ms. Nothing here is slow — the whole dashboard payload is under 200 kB, and every table is around a thousand rows. More connections is the wrong answer: the RLS session context is per transaction, so parallel reads mean parallel transactions, and those multiply the connections the database will grant. Fewer round trips instead. Postgres will return each sub-select as its own JSON column of one result. Per page view, counting the transaction frame: shell (paid by every page) 10 → 4 overview 14 → 5 employee file 14 → 7 employee list 8 → 6 The overview plus its shell went from 24 round trips to 9 — about 860 ms of pure waiting down to about 320 ms. The one trap is documented where it bites: inside json_agg, Postgres formats values itself and the driver's parsers (lib/db/pool.ts) never see them. Dates, numerics and uuids come out identical; timestamptz does not — "+00:00" where the driver gives "…Z". Timestamps are compared as strings in lib/history.ts to decide what happened later, and those two forms sort against each other wrongly. Every timestamptz in a bundled query therefore goes through zeitstempel(), which was checked character-for-character against the driver. Four loaders moved out of their pages into lib/ so the number of round trips can be measured without building a React tree, and so the new path could be held against the old one field by field: same rows, same order, same strings, for the overview and for four employee files chosen to differ (with history, a chief, a planned entry, one with dependents). withUser now counts the queries in each transaction and says so in development past a threshold. Without that, this grows back: each new tile brings its own query, and nobody notices until everybody does. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { sql, type Tx } from "./db";
|
||||
import { addDaysIso } from "./format";
|
||||
import type { OrgEb } from "./org";
|
||||
|
||||
/**
|
||||
* Der Stichtag, zu dem die Organisation *dieser* Person betrachtet wird.
|
||||
@@ -97,6 +98,34 @@ export function pickPlacements(rows: Row[], asOf: string): Map<string, Placement
|
||||
return byEmployee;
|
||||
}
|
||||
|
||||
/**
|
||||
* Die Besetzungen als *Teilabfrage* — zum Einhängen in die eine Abfrage, die
|
||||
* eine Seite ohnehin stellt (lib/db/json.ts).
|
||||
*
|
||||
* Ein Join statt einer eingebetteten Ressource. Und ohne die
|
||||
* 1000-Zeilen-Grenze von PostgREST fällt das seitenweise Nachladen weg, das
|
||||
* es dafür brauchte.
|
||||
*/
|
||||
export function besetzungenAbfrage(eb: OrgEb, employeeIds?: string[]) {
|
||||
const q = eb
|
||||
.selectFrom("position_assignments as pa")
|
||||
.innerJoin("om_positions as p", "p.id", "pa.position_id")
|
||||
.innerJoin("jobs as j", "j.id", "p.job_id")
|
||||
.select([
|
||||
"pa.employee_id",
|
||||
"pa.valid_from",
|
||||
"pa.valid_to",
|
||||
"p.id as position_id",
|
||||
"p.position_number",
|
||||
"p.org_unit_id",
|
||||
"p.is_chief",
|
||||
"j.title as job_title",
|
||||
])
|
||||
.orderBy("pa.employee_id");
|
||||
|
||||
return employeeIds ? q.where("pa.employee_id", "in", employeeIds) : q;
|
||||
}
|
||||
|
||||
export async function loadPlacements(
|
||||
tx: Tx,
|
||||
{ asOf, employeeIds }: { asOf: string; employeeIds?: string[] }
|
||||
@@ -148,6 +177,25 @@ export type ReportingLine = {
|
||||
* bei der Detailseite wandern damit neun Zeilen über die Leitung statt
|
||||
* achthundert.
|
||||
*/
|
||||
/**
|
||||
* Die Berichtslinien als *Teilabfrage* — zum Einhängen in die eine Abfrage,
|
||||
* die eine Seite ohnehin stellt (lib/db/json.ts).
|
||||
*
|
||||
* `filter` schränkt die Funktion selbst ein, nicht das Ergebnis im Speicher.
|
||||
*/
|
||||
export function berichtslinienAbfrage(
|
||||
eb: OrgEb,
|
||||
asOf: string,
|
||||
filter?: { employeeId?: string; actingManagerId?: string }
|
||||
) {
|
||||
let q = eb
|
||||
.selectFrom(sql<ReportingLine>`om_reporting_lines(${asOf}::date)`.as("l"))
|
||||
.select(["l.employee_id", "l.position_id", "l.org_unit_id", "l.is_chief", "l.formal_manager_id", "l.acting_manager_id"]);
|
||||
if (filter?.employeeId) q = q.where("l.employee_id", "=", filter.employeeId);
|
||||
if (filter?.actingManagerId) q = q.where("l.acting_manager_id", "=", filter.actingManagerId);
|
||||
return q;
|
||||
}
|
||||
|
||||
export async function loadReportingLines(
|
||||
tx: Tx,
|
||||
asOf: string,
|
||||
|
||||
Reference in New Issue
Block a user