Control the batch size used when fetching analytics data from the database. This helps optimize query performance for large datasets.
When fetching analytics data for reports, Alpha Insights processes records 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.
File: includes/classes/WPDAI_Data_Warehouse.php
Method: WPDAI_Data_Warehouse::get_analytics_data()
Line: ~6611
| Parameter | Type | Description |
|---|---|---|
| $batch_size | int | The number of records to fetch per batch (default: 10000) |
Type: int
Description: Modified batch size (must be a positive integer)
add_filter( 'wpd_ai_analytics_data_fetch_batch_size', 'increase_analytics_batch_size' );
function increase_analytics_batch_size( $batch_size ) {
// Increase to 25000 for high-performance servers
return 25000;
}
add_filter( 'wpd_ai_analytics_data_fetch_batch_size', 'reduce_analytics_batch_size' );
function reduce_analytics_batch_size( $batch_size ) {
// Reduce to 5000 for servers with limited memory
return 5000;
}
add_filter( 'wpd_ai_analytics_data_fetch_batch_size', 'dynamic_analytics_batch_size' );
function dynamic_analytics_batch_size( $batch_size ) {
global $wpdb;
// Get total analytics records count
$total_records = $wpdb->get_var(
"SELECT COUNT(*) FROM {$wpdb->prefix}wpd_ai_events"
);
// Adjust batch size based on total records
if ( $total_records > 1000000 ) {
// Very large dataset: use smaller batches
return 5000;
} elseif ( $total_records > 100000 ) {
// Large dataset: use medium batches
return 10000;
} else {
// Small dataset: use larger batches
return 20000;
}
}
add_filter( 'wpd_ai_analytics_data_fetch_batch_size', 'memory_based_analytics_batch_size' );
function memory_based_analytics_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 5000;
} elseif ( $available_memory < 256 * 1024 * 1024 ) { // Less than 256MB
return 10000;
} else {
return 20000; // More memory available, use larger batches
}
}
| Batch Size | Memory Usage | Query Count | Best For |
|---|---|---|---|
| 5,000 | Low | High | Shared hosting, low memory, very large datasets |
| 10,000 (default) | Medium | Medium | Most standard setups |
| 20,000-25,000 | High | Low | Dedicated servers, high memory, medium datasets |
add_filter( 'wpd_ai_analytics_data_fetch_batch_size', 'debug_analytics_batch_size', 999 );
function debug_analytics_batch_size( $batch_size ) {
error_log( 'Alpha Insights Analytics Batch Size: ' . $batch_size );
error_log( 'Memory Usage: ' . size_format( memory_get_usage( true ) ) );
return $batch_size;
}
WPDAI_Data_Warehouse - React dashboard data warehouse