UNPKG

bc-code-intelligence-mcp

Version:

BC Code Intelligence MCP Server - Complete Specialist Bundle with AI-driven expert consultation, seamless handoffs, and context-preserving workflows

66 lines (38 loc) 5.19 kB
--- title: "Table Event Performance in Batch Operations" domain: "dean-debug" difficulty: "intermediate" bc_versions: "14+" tags: ["table-events", "batch-operations", "performance", "optimization"] prerequisites: ["table-events-basics", "batch-processing"] samples: "samples/table-event-batching.md" --- # Table Event Performance in Batch Operations ## Overview Table events (OnInsert, OnModify, OnDelete) trigger for every record operation, creating significant performance implications during batch processing scenarios. Understanding event firing patterns and optimization strategies is crucial for maintaining system performance during high-volume operations. Batch operations that process thousands of records can generate corresponding thousands of event invocations, each potentially executing complex subscriber logic. This multiplication effect can transform efficient batch operations into performance bottlenecks. Event performance impact varies dramatically based on subscriber complexity, event frequency, and batch size. Proper optimization requires understanding both the business requirements for event processing and the performance characteristics of subscriber implementations. ## Event Firing Patterns Table events fire synchronously for each record operation, creating a direct correlation between batch size and total event processing time. A batch inserting 1000 records triggers 1000 OnInsert events, each executing all registered subscribers. Event firing cannot be disabled for individual operations, meaning all batch operations inherit the full event processing overhead. This design ensures data consistency and business rule enforcement but requires careful performance consideration. Nested batch operations compound event firing, where batch operations triggering additional record operations create cascading event execution. Understanding operation nesting prevents unexpected performance degradation. ## Performance Impact Analysis Subscriber execution time directly multiplies with batch operation size, making even lightweight subscribers significant performance factors in large batches. A 10ms subscriber becomes a 10-second overhead for 1000-record batches. Complex subscribers performing database queries, external service calls, or heavy calculations create substantial batch processing delays. The cumulative effect of multiple subscribers amplifies performance impact exponentially. Memory allocation within subscribers accumulates across batch operations, potentially causing memory pressure during large batch processing. Subscriber memory patterns must consider batch operation contexts. ## Optimization Strategies Implement conditional logic within subscribers to minimize processing for batch operation contexts. Use system flags or operation context to detect batch scenarios and apply streamlined processing logic. Defer expensive operations within subscribers when batch processing is detected. Accumulate changes for bulk processing after batch completion rather than processing each record individually. Consider subscriber architecture patterns that aggregate operations across multiple event invocations, reducing per-event processing overhead through batching within the subscriber logic itself. ## Batch Detection Techniques System context information can indicate batch operation scenarios, allowing subscribers to adapt their processing logic accordingly. Session information and operation patterns provide batch detection signals. Operation timing patterns help identify batch scenarios where multiple rapid-fire events suggest bulk processing rather than individual user operations. Time-based detection enables adaptive subscriber behavior. Custom batch identification mechanisms allow application code to signal batch operations to subscribers, providing explicit control over batch-optimized processing paths. ## Memory Management Batch operations with table events can create significant memory allocation patterns as subscribers process large numbers of events consecutively. Memory allocation strategies must account for cumulative subscriber memory usage. Global variables in subscribers accumulate memory across batch operations unless explicitly managed. Batch-aware cleanup strategies prevent memory accumulation during extended batch processing. Temporary objects created within subscribers should be explicitly disposed when possible to prevent memory pressure during large batch operations. ## Transaction Considerations Table events execute within the same transaction as the triggering operation, meaning subscriber failures can rollback entire batch operations. Error handling strategies must balance data integrity with batch operation success. Long-running subscribers can extend transaction duration for batch operations, potentially causing locking issues or transaction timeout failures. Subscriber performance directly affects batch operation transaction management. Nested transaction scenarios in subscribers require careful consideration during batch operations to prevent deadlocks or transaction conflicts.