Skip to content

feat(schema): schema insights advisor (issue #6 scope A)#93

Merged
ringo380 merged 1 commit into
mainfrom
feat/schema-insights-issue-6
May 21, 2026
Merged

feat(schema): schema insights advisor (issue #6 scope A)#93
ringo380 merged 1 commit into
mainfrom
feat/schema-insights-issue-6

Conversation

@ringo380
Copy link
Copy Markdown
Owner

Summary

Implements sub-scope A (broader non-index schema analysis) of #6. PR #54 delivered the index-recommendation slice; this adds a SchemaAdvisor service producing four structural/statistical insights, displayed in a new "Schema insights" panel parallel to the index panel.

Insights

  • FK fan-out — one-to-many JOIN without aggregation/DISTINCT (row multiplication)
  • Nullable FK — nullable FK column in a JOIN silently drops orphan rows
  • Low cardinality — WHERE/JOIN on a low-NDV column (poor selectivity)
  • Stale stats — planner stats old (≥14d) or never gathered

Backend coverage

Degrades gracefully to skipped (never errors):

  • PostgreSQL: full — pg_stats.n_distinct, pg_stat_user_tables.last_analyze
  • MySQL: partial — STATISTICS.CARDINALITY, innodb_table_stats.last_update (the real stats-refresh time, not the inverted update_time proxy)
  • SQLite: FK insights only (no NDV/staleness available)

Changes

  • DatabaseIntrospector: NDV + stats-freshness fetchers
  • LiveSchemaContext: snapshot cache bump v1→v2, last_analyzed + per-column ndv
  • QueryAnalysis.schema_insights JSONField + migration 0007
  • Guarded advisor call in the grade view (advisor failure never blocks the grade)
  • grade_results.html panel + dark-mode severity pills

Placement

Mirrors the existing index panel exactly — both render on grade_results.html (reached via history "View full analysis"). No change to enhanced_grade_results.html.

Test plan

  • 20 new tests: 17 unit (every insight's positive/negative/skipped path) + 3 e2e (persistence, no-connection empty, advisor-failure resilience)
  • Full analyzer suite green (720 tests, 0 fail/err, 14 skip)
  • New files flake8-clean

Relates to #6

Add SchemaAdvisor service producing four non-index schema insights
alongside the existing index recommender:

- FK fan-out: one-to-many JOIN without aggregation/DISTINCT
- Nullable FK: nullable FK column in a JOIN drops orphan rows
- Low cardinality: WHERE/JOIN on a low-NDV column (poor selectivity)
- Stale stats: planner stats old (>=14d) or never gathered

Backend coverage degrades gracefully to "skipped" rather than erroring:
PostgreSQL full (pg_stats.n_distinct, pg_stat_user_tables.last_analyze),
MySQL partial (STATISTICS.CARDINALITY, innodb_table_stats.last_update),
SQLite FK insights only.

Extends DatabaseIntrospector with NDV + stats-freshness fetchers, bumps
the live schema snapshot cache to v2, adds QueryAnalysis.schema_insights
(migration 0007), wires a guarded advisor call into the grade view
(failure never blocks the grade), and renders a Schema insights panel in
grade_results.html with dark-mode severity pills.

Adds 20 tests (17 unit covering every insight's positive/negative/skipped
path, 3 e2e). Full analyzer suite green.
@ringo380 ringo380 merged commit dd51429 into main May 21, 2026
3 of 4 checks passed
@ringo380 ringo380 deleted the feat/schema-insights-issue-6 branch May 21, 2026 09:07
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