## Filter: wpd_ai_report_filters_batch_size

Control the batch size used when processing report filter queries. This helps optimize database performance for large datasets.

### Description

When processing report filters, Alpha Insights processes data in batches to avoid memory exhaustion and optimize database performance. This filter allows you to customize the batch size based on your server capabilities and data volume.

#### Location

File: `includes/classes/WPDAI_Report_Filters.php`

Method: `WPDAI_Report_Filters::__construct()`

Line: ~60

### Parameters

Parameter | Type | Description
--- | --- | ---
$batch_size | int | The number of records to process per batch (default: varies by filter type)

#### Return

Type: int

Description: Modified batch size (must be a positive integer)

### Example Usage

### Increase Batch Size for Better Performance

    add_filter( 'wpd_ai_report_filters_batch_size', 'increase_report_batch_size' );
    function increase_report_batch_size( $batch_size ) {
        // Increase to 5000 for high-performance servers
        return 5000;
    }

### Reduce Batch Size for Memory-Constrained Servers

    add_filter( 'wpd_ai_report_filters_batch_size', 'reduce_report_batch_size' );
    function reduce_report_batch_size( $batch_size ) {
        // Reduce to 1000 for servers with limited memory
        return 1000;
    }

### Dynamic Batch Size Based on Data Volume

    add_filter( 'wpd_ai_report_filters_batch_size', 'dynamic_report_batch_size' );
    function dynamic_report_batch_size( $batch_size ) {
        global $wpdb;

        // Get total orders count
        $total_orders = $wpdb->get_var(
            "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'shop_order' AND post_status != 'trash'"
        );

        // Adjust batch size based on total orders
        if ( $total_orders > 100000 ) {
            // Very large dataset: use smaller batches
            return 1000;
        } elseif ( $total_orders > 10000 ) {
            // Large dataset: use medium batches
            return 2500;
        } else {
            // Small dataset: use larger batches
            return 5000;
        }
    }

### Batch Size Based on Available Memory

    add_filter( 'wpd_ai_report_filters_batch_size', 'memory_based_report_batch_size' );
    function memory_based_report_batch_size( $batch_size ) {
        // Get available memory
        $memory_limit = ini_get( 'memory_limit' );
        $memory_limit_bytes = wp_convert_hr_to_bytes( $memory_limit );
        $memory_usage = memory_get_usage( true );
        $available_memory = $memory_limit_bytes - $memory_usage;

        // Adjust batch size based on available memory
        if ( $available_memory < 128 * 1024 * 1024 ) { // Less than 128MB
            return 1000;
        } elseif ( $available_memory < 256 * 1024 * 1024 ) { // Less than 256MB
            return 2500;
        } else {
            return 5000; // More memory available, use larger batches
        }
    }

### Best Practices

- Default values are optimized for most stores
- Monitor memory usage when increasing batch size
- Use smaller batches on shared hosting environments
- Larger batches = fewer queries but more memory usage
- Smaller batches = more queries but less memory usage
- Test with your actual data volume before production

### Performance Impact

Batch Size | Memory Usage | Query Count | Best For
--- | --- | --- | ---
1,000 | Low | High | Shared hosting, low memory, very large datasets
2,500 (typical default) | Medium | Medium | Most standard setups
5,000 | High | Low | Dedicated servers, high memory, medium datasets

### Important Notes

- Batch size must be a positive integer
- Very large batch sizes may cause memory exhaustion
- Very small batch sizes may slow down queries
- Default value is optimized for most use cases
- Only adjust if you're experiencing performance issues
- This affects report filter processing, not analytics data fetching

### Related Filters

- wpd_ai_analytics_data_fetch_batch_size - Control analytics data batch size
- wpd_ai_report_filters_is_transient_enabled - Control report caching
- wpd_ai_report_filters_users_should_sort - Control user sorting in reports

### Related Classes

- `WPDAI_Report_Filters` - Report filtering and processing

### Debugging

    add_filter( 'wpd_ai_report_filters_batch_size', 'debug_report_batch_size', 999 );
    function debug_report_batch_size( $batch_size ) {
        error_log( 'Alpha Insights Report Filters Batch Size: ' . $batch_size );
        error_log( 'Memory Usage: ' . size_format( memory_get_usage( true ) ) );
        return $batch_size;
    }