Lawfare Group → Knowledge Institute → Lawfare Academy

← Course syllabus · Related lesson

A faster report must still describe one coherent database state

Current complete on-site edition 6

Required teaching and complete optional models are supplied on LawfareGroup.com. Publisher and primary links are optional provenance or further reading. Florida is the contemporary state-law default; federal rules and explicitly comparative accounting frameworks are distinguished. Independent subject review and observed human learner testing remain pending.

Cumulative data-system dossier, transfer models and 30-point rubric

Preserved original teaching integrated with complete current on-site preparation. Independent human subject review and observed learner testing remain pending.

Learning objectives

Freeze the question and result before tuning

A business report needs a defined population, unit of analysis, time boundary, status policy and valuation basis. Optimizing a query that duplicates charges or mixes states produces a faster wrong answer. Preserve a small fixture with known results, boundary cases and an expected row key set before changing the physical access path. Store historical transaction prices on the event record where that is the defined business fact; joining today’s catalog price does not reconstruct an earlier invoice.

The separate index fixture contains ten thousand synthetic events with an event identifier, customer identifier, integer day number and amount in integer minor currency units. Its query requests the count and amount for customer 42 on days 10 through 20, inclusive. Compute the expected answer independently from the generated events, then compare SQL before and after an index. Inclusive endpoints are part of this exercise’s contract, not a universal recommendation for timestamp reporting. A half-open time interval may be preferable for a real dated report, but it must be chosen explicitly.

Read a plan as an execution explanation, not a correctness certificate

A candidate composite index begins with customer_id, followed by the range-filtered day and a stable event identifier. Such ordering can support the equality predicate followed by the range under the actual planner and data distribution. It is not a universal optimal index: selectivity, table size, ordering, covering needs, statistics, other workloads and write cost matter. Adding every possible column or index can increase storage and maintenance costs.

Use EXPLAIN QUERY PLAN to inspect the actual access strategy. SQLite explicitly warns that the output format is for interactive debugging and can change between releases. Do not make a course exercise depend on one exact wording or node-number sequence. A scan on a small table can be reasonable; seeing the word index does not prove a query is fast. Measure representative workloads with a stated environment and cache conditions before claiming performance improvement.

The reproducible test checks that the count and amount remain exact after adding the index and prints both plans for inspection. It does not assert a universal millisecond result or force an index to make an attractive screenshot. A uniqueness constraint can also change which data are allowed, so distinguish that semantic change from an ordinary non-unique access-path experiment.

A report assembled from several reads can mix snapshots

In a separate file-backed SQLite WAL fixture, a first connection begins a read transaction and reads a ledger amount of 100. A second connection commits a change to 150. The first read transaction still sees 100; after ending that transaction and beginning a new read, it sees 150. This demonstrates the stated snapshot behavior with separate connections and no shared-cache dirty-read configuration. It does not mean every database engine, isolation level or web request behaves identically.

If a dashboard reads its order subtotal before an update and its refund subtotal after that update in separate autocommit reads, the resulting report may never have represented one coherent database state. Identify the transaction boundary, snapshot time and late-arriving-event policy needed for the business question. A current consistent snapshot and a historically correct as-of report are different requirements; the latter may also need effective timestamps, preserved event history or an appropriate version model.

Do not hold a production read transaction open indefinitely merely to freeze a screen. Resource retention and operational effects need evaluation. These laboratory connections operate on disposable synthetic files; no live course or learner database is touched. A complete validation record states the expected answer, unchanged logical keys, chosen snapshot boundary, plan evidence and limits of any performance claim. Use the complete current on-site controls/query studios and preserved temporal application; official links are optional provenance.

Worked example: Indexing preserves the reporting contract

The generated event set is evaluated independently for customer 42 and the inclusive day range. SQL must return the same count and minor-unit total before and after index creation. A changed total means the logical query or data changed; it cannot be justified as an optimization.

Worked example: Two correct reads can belong to different states

The original read transaction sees 100 even after the writer commits 150. A later transaction sees 150. A dashboard combining unrelated transaction boundaries must not call the mixture a single-snapshot total merely because each read individually succeeded.

Progressive optional practice

Attempt each problem before opening its model. Then revise the reasoning under the changed conditions.

Guided

What must be checked before comparing query runtimes?

Compare the model reasoning

The exact population, predicates, row grain, expected result and stable ordering where relevant.

Changed conditions

The tuned query drops customers with no matching events.

Compare the revised analysis

It changed semantics. Restore the intended population before interpreting a timing difference.

Independent

Why should a test not hardcode EXPLAIN QUERY PLAN text?

Compare the model reasoning

The official output format can change; inspect strategy while testing semantic results independently.

Changed conditions

The plan changes after a database version update.

Compare the revised analysis

Recheck behavior and performance under the new environment instead of treating different display wording as proof of incorrect results.

Transfer

Does a consistent current snapshot answer every historical as-of question?

Compare the model reasoning

No. Historical reconstruction may require effective dates, preserved prices and event/version history.

Changed conditions

A late refund is posted for an earlier order.

Compare the revised analysis

Apply the stated event-time and posting-time policy, preserving both dates rather than silently rewriting an earlier published result.

Preserved historical development snapshot · Course content review

Source readings and scope

SQLite: query planning — Read equality/range access, multi-column indexes, ordering and tradeoffs. Primary source checked October 27, 2026. The business records and numerical models are original synthetic exercises.

SQLite: EXPLAIN QUERY PLAN — Read the output-format warning and interpretation guidance. Primary source checked October 27, 2026. The business records and numerical models are original synthetic exercises.

SQLite: isolation — Read transaction visibility and WAL snapshot behavior. Primary source checked October 27, 2026. The business records and numerical models are original synthetic exercises.

Reproduce the worked examples in Python 3. Standard library only; synthetic data; no production records or network access.