Performance Optimization

This guide covers techniques for optimizing CQL performance in your applications.

Quick Start

Development (Auto-Enabled)

# Performance monitoring is automatically enabled in development
CQL.configure do |config|
  config.db = "postgresql://localhost/myapp"
end

# You get:
# - Query profiling
# - N+1 detection
# - Slow query analysis
# - Performance reports every 5 minutes

Production

CQL.configure do |config|
  config.db = ENV["DATABASE_URL"]
  config.env = "production"
  config.monitor_performance = true  # Opt-in for production
  config.performance_auto_report = false  # No auto-reports
end

Query Optimization

1. Use Indexes

2. Avoid N+1 Queries

3. Use Query Scopes

4. Batch Operations

Connection Pool Optimization

Development

Production

Caching Strategies

1. Query Result Caching

2. Fragment Caching

3. Request-Scoped Caching

Performance Monitoring

Reading Performance Reports

Every 5 minutes in development, you'll see:

Custom Report Intervals

Manual Performance Analysis

Query Plan Analysis

PostgreSQL

Disable Plan Analysis

Database-Specific Optimizations

PostgreSQL

MySQL

SQLite

Best Practices

  1. Use Development Auto-Monitoring: Let CQL show you performance issues automatically

  2. Fix N+1 Queries: Use eager loading with includes

  3. Add Indexes: On foreign keys and frequently queried columns

  4. Cache Strategically: Cache expensive queries and calculations

  5. Monitor Production: Enable performance monitoring selectively

  6. Batch Operations: Use insert_many and update_many

  7. Use Scopes: Define reusable query patterns

Common Performance Issues

Slow Queries

Memory Usage

Connection Pool Exhaustion

SQL Logging

Beautiful SQL logging is automatically enabled in development environments to help you debug and optimize your queries.

Zero Configuration (Development)

Configuration Options

Output Examples

Standard Query

Slow Query

Error Query

Performance Indicators

The formatter uses visual indicators for query performance:

  • Fast (< 50ms) - Green checkmark

  • Slow (50-1000ms) - Yellow warning

  • Very Slow (> 1000ms) - Slow indicator

  • Error - Red X

Production Usage

For production, SQL logging should typically be disabled:

If you need SQL logging in production:

Disable Auto-Logging

Debugging Performance

Enable All Features

Disable Auto-Features

Last updated

Was this helpful?