rdst scan
Find SQL queries in your application's ORM code and analyze them for performance, optionally as a CI check.
rdst scan walks a codebase, finds ORM-generated SQL in Python and TypeScript
projects, converts it to real SQL, and (optionally) analyzes each query for
performance issues. With --check it becomes a CI gate.
Supported ORMs
| Language | ORMs |
|---|---|
| Python | SQLAlchemy (1.x and 2.0), Django ORM |
| TypeScript / JavaScript | Prisma, Drizzle |
All four work against both PostgreSQL and MySQL targets.
Common usage
# Scan a directory and list the queries it finds
rdst scan ./backend --schema prod-orders
# Analyze each query against the live database
rdst scan ./backend --schema prod-orders --analyze
# Schema-only analysis (no DB connection needed, useful in CI)
rdst scan ./backend --schema prod-orders --analyze --shallow
# Only scan files changed in the current PR
rdst scan ./backend --schema prod-orders --diff HEAD --analyze --check
# Fail the build if any query has a risk score below 30
rdst scan ./backend --schema prod-orders --analyze --check --fail-threshold 30How extraction works
RDST detects ORM call sites with code parsing. Python detection looks for SQLAlchemy and Django expressions. TypeScript and JavaScript detection looks for Prisma and Drizzle expressions.
AI translates detected call sites into SQL using schema context. Review that SQL before relying on the analysis. Fixed prompts and low temperature don't guarantee identical SQL or recommendations across runs.
Example output
$ rdst scan ./backend --schema prod-orders --analyze --shallow
Scanning ./backend ...
Found 47 ORM call sites in 22 files.
Extracted 47 SQL queries.
Analyzing with shallow mode (no DB connection) ...
Score File Query
─────── ────────────────────────────── ────────────────────────────
88 services/order_service.py:42 SELECT o.id, o.total_cents ...
72 services/order_service.py:114 SELECT COUNT(*) FROM orders...
65 services/customer_service.py:8 SELECT c.* FROM customers c ...
48 services/reports.py:201 SELECT c.id, COUNT(o.id) ... ⚠ warn
21 services/reports.py:233 SELECT * FROM orders WHERE ... ✗ fail
...
Summary
47 queries analyzed.
Worst score: 21 (below fail threshold 30)
Below warn (50): 3
Below fail (30): 1
CI status: FAILAnalysis modes
| Mode | Needs DB? | What it checks |
|---|---|---|
| (no flag) | no | Extraction only: produces SQL and saves to registry |
--analyze --shallow | no | Schema-only LLM analysis. Detects SELECT *, missing LIMIT, obvious missing indexes, N+1 patterns. |
--analyze | yes | Full EXPLAIN ANALYZE against the target plus LLM analysis. Most accurate, slowest. |
Shallow mode is the recommended default for CI. It doesn't require a database password or network access to your production database, yet catches most practical issues.
CI mode (--check)
--check turns rdst scan into a build gate:
rdst scan ./backend --schema prod-orders --analyze --shallow --check \
--fail-threshold 30 --warn-threshold 50| Exit code | Meaning |
|---|---|
0 | All queries meet the fail threshold (PASS) |
1 | At least one query below fail threshold (FAIL) |
The summary panel shows worst-query score, thresholds, and the counts in
each band. Without --check, scores are informational only and the command always
exits 0.
Scanning just the diff
Pair --diff <ref> with --check to only gate queries that were touched in the
current change:
rdst scan ./backend --schema prod-orders --diff HEAD~1 --analyze --check| Ref | What gets scanned |
|---|---|
HEAD | Uncommitted changes |
HEAD~1 | Files changed in the last commit |
main, abc123 | Files changed since that commit/branch |
Flags reference
| Flag | What it does |
|---|---|
directory | Directory to scan (positional) |
--dry-run | Show what would be scanned without scanning |
--analyze | Run performance analysis against the extracted queries |
--shallow | Schema-only analysis (no DB connection). Use with --analyze. |
--schema <target> | Target name to use for schema context |
--output {table,json} | Output format |
--diff <ref> | Only scan files changed since ref |
--check | CI mode: exit 1 if issues found |
--warn-threshold <N> | Risk score threshold for warnings (default 50) |
--fail-threshold <N> | Risk score threshold for failures (default 30) |
--file-pattern <glob> | Glob pattern to filter files (e.g. *.py) |
--nosave | Don't save extracted queries to the registry |
--sequential | Run analysis queries one at a time |
Risk scoring
Each query gets a score from 0 to 100:
| Range | Meaning |
|---|---|
| 80 to 100 | Low risk |
| 50 to 79 | Medium risk |
| 30 to 49 | High risk |
| 0 to 29 | Very high risk: likely to cause production incidents |
RDST computes the score from the analyzer's findings. A query scores lower when it lacks supporting indexes or scans too much data. Selecting every column from a large table or repeating a query for each result row also lowers the score.
Scores depend on translated SQL, schema context, and analysis output. Review changes in scores alongside the findings that produced them.
See also
rdst analyze: re-analyze any extracted query by hash for a full reportrdst query list: browse every query scan has saved- Core concepts: The semantic layer: annotate your schema once and every scan gets better