Skip to content

feat: add AI query optimization feature#56

Merged
wmoew merged 7 commits into
devfrom
feature/ai-query-enhancement
May 23, 2026
Merged

feat: add AI query optimization feature#56
wmoew merged 7 commits into
devfrom
feature/ai-query-enhancement

Conversation

@Bryancabanin
Copy link
Copy Markdown
Contributor

Description

Adds an AI-powered query optimization feature that analyzes a user's SQL query alongside its performance metrics and database schema context to suggest an improved version. Improved the QueryComparisonPage with color coding, no-difference handling, and a performance bar chart.

Reproduction steps

  1. Clone the repo and run docker compose up
  2. Log in with GitHub OAuth
  3. Navigate to Query Tester

Testing AI Optimization on a new query:
4. Enter your database URI, query name, and a SQL query, then click Fetch Metrics
5. Once metrics appear, click "Optimize Query with AI"
6. The AI will analyze your query and return a suggested optimized version with an explanation
7. Click "Load into Tester" to load the suggested query back into the form and re-run it

Testing AI Optimization on a past query:
8. Click Query History and load a previous query
9. Re-enter your database URI in the Database URI field (we don't save the URI for security)
10. The "Optimize Query with AI" button will now be clickable — click it to get an AI suggestion

Checklist

  • I've followed the Contributing guidelines
  • I've titled my PR according to the Conventional Commits spec
  • I've added tests that fail without this PR but pass with it
  • I've linted, tested, and commented my code
  • I've updated documentation (if appropriate)

- Add aiController with getQueryOptimization handler
- Add queryOptimizationService to call OpenAI GPT-4o with query and metrics
- Add schemaService to fetch table names and schema context from user DB
- Add schemaUtils to format schema for GPT prompt
- Add sqlParserUtils for AST column ref and alias extraction
- Add validateQuery to ensure only SELECT queries are optimized
- Add optimizationLimiter rate limiting middleware
- Add POST /query/optimization route
- Add openai, node-sql-parser, express-rate-limit dependencies
- Add isQueryLoaded prop to TestQueryForm to lock fields and disable submit when query is loaded
- Add Optimize Query with AI button with loading state
- Add OptimizationResultCard component to display suggested query, explanation, and load into tester
- Add handleNewQuery to reset query state
- Add auto-scroll to optimization result on load
… handling

- Pre-compute rounded metric values to avoid repeated toFixed calls
- Add color coding to performance summary (green/red/grey)
- Handle equal metric values with No difference label
- Improve query recommendation caption to show "Queries are equivalent" when metrics are equal
…trics

- Add warmup run before EXPLAIN ANALYZE to reflect steady state cache performance
- Fix shared hit/read blocks to read from Plan node instead of Planning node
- removed queryAnalysisService
…ry is active

- Add isQueryLoaded prop to make Query Name and Query fields read-only when loaded
- Disable submit button when query is already loaded
- Update tooltip to guide user to click New Query when fields are locked
- Reduce bottom margin from mb:4 to mb:2
@socket-security
Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedopenai@​6.34.074100100100100
Addednode-sql-parser@​5.4.010010010083100
Addedexpress-rate-limit@​8.5.19910010094100

View full report

@wmoew wmoew merged commit ea64327 into dev May 23, 2026
2 checks passed
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.

2 participants