Your cron task is quite confusing and introducing unncessary overhead.
foreach ( \IPS\Db::i()->select( '*', 'contenthistory_visits' ) as $row )
\IPS\Db::i()->delete( 'contenthistory_visits', array( 'ch_date<?', \IPS\DateTime::create()->sub( new \DateInterval( 'P' . \IPS\Settings::i()->ch_prune_logs_days . 'D' ) )->getTimestamp() ) );
What's the point of the foreach loop? There's really no need to iterate over all the rows in contenthistory_visits, you can just run the query which you have inside the loop.