A T-Bench task for analyzing slow SQL queries and providing optimization recommendations.
This task tests an AI agent's ability to:
- Analyze SQL queries for performance issues
- Identify missing indexes, inefficient JOINs, and full table scans
- Generate specific optimization recommendations
- Create structured JSON reports
- Use Case: Perf Optimization
- Domain: data_science
- Languages: SQL, Python
- Difficulty: Medium
Given 6 slow SQL queries from an IoT sensor monitoring database, the agent must analyze each query and generate an optimization report identifying:
- Missing indexes - Columns used in WHERE, JOIN, or ORDER BY without indexes
- Inefficient JOINs - SELECT * with JOINs, cartesian products
- Full table scans - Functions on indexed columns, leading wildcards in LIKE
- Query structure issues - Suboptimal patterns that hurt performance
The task provides:
queries/schema.sql- Database schema (IoT: sensors, readings, alerts, maintenance logs)queries/slow_queries.sql- 6 slow queries labeled Q1-Q6queries/sample_data.sql- Sample data for testing
Agent must create optimization_report.json with:
{
"queries": [
{
"query_id": "Q1",
"issues": [...],
"optimized_query": "SELECT ...",
"expected_improvement": "..."
}
],
"summary": {...}
}- Q1: Sensor lookup by location_zone (missing index on sensors.location_zone)
- Q2: Temperature readings by device type with SELECT * and JOIN
- Q3: Alerts in date range (missing index on alert_history.triggered_at)
- Q4: Sensor readings with alert history and maintenance logs (3 JOINs with SELECT *)
- Q5: Sensor search with LOWER() function (prevents index usage)
- Q6: Sensor readings with ORDER BY (missing composite index)
Tests verify:
- All 6 queries are analyzed
- Each query has identified issues with proper severity (high/medium/low)
- Recommendations include specific SQL (e.g., CREATE INDEX statements)
- optimized_query and expected_improvement fields are present
- High-severity issues are correctly identified (location_zone lookup, date ranges)
# Agent should create an analysis script (analyze.py, analyze.sh, etc.)
# Or implement solution in solve.sh
# The test runner will execute the agent's code and validate outputA Python-based analyzer is provided in solution/solve.sh that demonstrates:
- Parsing SQL files to extract queries
- Pattern matching to identify performance issues
- Generating structured JSON reports
This task fills important taxonomy gaps:
- data_science domain - Underrepresented vs web_frontend
- SQL language - Critical for backend/data engineering
- Perf Optimization - Practical real-world skill for production systems
- Database expertise - Essential for full-stack developers
Unlike algorithm puzzles, this mirrors real production debugging work that engineers do daily.