Skip to content

Query metrics, N+1 reference prevention, and reference-aware queries

Choose a tag to compare

released this 07 Dec 02:44
· 18 commits to working since this release

This release focuses on instrumenting query execution and reducing N+1 queries. It introduces query metrics, auto-detects and includes reference JOINs during query building, and updates row processing to skip reloading references that were already included. Several logs were promoted for better visibility during debugging.

New Features

  • Query execution metrics (src/metrics/QueryMetrics.ts, src/metrics/index.ts)
    • Adds a QueryMetrics class with per-model and total query counters and a singleton export
    • Emits a debug log every 10 queries with per-model breakdown
    • Integrated into the ALL operation to record queries around count() and findAll() (src/ops/all.ts)

Improvements

  • Reference-aware query building to prevent N+1

    • addReferenceIncludes is now used by ALL and GET to auto-detect and add reference includes to options (src/ops/all.ts, src/ops/get.ts)
    • buildQuery now accepts references and registry to support reference join auto-detection (src/ops/all.ts; tests updated in tests/ops/all.test.ts)
    • RowProcessor accepts includedReferences and skips buildSequelizeReference when data is preloaded via INCLUDE (N+1 prevention) (src/RowProcessor.ts)
    • Reference loading adjusted to use async map; includes detailed skip logging at default level (src/RowProcessor.ts)
  • Logging level adjustments for better visibility

    • Promoted selected debug logs to default in RowProcessor, AggsAdapter, and ReferenceBuilder (src/RowProcessor.ts, src/processing/AggsAdapter.ts, src/processing/ReferenceBuilder.ts)
  • Operator support in QueryBuilder

    • Enables the '!=' operator, mapping it to Sequelize Op.ne (validated in tests/QueryBuilder.test.ts)

API Changes

  • buildQuery signature extended
    • Now accepts (itemQuery, model, references, registry) to support reference include auto-detection (src/ops/all.ts; tests updated in tests/ops/all.test.ts)
    • Update any custom callers accordingly

Bug Fixes / Behavior Changes

  • N+1 reference prevention in row processing
    • When a reference is already loaded via INCLUDE, RowProcessor skips issuing an additional query and logs the decision (src/RowProcessor.ts)

Testing Improvements

  • QueryBuilder reference join auto-detection tests

    • Adds comprehensive tests covering direct and nested reference conditions, multiple conditions, null handling, operator coverage, and error cases when associations are missing (tests/QueryBuilder.test.ts)
  • ALL operation tests updated

    • Verifies buildQuery is called with references and registry and that processRow receives includedReferences in addition to includedAggregations (tests/ops/all.test.ts)

Code Reference (from commit 14c62eb)

  • Add query metrics, N+1 reference prevention, and logging level adjustments
    • Metrics: src/metrics/QueryMetrics.ts (singleton export in src/metrics/index.ts)
    • ALL: addReferenceIncludes, buildQuery signature update, recordQuery integration, pass includedReferences to RowProcessor
    • GET: import addReferenceIncludes, pass includedReferences to processRow
    • RowProcessor: includedReferences param, skip logic, async mapping, log promotions
    • AggsAdapter and ReferenceBuilder: log promotions and formatting
    • Tests: '!=' operator support; Reference Join Auto-Detection suite; updated ALL expectations