Commit 09b59a02 authored by Liz Fong-Jones's avatar Liz Fong-Jones
Browse files

Ch20: add nested-IF -> SWITCH query-flattening example

A query-optimization snippet for the performance-engineering chapter: a derived
column that dispatches on a single field through nested IF(EQUALS($field, ...))
checks, flattened into one SWITCH that reads the field once. Illustrative DSL,
labeled to confirm the exact SWITCH signature against your tool's docs.

Co-Authored-By: default avatarClaude Opus 4.8 (1M context) <noreply@anthropic.com>
parent ac7ff1f8
Loading
Loading
Loading
Loading
+8 −0
Changes for 2e/chapter-20-performance-engineering/README.md: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -14,3 +14,11 @@ own method, called once from `Start()`, instead of running inside every
> copy, not to run. Notably, this class of bug couldn't be caught with tracing
> alone: the tracing hook fires *after* the mux has already been instantiated and
> the request matched to a route.

## Query optimization: flatten nested `IF` into `SWITCH`

[`nested-if-vs-switch.md`](nested-if-vs-switch.md) shows a query-tuning refactor:
a derived column that dispatches on one field via a tower of nested
`IF(EQUALS($field, …))` checks, collapsed into a single, cheaper-to-evaluate
`SWITCH`. (Illustrative DSL — confirm the exact `SWITCH` signature against your
tool's docs.)
+56 −0
Changes for 2e/chapter-20-performance-engineering/nested-if-vs-switch.md: 56 added lines, 0 removed lines.
Original line number Diff line number Diff line
# Flattening nested `IF(EQUALS(...))` into `SWITCH`

A common derived-column smell: dispatching on a single field by nesting `IF`s,
each one re-stating `EQUALS($same_field, …)`. It works, but it re-evaluates the
same field at every level, grows a wall of parentheses that's easy to
unbalance, and forces the engine to walk a chain of binary branches instead of
one multi-way dispatch. When you're tuning hot derived columns, collapsing these
into a `SWITCH` is cheaper to evaluate and far easier to read and maintain.

## Before: nested IF/EQUALS

Classify each request's HTTP method as a read, a write, or something else:

```
IF(
  EQUALS($http.request.method, "GET"), "read",
  IF(
    EQUALS($http.request.method, "HEAD"), "read",
    IF(
      EQUALS($http.request.method, "POST"), "write",
      IF(
        EQUALS($http.request.method, "PUT"), "write",
        IF(
          EQUALS($http.request.method, "DELETE"), "write",
          "other"
        )
      )
    )
  )
)
```

## After: a single SWITCH

```
SWITCH(
  $http.request.method,
  "GET",    "read",
  "HEAD",   "read",
  "POST",   "write",
  "PUT",    "write",
  "DELETE", "write",
  "other"
)
```

`SWITCH` reads the field once and dispatches on its value: a leading column to
test, then `case, result` pairs, then a trailing default. The two expressions
are functionally equivalent, but the second states the field once and removes
the nesting entirely.

> Illustrative, and the exact `SWITCH` argument order (and whether `IF` takes a
> two- or three-argument form) depends on your observability tool — confirm
> against its query/derived-column documentation before pasting. The point is the
> refactor: collapse a tower of single-field `IF(EQUALS(...))` checks into one
> multi-way dispatch.
+1 −1
Changes for README.md: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ Not runnable programs, but worth having in copy-pasteable form:
| Chapter | What | Paste into |
|---------|------|------------|
| [11 — Service Level Objectives](2e/chapter-11-slos/) | LLM prompts to draft/refine/grade SLOs, the generated SLI expressions, and a **bonus** constrained-decoding grammar the book omits | Your LLM tooling / SLI definitions |
| [20 — Performance Engineering](2e/chapter-20-performance-engineering/) | Example 20-1: the five-line router-reuse bug-fix diff | Read-only reference |
| [20 — Performance Engineering](2e/chapter-20-performance-engineering/) | Example 20-1's router-reuse bug-fix diff, plus a nested-`IF` → `SWITCH` query-flattening example | Read-only reference |

## First edition (`1e/`)