Skip to content

Group N+1 and duplicate queries into expandable cards - #1

Merged
CodeWithDennis merged 16 commits into
mainfrom
feat/n-plus-one-highlighting
Dec 1, 2026
Merged

CodeWithDennis merged 16 commits into
mainfrom
feat/n-plus-one-highlighting

Conversation

@CodeWithDennis

@CodeWithDennis CodeWithDennis commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

What

The Queries panel only flagged a statement as Duplicate when its SQL and bindings were identical, so a real N+1 (the same query run once per row, with a different id each time) was never flagged. Opaque bindings made it worse, since they can't be compared at all. Repeated queries also filled the list with near-identical rows.

This PR groups repeated queries into compact, expandable cards and flags N+1 patterns. It is frontend-only (resources/assets/lens.js, plus a README line and tests); there are no Rust or API changes.

What you see

Each group is one card instead of N repeated rows:

  • Count tile on the left: 5× over N+1 (red) or 2× over DUP (orange). The tooltip gives the run count and total time.
  • Header: the SQL once, with the call site, connection and driver underneath, a sparkline of the run durations (one bar per run, or up to 12 bucketed bars for longer groups), the group's total time, and a chevron.
  • Expand (click the card header, or Enter/Space when it is focused) to list every run: index, bindings or call site, a duration bar, and the time. A footer has one Copy button for the group's SQL, and says Showing k of n runs when a filter hides some of them. The runs slide open with a short staggered fade, and the header stays pinned while you scroll a long list.
  • Summary bar: a new red N+1 queries button filters the list to N+1 groups, like the existing duplicates button. Combining both shows only runs that match both, which is usually nothing. A filter that has nothing to match on the selected request (for example the N+1 filter on a request with no N+1) is ignored instead of emptying the list.

Definitions

  • N+1: the same SQL run 3 or more times with varying bindings (N_PLUS_ONE_THRESHOLD). Opaque bindings can't be compared, so they count as varying. Two runs are more likely coincidence than a loop.
  • Duplicate: the same SQL and bindings, two or more times (this includes 3+ identical runs). Same card, in orange. If a query is N+1 and some of its runs are also exact repeats, it shows as N+1 and those runs get a small Duplicate badge.
  • The tile and total always describe the whole group. When Lens truncated the request (truncated_count > 0) both card types show the count as a lower bound (≥5×).
  • Statements without a SQL string are never grouped, and missing, negative or non-finite timings are shown as 0 (in cards and in plain rows).
  • The render cap (MAX_RENDERED_QUERIES) counts cards and plain rows, so a huge group can't push distinct queries out of the list.

Performance, accessibility, compatibility

  • Expand and collapse are CSS-only transitions (grid-row height, opacity, transform, no JS timers). Only the toggled card animates and the stagger is capped. Re-rendering 5,000 statements takes about 10-20 ms. prefers-reduced-motion turns the card transitions off.
  • The header is a real <button> with aria-expanded, a focus ring, and an accessible name such as "5 N+1 queries, 128µs total: select …"; the tile, sparkline, total and chevron are aria-hidden. Collapsed runs are inert and visibility: hidden, so they are skipped by Tab and screen readers immediately.
  • Layout is checked from 1280px down to 320px (the sparkline hides and the total moves under the SQL on narrow screens), in light and dark themes, and on right-to-left host pages (the toolbar now sets direction: ltr). Light-theme text uses darker --lens-*-strong tokens for contrast.
  • Open or closed state is kept per request, in memory only; it survives filters and re-renders.
  • Sparkline and bars are inline SVG with no inline style attributes, so strict CSPs are fine. It uses color-mix(), overflow: clip, inert and grid-template-rows transitions (all evergreen browsers; older ones just lose tints or the animation).

Tests

tests/lens_n_plus_one.test.mjs loads the real, unmodified lens.js in a Node vm sandbox with minimal DOM stubs, so there are no npm dependencies. Its 30 tests cover the definitions and thresholds, duplicate and N+1 precedence, opaque bindings, truncation, missing SQL and odd timings, expand/collapse in place (including aria-expanded, inert and focus), stable group ids across filters, re-renders and requests (including awkward SQL and hostile request ids), counts versus filtered runs, the render cap, sparkline bucketing, location handling, escaping of every user-controlled value, both filters and the stuck-filter fix. Breaking lens.js in ~45 different ways makes at least one test fail each time. CI runs them with node --test tests/*.test.mjs (Node 22) and lints with Biome.

I also checked it in headless Chromium against a demo app (laravel-rust + SQLite): grouping, the expand animation, the filters, keyboard use, and narrow widths.

Notes

  • Grouping is by SQL only (plus the bindings check above), so a loop of repeated INSERT/UPDATE statements is flagged too. Those usually should be batched as well.
  • The threshold is a single constant if you'd rather tune it.
  • tests/** is in the crate's include list, so the small .mjs test file ships with the published crate. Say if you'd rather exclude it.

@CodeWithDennis CodeWithDennis changed the title Highlight N+1 queries in the Queries panel Group N+1 and duplicate queries into expandable cards Dec 1, 2026
@CodeWithDennis
CodeWithDennis merged commit 5e60a25 into main Dec 1, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant