shilpisingh-oracle/SQL-Optimization-RAG
0
Oracle SQL/PLSQL Query Optimization Tool
Overview
This project provides a Gradio-based web UI to help analyze and optimize Oracle SQL and PL/SQL queries using the OpenAI API for recommendations. It connects to an Oracle database (using thin mode), retrieves explain plans and execution timings, and suggests rewritten queries that aim to reduce execution time without changing results.
Important Security Notes
- Never store real credentials in the repository. Use environment variables or Hugging Face Spaces Secrets.
- The app blocks potentially destructive SQL statements (DROP, TRUNCATE, DELETE without WHERE, etc.) but you should still use a read-only or development DB for analysis.
Quick Start (Local)
- Copy
.env.exampleto.envand fill in credentials or set environment variables: - OPENAIAPIKEY
- ORACLEDSN (e.g. host:port/servicename)
- ORACLE_USER
- ORACLE_PASSWORD
- Create a virtualenv and install requirements:
python -m venv venv
venv\Scripts\Activate.ps1
pip install -r requirements.txt- Run the Gradio app:
python app.pyDeployment to Hugging Face Spaces
- Create a new Space, choose Gradio as the SDK.
- Push this repository to the Space's Git URL.
- In the Space settings, add Secrets:
OPENAI_API_KEY,ORACLE_DSN,ORACLE_USER,ORACLE_PASSWORD. - The Space will install packages from
requirements.txtand runapp.pyautomatically.
Notes:
- Hugging Face may not allow direct outbound connections to arbitrary databases. If connecting to an external Oracle DB fails, consider using an SSH tunnel or a hosted DB accessible from HF.
- Keep credentials in Secrets only.
Files
app.py- Gradio frontend.db.py- Oracle connection helpers and plan retrieval.optimizer.py- Query optimization logic.utils.py- Utility functions (normalization, hashing, safety checks, etc.).requirements.txt- Python dependencies.sample.sql- Example SQL queries.tests/- Unit and integration tests.
License
MIT
