A terminal-based Python tool for analyzing and optimizing SQL query performance across SQLite, MySQL, and PostgreSQL databases.A powerful, terminal-based Python tool for analyzing, measuring, and optimizing SQL query performance. This tool helps developers identify slow queries, provides actionable optimization suggestions, and generates comprehensive performance reports.
-
Multi-database support: SQLite, MySQL, PostgreSQL- ๐ Multi-Database Support: SQLite, MySQL, and PostgreSQL
-
Performance measurement with millisecond accuracy- โก Performance Measurement: Precise execution time tracking with millisecond accuracy
-
Slow query detection based on configurable thresholds- ๐ Slow Query Detection: Automatic identification of queries exceeding performance thresholds
-
Pattern-based optimization suggestions:- ๐ก Smart Optimization Suggestions: AI-powered analysis with actionable recommendations including:
-
Index recommendations for WHERE, JOIN, ORDER BY clauses - Index suggestions for WHERE, JOIN, and ORDER BY clauses
-
Detection of SELECT * anti-patterns - Detection of
SELECT *anti-patterns -
Subquery optimization - Subquery optimization recommendations
-
LIKE wildcard warnings - LIKE wildcard performance warnings
-
Missing WHERE clause detection - Missing WHERE clause detection
-
Multiple JOIN analysis - Multiple JOIN analysis
-
-
CSV logging of query executions- ๐ Comprehensive Logging: CSV export of all query executions with performance metrics
-
Summary reports with statistics- ๐ Summary Reports: Detailed session summaries with statistics and insights
-
Rich terminal output (optional)- ๐จ Rich Terminal Output: Beautiful, color-coded output using Rich library (optional)
-
Interactive and batch modes- ๐ Interactive & Batch Modes: Run queries interactively or execute single queries
-
๐ฏ Modular Design: Easy to extend and customize
-
Python 3.7+### Prerequisites
-
Database connectors as needed
-
Python 3.7 or higher
pip install -r requirements.txt### Step 1: Clone or Download
Dependencies: mysql-connector-python, psycopg2-binary, rich (optional)cd d:\SQl_Query_optimization
Copy .env.example to .env and fill in credentials.### Step 2: Install Dependencies
pip install -r requirements.txt
python sql_optimizer.py --db-type sqlite --database test.db**Dependencies:**
```- `mysql-connector-python` - For MySQL support
- `psycopg2-binary` - For PostgreSQL support
### Single Query- `rich` - For enhanced terminal output (optional but recommended)
```bash
python sql_optimizer.py --db-type sqlite --database test.db --query "SELECT * FROM users"**Note:** SQLite support is included in Python's standard library.
-
--threshold: Custom slow query threshold (ms)Copy the example environment file and configure your database credentials:
-
--log-file: Custom CSV log file
-
--summary-file: Custom summary file```powershell
Copy-Item .env.example .env
-
SQLOptimizer class: Main optimizer with connect(), execute_query(), analyze_performance(), suggest_optimizations(), log_results()
-
Interactive mode: CLI interface## ๏ฟฝ Protecting secrets and the
.envfile -
Configuration: config.py for thresholds and settings
Do NOT commit your .env file (it contains sensitive credentials). This project ships a .env.example file so you can copy it to .env locally and fill in your credentials.
-
SELECT *: High severity, suggest column specificationIf you accidentally added
.envto git, untrack it and commit the removal from the index (this keeps your local file but removes it from the repository): -
Missing WHERE: Medium, suggest filters
-
Multiple JOINs: Medium, ensure indexesPowerShell commands (run from the repository root):
-
Subqueries: Medium, consider JOINs/CTEs
-
ORDER BY without LIMIT: Medium, add LIMIT```powershell
-
LIKE leading wildcard: High, restructure or use full-text# 1. Make sure .env is in .gitignore (this repo's .gitignore already includes it)
-
DISTINCT: Low, verify necessitygit status
Automatic recommendations for columns in WHERE, JOIN, ORDER BY.git rm --cached .env
git add .gitignore
Edit config.py:git commit -m "chore: stop tracking .env and add to .gitignore"
-
SLOW_QUERY_THRESHOLD: Default 500.0 msgit push origin main
-
WARNING_THRESHOLD: 300.0 ms```
-
MAX_ROWS_DISPLAY: 10
-
USE_RICH_FORMATTING: TrueIf
.env(or other secrets) were already committed in previous commits, you should assume those secrets may be exposed and rotate them immediately. To remove a secret entirely from repository history you can use tools likegit filter-repoor theBFG Repo-Cleaner. These operations rewrite history and require force-pushing โ use with caution. -
LOG_FILE: 'query_log.csv'
-
SUMMARY_FILE: 'query_summary.txt'Short guidance for history cleaning:
- Rotate the compromised credentials right away (change passwords, API keys, tokens).
- Use
git filter-repoor BFG to remove the file from all commits (example with BFG):
# Using BFG (Java required):
bfg --delete-files .env
git reflog expire --expire=now --all
git gc --prune=now --aggressive
git push --force- Notify collaborators to reclone or reset their local copies because history was rewritten.
If you'd like, I can run the git rm --cached .env step for you and create the commit (I will not rewrite history or force-push). Tell me if you want me to perform that local git change now.
python sql_optimizer.py --db-type sqlite --database test.dbpython sql_optimizer.py --db-type mysql --host localhost --user root --password yourpass --database testdbpython sql_optimizer.py --db-type postgresql --host localhost --user postgres --password yourpass --database testdbExecute a single query and get immediate analysis:
python sql_optimizer.py --db-type sqlite --database test.db --query "SELECT * FROM users WHERE age > 25"Set a custom threshold (in milliseconds) for slow query detection:
python sql_optimizer.py --db-type sqlite --database test.db --threshold 1000Specify custom log and summary file names:
python sql_optimizer.py --db-type sqlite --database test.db --log-file my_queries.csv --summary-file my_summary.txt================================================================================
SQL QUERY OPTIMIZATION TOOL - Interactive Mode
================================================================================
Enter SQL queries to analyze (type 'exit', 'quit', or press Ctrl+C to stop)
Type 'summary' to generate a summary report
================================================================================
Enter SQL query: SELECT * FROM users WHERE email LIKE '%@gmail.com'
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ Query โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ SELECT * FROM users WHERE email LIKE '%@gmail.com' โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
โฑ๏ธ Execution Time: 752.34ms
๐ Results: 1,247 row(s) returned
๐ก Optimization Suggestions:
โข ๐ SLOW QUERY DETECTED! Execution time: 752.34ms (threshold: 500ms)
โข โ Avoid SELECT *: Specify only the columns you need. This reduces data transfer.
โข โ LIKE with leading wildcard ('%text'): This prevents index usage.
โข ๐ Create indexes on WHERE clause columns: email
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The tool analyzes queries for the following patterns:
-
SELECT * Usage
- Severity: High
- Suggestion: Specify exact columns needed
- Impact: Reduces data transfer and improves performance
-
Missing WHERE Clause
- Severity: Medium
- Suggestion: Add filters to reduce result set
- Impact: Prevents full table scans
-
Multiple JOINs (>3)
- Severity: Medium
- Suggestion: Ensure proper indexes on JOIN columns
- Impact: Prevents cartesian products and improves JOIN efficiency
-
Subqueries
- Severity: Medium
- Suggestion: Consider using JOINs or CTEs
- Impact: Better query plan optimization
-
ORDER BY without LIMIT
- Severity: Medium
- Suggestion: Add LIMIT clause
- Impact: Reduces sorting overhead
-
LIKE with Leading Wildcard
- Severity: High
- Suggestion: Restructure query or use full-text search
- Impact: Allows index usage
-
DISTINCT Usage
- Severity: Low
- Suggestion: Ensure necessity, consider GROUP BY
- Impact: Reduces sorting/deduplication overhead
The tool automatically suggests indexes for:
- Columns used in WHERE clauses
- Columns used in JOIN conditions
- Columns used in ORDER BY clauses
- Default Slow Query Threshold: 500ms
- Customizable: Use
--thresholdflag to set your own threshold - Recommendations:
- <100ms: Excellent
- 100-300ms: Good
- 300-500ms: Acceptable
-
500ms: Needs optimization
CSV file containing all executed queries with:
- Timestamp
- Query Type (SELECT, INSERT, etc.)
- Execution Time
- Slow Query Flag
- Complete Query Text
- Optimization Suggestions
Example:
Timestamp,Query Type,Execution Time (ms),Is Slow,Query,Suggestions
2025-10-15 14:32:10,SELECT,752.34,Yes,"SELECT * FROM users WHERE email LIKE '%@gmail.com'","๐ SLOW QUERY DETECTED! | โ Avoid SELECT * | ๐ Create indexes on WHERE clause columns: email"
Comprehensive summary report with:
- Performance statistics
- Query type breakdown
- Detailed query log
- Recommendations
sql_optimizer.py
โโโ SQLOptimizer (Main Class)
โ โโโ connect() # Database connection management
โ โโโ execute_query() # Query execution with timing
โ โโโ analyze_performance() # Pattern detection and analysis
โ โโโ suggest_optimizations()# Generate recommendations
โ โโโ log_results() # CSV logging
โ โโโ generate_summary_report() # Summary generation
โโโ interactive_mode() # Interactive CLI interface
โโโ main() # CLI argument parsing
_extract_table_names(): Identify tables in query_extract_where_columns(): Extract WHERE clause columns_extract_join_columns(): Extract JOIN condition columns_extract_order_by_columns(): Extract ORDER BY columns_display_rich_results(): Formatted output with Rich_display_plain_results(): Plain text output
Customize default settings:
# Performance thresholds (milliseconds)
SLOW_QUERY_THRESHOLD = 500.0
WARNING_THRESHOLD = 300.0
# Display settings
MAX_ROWS_DISPLAY = 10
USE_RICH_FORMATTING = True
# File settings
LOG_FILE = 'query_log.csv'
SUMMARY_FILE = 'query_summary.txt'from sql_optimizer import SQLOptimizer
# Initialize
optimizer = SQLOptimizer(
db_type='mysql',
db_config={
'host': 'localhost',
'user': 'root',
'password': 'pass',
'database': 'testdb'
},
slow_threshold=500.0
)
# Connect
optimizer.connect()
# Execute query
results, exec_time, error = optimizer.execute_query("SELECT * FROM users")
# Analyze
analysis = optimizer.analyze_performance("SELECT * FROM users", exec_time)
suggestions = optimizer.suggest_optimizations("SELECT * FROM users", analysis)
# Display
optimizer.display_results("SELECT * FROM users", results, exec_time, suggestions)
# Log
optimizer.log_results("SELECT * FROM users", analysis, suggestions)
# Cleanup
optimizer.disconnect()Create a file queries.txt with SQL queries (one per line) and process them:
with open('queries.txt', 'r') as f:
queries = f.readlines()
for query in queries:
results, exec_time, error = optimizer.execute_query(query.strip())
if not error:
analysis = optimizer.analyze_performance(query, exec_time)
suggestions = optimizer.suggest_optimizations(query, analysis)
optimizer.log_results(query, analysis, suggestions)
optimizer.generate_summary_report()See examples/ directory for:
sample_data.sql: Sample database schema and datatest_queries.sql: Example queries to test the tooltest_optimizer.py: Unit tests for the optimizer
# Create sample database
sqlite3 test.db < examples/sample_data.sql
# Run optimizer with sample queries
python sql_optimizer.py --db-type sqlite --database test.dbBeautiful, color-coded output with:
- Bordered panels for queries
- Color-coded execution times (green/red)
- Formatted tables for results
- Emoji icons for visual clarity
Plain text output with:
- Clear section separators
- Structured formatting
- All essential information
Users typically see:
- 35-50% reduction in query execution time after applying suggestions
- 60-70% improvement with proper indexing
- 80%+ improvement by eliminating SELECT * in large tables
Contributions are welcome! Areas for enhancement:
- Additional database support (Oracle, SQL Server)
- Query rewriting capabilities
- EXPLAIN plan analysis
- Real-time monitoring mode
- Web dashboard interface
MIT License - Feel free to use and modify for your projects.
Install required dependencies:
pip install -r requirements.txt- Verify database credentials
- Check database server is running
- Ensure proper network connectivity
- Verify database user permissions
Rich is optional. The tool falls back to plain text if Rich is not installed:
pip install richFor issues, questions, or suggestions:
- Create an issue in the repository
- Check existing documentation
- Review example files in
examples/directory
- EXPLAIN plan integration
- Query rewriting engine
- Real-time monitoring dashboard
- Integration with popular ORMs (SQLAlchemy, Django ORM)
- Machine learning-based optimization suggestions
- Query caching analysis
- Connection pooling recommendations
Happy Optimizing! ๐
Made with โค๏ธ for developers who care about performance.