get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE ts < %s', $hits, $cutoff ) ) + (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE day < %s', $crawlers, gmdate( 'Y-m-d', time() - self::retention_days() * DAY_IN_SECONDS ) ) ); } $total = 0; $batches = 0; do { $deleted = (int) $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE ts < %s LIMIT 10000', $hits, $cutoff ) ); $total += $deleted; } while ( $deleted > 0 && ++$batches < 30 ); // A huge backlog finishes across daily runs, not one tick. // The crawler rollup ages out with the hits (Phase 18) — at one row per // bot per day it never needs batching. $total += Saddle_Analytics::crawlers()->prune( self::retention_days() ); return $total; } }