## Filter: wpd_ai_session_data_chunk_size

Control the chunk size used when querying session data in batches. This helps optimize database queries for large datasets by processing session IDs in smaller chunks.

### Description

When fetching session data for analytics reports, Alpha Insights processes session IDs in chunks to avoid memory issues and optimize database performance. This filter allows you to customize the chunk size based on your server capabilities and data volume.

#### Location

File: `includes/classes/WPDAI_Data_Warehouse.php`

Method: `WPDAI_Data_Warehouse::get_session_data_by_ids()`

Line: ~6222

### Parameters

Parameter | Type | Description
--- | --- | ---
$chunk_size | int | The number of session IDs to process per chunk (default: 100-200 based on session ID length)

#### Return

Type: int

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

### Default Behavior

The plugin automatically determines chunk size based on average session ID length:

- Very long session IDs (>60 chars): 100 per chunk
- Medium-long session IDs (40-60 chars): 150 per chunk
- Standard session IDs (<40 chars): 200 per chunk

### Example Usage

### Increase Chunk Size for Better Performance

    add_filter( 'wpd_ai_session_data_chunk_size', 'increase_session_chunk_size' );
    function increase_session_chunk_size( $chunk_size ) {
        // Increase to 500 for high-performance servers
        return 500;
    }

### Reduce Chunk Size for Memory-Constrained Servers

    add_filter( 'wpd_ai_session_data_chunk_size', 'reduce_session_chunk_size' );
    function reduce_session_chunk_size( $chunk_size ) {
        // Reduce to 50 for servers with limited memory
        return 50;
    }

### Dynamic Chunk Size Based on Server Load

    add_filter( 'wpd_ai_session_data_chunk_size', 'dynamic_session_chunk_size' );
    function dynamic_session_chunk_size( $chunk_size ) {
        // Get server load average (Unix/Linux only)
        if ( function_exists( 'sys_getloadavg' ) ) {
            $load = sys_getloadavg();
            $current_load = $load[0];

            // Reduce chunk size if server is under heavy load
            if ( $current_load > 2.0 ) {
                return max( 50, (int) ( $chunk_size * 0.5 ) );
            }
        }

        return $chunk_size;
    }

### Chunk Size Based on Available Memory

    add_filter( 'wpd_ai_session_data_chunk_size', 'memory_based_chunk_size' );
    function memory_based_chunk_size( $chunk_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 chunk size based on available memory
        if ( $available_memory < 64 * 1024 * 1024 ) { // Less than 64MB
            return 50;
        } elseif ( $available_memory < 128 * 1024 * 1024 ) { // Less than 128MB
            return 100;
        } else {
            return 300; // More memory available, use larger chunks
        }
    }

### Best Practices

- Start with default values and adjust based on performance
- Monitor memory usage when increasing chunk size
- Use smaller chunks on shared hosting environments
- Larger chunks = fewer queries but more memory usage
- Smaller chunks = more queries but less memory usage
- Test with your actual data volume before production

### Performance Impact

Chunk Size | Memory Usage | Query Count | Best For
--- | --- | --- | ---
50 | Low | High | Shared hosting, low memory
100-200 (default) | Medium | Medium | Most standard setups
300-500 | High | Low | Dedicated servers, high memory

### Important Notes

- Chunk size must be a positive integer
- Very large chunk sizes may cause memory exhaustion
- Very small chunk sizes may slow down queries
- Default values are optimized for most use cases
- Only adjust if you're experiencing performance issues

### Debugging

    add_filter( 'wpd_ai_session_data_chunk_size', 'debug_session_chunk_size', 999 );
    function debug_session_chunk_size( $chunk_size ) {
        error_log( 'Alpha Insights Session Chunk Size: ' . $chunk_size );
        error_log( 'Memory Usage: ' . size_format( memory_get_usage( true ) ) );
        return $chunk_size;
    }

### Related Filters

- wpd_ai_analytics_data_fetch_batch_size - Control analytics data batch size

### Related Classes

- `WPDAI_Data_Warehouse` - React dashboard data warehouse