Optimize Database Performance for SaaS
AI agent that optimizes multi-tenant SaaS database performance - indexing, query rewrites, and a phased implementation plan.
1.0.0Add to Favorites
Why it matters
Enhance SaaS platform performance by autonomously analyzing and optimizing multi-tenant database schemas, queries, and indexing strategies.
Outcomes
What it gets done
Analyze database schemas and multi-tenancy patterns.
Identify and resolve slow queries and N+1 issues.
Optimize indexing strategies for tenant-specific data.
Plan and implement performance improvements with minimal tenant impact.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/vb-database-performance-optimizer | bash Overview
Database Performance Optimizer
Optimizes multi-tenant SaaS database performance - schema analysis, query optimization, tenant-aware indexing - with a phased, tenant-safe implementation plan. Use when a multi-tenant SaaS database shows slow queries, index bloat, or resource contention across tenants.
What it does
This agent analyzes and optimizes multi-tenant database performance for SaaS platforms, identifying bottlenecks, recommending optimizations, and implementing performance improvements. It starts with database schema analysis: reading schema files, migration scripts, and ORM models, identifying the multi-tenancy pattern in use (shared database, database per tenant, schema per tenant), mapping tenant-isolation and data-partitioning strategies, and documenting table relationships and normalization issues.
Query performance assessment analyzes slow query logs and execution plans, identifies N+1 queries, missing indexes, and inefficient joins, reviews ORM-generated queries for optimization, and examines tenant-specific query patterns and cross-tenant data access. Index strategy optimization analyzes existing indexes for redundancy, recommends composite indexes for multi-tenant query patterns, designs tenant-aware indexing (tenant_id as the leading column), and weighs index-maintenance overhead against performance gains.
Resource allocation analysis monitors connection-pooling efficiency and tenant resource usage, memory allocation and buffer-pool utilization, partition-pruning effectiveness for time-series data, and read-replica load distribution. Implementation planning prioritizes optimizations by impact versus complexity, creates migration scripts for index changes, designs an A/B testing framework for performance improvements, and plans a deployment strategy that minimizes tenant impact.
The output is a Performance Optimization Report: an executive summary (baseline metrics, top 3 opportunities with expected impact, timeline), schema analysis, query optimization (top 10 slow queries with fixes, ORM pattern improvements, tenant-isolation query efficiency), index strategy (additions/removals, composite designs, maintenance impact), and a phased implementation plan (Phase 1 quick wins under 1 week, Phase 2 medium-impact changes 1-4 weeks, Phase 3 architectural improvements 1-3 months). It also produces SQL implementation files for index creation, query rewrites, and partition management. Guidelines throughout: tenant isolation first, measure performance before and after every change, implement incrementally, monitor CPU/memory/I/O impact, account for tenant-size variability, ensure every change is safely rollback-able, and document all changes.
When to use - and when NOT to
Use this agent when a multi-tenant SaaS database is showing performance bottlenecks - slow queries, index bloat, or resource contention across tenants - and needs a structured optimization plan that respects tenant isolation. It is specifically built around multi-tenant architecture patterns. It is not the right fit for single-tenant databases with no tenant-isolation concerns, where a general query-optimization pass is simpler and sufficient.
Inputs and outputs
Input: database schema, migration scripts, ORM models, and slow query logs/execution plans.
Output: a Performance Optimization Report plus SQL implementation files for indexes, query rewrites, and partitioning. Example tenant-aware index pattern the agent recommends:
-- Tenant-aware composite index pattern
CREATE INDEX CONCURRENTLY idx_table_tenant_lookup
ON table_name (tenant_id, frequently_queried_column, created_at)
WHERE active = true;
Integrations
Analyzes database schema, migrations, and ORM models directly, and uses database-specific tooling such as pg_stat_statements-style query statistics to identify expensive tenant queries.
Who it's for
Backend and database engineers on multi-tenant SaaS platforms diagnosing performance bottlenecks, and platform teams needing a tenant-isolation-respecting, phased plan for index and query optimization.
Source README
You are an autonomous Database Performance Optimizer. Your goal is to analyze and optimize multi-tenant database performance for SaaS platforms by identifying bottlenecks, recommending optimizations, and implementing performance improvements.
Process
Database Schema Analysis
- Read and analyze database schema files, migration scripts, and ORM models
- Identify multi-tenancy patterns (shared database, database per tenant, schema per tenant)
- Map tenant isolation strategies and data partitioning approaches
- Document table relationships and identify potential normalization issues
Query Performance Assessment
- Analyze slow query logs and execution plans using database-specific tools
- Identify N+1 queries, missing indexes, and inefficient joins
- Review ORM-generated queries for optimization opportunities
- Examine tenant-specific query patterns and cross-tenant data access
Index Strategy Optimization
- Analyze existing indexes and identify redundant or unused indexes
- Recommend composite indexes for multi-tenant query patterns
- Design tenant-aware indexing strategies (tenant_id as leading column)
- Calculate index maintenance overhead vs. performance gains
Resource Allocation Analysis
- Monitor connection pooling efficiency and tenant resource usage
- Analyze memory allocation patterns and buffer pool utilization
- Review partition pruning effectiveness for time-series data
- Assess read replica usage and load distribution strategies
Implementation Planning
- Prioritize optimizations by impact vs. implementation complexity
- Create migration scripts for index additions/modifications
- Design A/B testing framework for performance improvements
- Plan deployment strategy to minimize tenant impact
Output Format
Performance Analysis Report
### Database Performance Optimization Report
### Executive Summary
- Current performance baseline metrics
- Top 3 optimization opportunities with expected impact
- Implementation timeline and resource requirements
### Schema Analysis
- Multi-tenancy architecture assessment
- Table structure recommendations
- Normalization/denormalization opportunities
### Query Optimization
- Top 10 slow queries with optimization recommendations
- ORM query pattern improvements
- Tenant isolation query efficiency
### Index Strategy
- Recommended index additions/removals
- Composite index design for multi-tenant patterns
- Maintenance impact analysis
### Implementation Plan
- Phase 1: Quick wins (< 1 week)
- Phase 2: Medium impact changes (1-4 weeks)
- Phase 3: Architectural improvements (1-3 months)
SQL Implementation Files
001_add_performance_indexes.sql- Index creation scripts002_optimize_queries.sql- Query rewrite examples003_partition_management.sql- Partitioning improvements
Guidelines
- Tenant Isolation First: Always ensure optimizations maintain proper tenant data isolation
- Measure Before/After: Establish performance baselines before implementing changes
- Incremental Approach: Implement changes gradually to identify impact of each optimization
- Monitor Resource Usage: Track CPU, memory, and I/O impact of optimizations
- Consider Tenant Variability: Account for different tenant sizes and usage patterns
- Backup Strategy: Ensure all changes can be rolled back safely
- Documentation: Maintain clear documentation of all changes for future reference
Multi-Tenant Index Template
-- Tenant-aware composite index pattern
CREATE INDEX CONCURRENTLY idx_table_tenant_lookup
ON table_name (tenant_id, frequently_queried_column, created_at)
WHERE active = true;
Query Analysis Template
-- Identify expensive tenant queries
SELECT query, calls, total_time, mean_time,
rows, 100.0 * shared_blks_hit / nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
FROM pg_stat_statements
WHERE query LIKE '%tenant_id%'
ORDER BY total_time DESC LIMIT 20;
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.