Why Dashboards Slow Down Silently
Most endpoint slowdowns do not show up with 10 test rows in local development. In GGF-FMS, operational views pulled active plantation blocks across 100+ sites together with shift statuses, worker logs, and inspection flags. Once historical MySQL tables grew, screens felt noticeably sluggish even though each individual SQL query looked cheap.
Profiling the API endpoints revealed a classic culprit: lazy relation loading inside serialization loops caused a single request to trigger hundreds of tiny redundant SELECT statements.
Eliminating N+1 bottlenecks across 100+ plantation sites
Comparing lazy per-row relation queries against constrained Laravel eager loading.
Per-row nested queries
Each monitoring row fired extra SQL lookups for location, shift block, and worker logs.
Sluggish web dashboards + manual weekly spreadsheets
Batched with(...) & lean payload
Explicit column selection, indexed joins, and daily pre-aggregation in MySQL.
Fixing the Contract Alongside the Query
Adding with(...) blindly only solves half the issue if response serializers still pull unneeded blobs or execute hidden computed getters ($appends). Across Laravel 12 controllers (LocationActivitiesController, FormGeneratorController, Locations/ResourceController) and React Native (@op-engineering/op-sqlite), we paired two concrete steps:
- Constrained eager loading: explicitly declare loaded relations (
Location::query()->with(['analysesNdvis', 'analysesPlantWeights', 'plan'])andFormStudioTable::with(['schemas', 'parent.schemas'])) while joining rollup summary tables in MySQL. - Database-level daily rollups & offline sync parity: pre-aggregate activity realization tables in MySQL so React Mantine dashboards and mobile
NoteRecordlocal SQLite tables stay responsive.
// Verified offline NoteRecord persisted via @op-engineering/op-sqlite (fms-mobile)
export type NoteRecord = {
id: number;
web_id?: string | null;
location?: string | null;
status: string;
tagging?: string | null;
description: string;
coordinat: string;
is_synced: boolean;
note_code?: string | null;
type: string;
created_by: string;
};Guardrails Before Merging Query Changes
Before shipping query refactors across active sites, apply three quick verification gates:
# Inspect SQL round-trips on paginated API routes before deployment
php artisan config:cache && php artisan route:cache && npm run build- Assert query count remains constant when seeded row count doubles (from 20 to 200 items).
- Confirm both Android offline sync and web dashboards accept identical trimmed resource shapes.
- Measure before and after latency against real anonymized datasets.
Measured Impact
Enforcing explicit relation loading and slim API resources improved operational data-loading speed by 40% and removed manual weekly spreadsheet reconciliation (saving roughly 10 hours per week) across field teams.