This is a standalone tool for ad-hoc query optimization. If you’re running a PostgreSQL or MySQL service on Aiven, the platform automatically suggests optimizations for slow queries through the AI Insights feature.
Use cases
Pre-deployment optimization
Optimize queries before deploying to production
Performance troubleshooting
Identify bottlenecks in slow-running queries
Index recommendations
Get AI-powered suggestions for optimal indexes
Query learning
Understand how to write more efficient SQL
How it works
The query optimizer analyzes your SQL query structure, table schemas, and statistics to provide:- Optimized query: Rewritten query with better performance characteristics
- Index recommendations: Suggested indexes to improve query execution
- Execution analysis: Explanation of potential performance issues
- Best practices: General SQL optimization advice
Getting started
Access the optimizer
Navigate to the Aiven Console and click Tools > SQL query optimizer
Select database type
Choose your database type and version:
- PostgreSQL (versions 12-16)
- MySQL (versions 8.0+)
Add table information (optional)
For better recommendations, provide your table structure and statistics:Paste the output into the Query output field
- PostgreSQL
- MySQL
Run this query on your database:
Example: Optimizing a PostgreSQL query
Original query
Optimization recommendations
The optimizer might suggest:Optimized query
Optimized query
- Table aliases for cleaner syntax
- Reordered WHERE conditions (most selective first)
- Explicit INNER JOIN for clarity
Index recommendations
Index recommendations
Analysis
Analysis
Potential issues identified:
- Missing indexes: The query performs sequential scans on large tables
- Join order: Could benefit from pushing down the country filter
- Sort operation: ORDER BY on unindexed column requires expensive sort
Example: Optimizing a MySQL query
Original query
Optimization recommendations
Optimized query
Optimized query
- COUNT(oi.id) instead of COUNT(*) for accuracy with LEFT JOIN
- COALESCE for NULL handling
- Simplified GROUP BY (only p.id needed with ONLY_FULL_GROUP_BY)
Index recommendations
Index recommendations
Understanding the optimization report
The optimization report includes several sections:Query improvements
Suggested changes to your SQL query:- Syntax optimization: More efficient SQL constructs
- Join order: Optimal table join sequence
- Predicate pushdown: Moving filters earlier in execution
- Subquery elimination: Converting subqueries to joins
Index recommendations
Proposed indexes to speed up query execution:- Single-column indexes: For simple equality checks
- Composite indexes: For multiple column filters
- Covering indexes: Include additional columns to avoid table lookups
- Partial indexes: For queries with constant WHERE conditions
Performance analysis
- Bottleneck identification: Where the query spends most time
- Cardinality estimates: Expected number of rows at each step
- Cost analysis: Relative cost of different operations
- Alternative approaches: Different ways to achieve the same result
Best practices for query optimization
Provide table schema
Provide table schema
The more information you provide, the better the recommendations:
- Table structures (column names and types)
- Row counts
- Data distribution statistics
- Existing indexes
Start with problematic queries
Start with problematic queries
Focus on queries that:
- Take longer than 1 second to execute
- Run frequently (hundreds or thousands of times per minute)
- Scan large tables
- Use complex joins or subqueries
Test recommendations
Test recommendations
Always test optimizations in a non-production environment:
- Apply suggested indexes
- Run EXPLAIN ANALYZE to compare execution plans
- Measure actual performance improvements
- Monitor resource usage
Consider trade-offs
Consider trade-offs
Some optimizations have costs:
- Indexes: Speed up reads but slow down writes
- Denormalization: Faster queries but more storage
- Materialized views: Quick access but stale data
Limitations
AI Database Optimizer for services
If you’re running Aiven for PostgreSQL or MySQL, you get automatic optimization suggestions:- PostgreSQL
- MySQL
The AI Database Optimizer for PostgreSQL automatically:
- Monitors slow queries
- Analyzes execution plans
- Suggests index improvements
- Provides optimization recommendations
Frequently asked questions
Is my query data sent to third parties?
Is my query data sent to third parties?
Query text and structure is analyzed by AI systems, but:
- Actual data values are not required or stored
- Query analysis happens in secure, isolated environments
- No query content is shared with external parties
- See Aiven’s privacy policy for details
Can I use this for other databases?
Can I use this for other databases?
Currently, the standalone optimizer supports:
- PostgreSQL (versions 12, 13, 14, 15, 16)
- MySQL (versions 8.0, 8.1, 8.2)
How accurate are the recommendations?
How accurate are the recommendations?
Accuracy depends on:
- Query complexity
- Quality of provided statistics
- Database version and configuration
- Data distribution patterns
Can I optimize stored procedures?
Can I optimize stored procedures?
Yes, paste the SQL from your stored procedure. However:
- Procedural logic (IF/WHILE statements) isn’t optimized
- Focus on individual SELECT/UPDATE/DELETE statements
- Consider the procedure’s overall structure separately