Slow Query Log

The queries costing you the most, ranked by total time rather than mean. The 9-second report running twice a day is easy to find; the 40ms lookup running four thousand times a minute is the one saturating the database.

src/components/ui/slow-query-log.tsx

About

From the component's own source, where it was written.

The queries costing you the most, ranked by total time.

**Total time, not mean.** The query everyone finds first is the 9-second report that runs twice a day; the one actually saturating the database is the 40ms lookup running four thousand times a minute. Ranking by mean × calls surfaces the second, which is the whole point of a slow query log and the thing a naive "slowest queries" list gets backwards.

Statements are normalised by the caller — $1 rather than a literal — so the same query with different parameters aggregates into one row instead of ten thousand.

A query with no index scan is flagged. It is the single most actionable finding here and it is invisible in the timings alone.

Install

npx astralyx-ui add slow-query-log

Usage

import { SlowQueryLog } from '@/components/ui/slow-query-log'

<SlowQueryLog queries={queries} />

Composer

Change the props and the source below follows.

Slow queriesCallsMeanTotal
Props
tsx
<SlowQueryLog queries={queries} rankByTotal={true} />

Examples

Ranked by total database time

API

Generated from the source, so it cannot fall behind the code. One table per exported component, then hooks, functions and types.

SlowQueryLog

Extends Omit<ComponentProps<'div'>, 'children'>.

PropTypeDefaultDescription
queriesSlowQuery[]required{ id, statement, calls, meanMs, p95Ms?, rows?, seqScan?, meta? }. Statements should be normalised by the caller so the same query aggregates into one row.
rankByTotalbooleantruemean × calls. Turn it off to rank by mean, which is the ordering most tools use and the one that hides the real problem.
seqScanLabelstring'no index'—
emptyLabelstring'No queries in this window.'—
formatDuration(ms: number) => stringdefaultDuration—
headers{ query?: string; calls?: string; mean?: string; total?: string }——
labelstring'Slow queries'—

Types

SlowQuery

The queries costing you the most, ranked by total time.

FieldTypeDescription
idstring—
statementstringNormalised statement — parameters replaced with placeholders.
callsnumberExecutions in the window.
meanMsnumberMilliseconds.
p95Ms?number—
rows?numberRows returned per call, on average.
seqScan?booleanNo index was used — the most actionable finding here, and invisible in the timings alone.
meta?ReactNode—