A production-ready demonstration of autonomous database optimization through hierarchical machine learning, featuring three distinct levels of self-improvement without manual intervention.
This system implements a novel three-tier architecture for autonomous database query optimization:
- Level 0 (Operational): Deep reinforcement learning agent that executes and optimizes individual queries in real-time
- Level 1 (Tactical): Policy learner that analyzes execution telemetry and continuously refines operational strategies
- Level 2 (Strategic): Meta-learner that optimizes the learning process itself through evolutionary algorithms
Unlike traditional rule-based query optimizers, this system learns from experience, adapts to workload patterns, and improves its own learning mechanisms autonomously.
- Autonomous Learning: Zero-configuration optimization that improves without human intervention
- Multi-Level Architecture: Hierarchical learning from query execution to meta-optimization
- Safety Mechanisms: Comprehensive monitoring with automatic rollback capabilities
- Real-Time Dashboard: Web-based visualization of performance metrics and learning progress
- Production-Ready: Built-in telemetry, logging, and state management
- Extensive Telemetry: Detailed tracking of all system decisions and performance metrics
┌─────────────────────────────────────────────────────────────┐
│ Level 2: Strategic Meta-Learning │
│ • Optimizes learning hyperparameters │
│ • Evolutionary algorithm for architecture search │
│ • Adapts to changing workload characteristics │
└──────────────────────────┬──────────────────────────────────┘
│
┌──────────────────────────▼──────────────────────────────────┐
│ Level 1: Tactical Policy Learning │
│ • Analyzes execution patterns │
│ • Updates operational policies │
│ • Validates changes before deployment │
└──────────────────────────┬──────────────────────────────────┘
│
┌──────────────────────────▼──────────────────────────────────┐
│ Level 0: Operational Query Execution │
│ • Deep Q-Network for query optimization │
│ • Real-time execution decisions │
│ • Experience replay and target networks │
└─────────────────────────────────────────────────────────────┘
- OS: Windows 10/11 (64-bit), Linux, or macOS
- RAM: 16 GB minimum (32 GB recommended)
- Storage: 50 GB available space
- CPU: 4+ cores recommended
- Network: Internet connection for initial setup
- Python 3.10 or later
- PostgreSQL 14 or later
- Git
Windows:
# Download from https://www.postgresql.org/download/windows/
# Run installer and note your postgres password
# Add to PATH
setx PATH "%PATH%;C:\Program Files\PostgreSQL\14\bin"Linux (Ubuntu/Debian):
sudo apt update
sudo apt install postgresql-14 postgresql-contrib
sudo systemctl start postgresqlmacOS:
brew install postgresql@14
brew services start postgresql@14# Clone repository
git clone https://github.com/alessoh/self-improving-db-optimizer.git
cd self-improving-db-optimizer
# Create virtual environment
python -m venv venv
# Activate virtual environment
# Windows:
venv\Scripts\activate
# Linux/macOS:
source venv/bin/activate
# Install dependencies
pip install --upgrade pip
pip install -r requirements.txt# Copy example configuration
cp config.yaml.example config.yaml
# Edit config.yaml and set your PostgreSQL password
# Replace "CHANGE_ME" with your actual password# This creates tables and loads ~10M rows of synthetic data
# Expected time: 10-15 minutes
python setup_database.py
# Verify setup
python setup_database.py --verifyQuick Test (5 minutes):
python run_demo.py --duration 0.1 --fast-modeFull Simulation (2 weeks compressed):
python run_demo.py --duration 14Custom Configuration:
python run_demo.py --duration 7 --queries 500 --workload analyticalOpen a new terminal window:
# Activate environment
venv\Scripts\activate # Windows
source venv/bin/activate # Linux/macOS
# Start dashboard
python dashboard.pyAccess at: http://localhost:5000
self-improving-db-optimizer/
│
├── config.yaml # System configuration
├── requirements.txt # Python dependencies
├── LICENSE # MIT License
├── README.md # This file
│
├── main.py # System orchestrator
├── setup_database.py # Database initialization
├── run_demo.py # Demonstration runner
├── dashboard.py # Web dashboard server
│
├── core/ # Core learning components
│ ├── query_optimizer.py # Level 0: RL agent
│ ├── policy_learner.py # Level 1: Policy optimization
│ ├── meta_learner.py # Level 2: Meta-learning
│ ├── models.py # Neural network architectures
│ └── safety_monitor.py # Safety & monitoring
│
├── database/ # Database management
│ ├── database_manager.py # Connection & query execution
│ ├── workload_generator.py # Synthetic workload
│ └── schema.sql # Database schema
│
├── telemetry/ # Metrics & monitoring
│ ├── collector.py # Real-time collection
│ └── storage.py # Persistent storage
│
├── dashboard/ # Web interface
│ ├── templates/
│ │ └── index.html # Dashboard UI
│ └── static/
│ ├── style.css # Styling
│ └── app.js # Frontend logic
│
├── utils/ # Utilities
│ ├── logger.py # Logging configuration
│ └── metrics.py # Performance calculations
│
└── data/ # Generated data (created on setup)
├── telemetry.db # SQLite telemetry database
├── policies/ # Saved policy checkpoints
└── logs/ # System logs
The config.yaml file controls all system parameters:
database:
host: localhost
port: 5432
user: postgres
password: YOUR_PASSWORD
dbname: query_optimizer_demolevel0: # Reinforcement Learning
learning_rate: 0.0003
gamma: 0.99
batch_size: 64
buffer_size: 10000
level1: # Policy Learning
enabled: true
update_interval: 3600
min_improvement: 0.02
level2: # Meta-Learning
enabled: true
evaluation_interval: 86400
population_size: 5safety:
enabled: true
query_timeout: 30
memory_limit_gb: 2
rollback_threshold: 0.15# Run full 2-week simulation
python run_demo.py --duration 14# Quick 1-hour test with 100x time acceleration
python run_demo.py --duration 0.04 --fast-mode# 7-day analytical workload with 500 queries/hour
python run_demo.py --duration 7 --queries 500 --workload analyticalfrom main import SystemOrchestrator
# Initialize system
orchestrator = SystemOrchestrator('config.yaml')
orchestrator.initialize_components()
# Run for specified duration
orchestrator.start(duration_days=14, fast_mode=False)# View telemetry summary
python -c "from telemetry.storage import TelemetryStorage; \
ts = TelemetryStorage({'paths': {'telemetry_db': 'data/telemetry.db'}}); \
ts.print_summary()"The dashboard provides real-time visualization of:
- Query latency trends
- Learning progress metrics
- Phase-by-phase comparison
- Latency distribution percentiles
- Policy update history
- Safety monitor status
data/logs/optimizer.log- Detailed system logsdata/logs/errors.log- Error trackingdata/final_report.txt- Performance summary
# Verify PostgreSQL is running
psql --version
pg_isready
# Test connection
psql -h localhost -U postgres -d postgres# Windows: Install Visual C++ Build Tools
# Download from: https://visualstudio.microsoft.com/visual-cpp-build-tools/
# Upgrade pip and setuptools
pip install --upgrade pip setuptools wheel
# Install PyTorch for Windows
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118- Reduce
workload.queries_per_hourinconfig.yaml - Decrease
level0.buffer_sizefor smaller memory footprint - Lower
database.max_connections
- Reduce scale factor:
python setup_database.py --scale 0.5 - Check available disk space
- Verify PostgreSQL settings allow sufficient memory
# Install development dependencies
pip install pytest pytest-cov pytest-mock
# Run test suite
pytest tests/ -v
# With coverage
pytest tests/ --cov=. --cov-report=html# Install formatters
pip install black isort flake8
# Format code
black .
isort .
# Check style
flake8 .- Algorithm: Deep Q-Network (DQN) with experience replay
- State Space: Database metrics, cache statistics, query characteristics
- Action Space: Query execution strategies (index usage, join methods, parallelization)
- Reward Function: Weighted combination of latency, resource usage, and success rate
- Method: Gradient-based policy optimization
- Update Frequency: Configurable (default: hourly)
- Validation: Statistical testing before deployment
- Rollback: Automatic reversion on performance degradation
- Algorithm: Genetic algorithm for hyperparameter optimization
- Population: Multiple hyperparameter configurations
- Fitness: Multi-objective optimization (speed, stability, resource efficiency)
- Evolution: Selection, crossover, mutation with elitism
- Resource Monitoring: CPU, memory, and connection limits
- Performance Tracking: Baseline comparison with automatic rollback
- Query Timeout: Prevents runaway queries
- Validation Sandbox: Tests changes before production deployment
Contributions are welcome! Please follow these guidelines:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Please ensure:
- Code follows PEP 8 style guidelines
- All tests pass
- New features include tests
- Documentation is updated
If you use this work in your research, please cite:
@software{self_improving_db_optimizer,
author = {Alesso, Harry Peter},
title = {Self-Improving Database Query Optimizer},
year = {2025},
url = {https://github.com/alessoh/self-improving-db-optimizer}
}This project is licensed under the MIT License - see the LICENSE file for details.
- PostgreSQL community for the excellent database system
- PyTorch team for the deep learning framework
- Research in reinforcement learning and meta-learning that inspired this architecture
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Author: Harry Peter Alesso
- Multi-database support (MySQL, SQLite)
- Distributed training capabilities
- Advanced visualization tools
- Cloud deployment templates
- REST API for external integration
- Real-world workload adapters
Note: This is a research demonstration system. For production use, conduct thorough testing and validation in your specific environment.