Оптимизация базы из под крона

0 2 * * * /usr/bin/php -f /home/bitrix/www/local/php_interface/optimaze.php

$_SERVER['DOCUMENT_ROOT'] = __DIR__ .'/../..';
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];

define('STOP_STATISTICS', true);
define('NO_KEEP_STATISTIC', true);
define('NOT_CHECK_PERMISSIONS', true);
define('BX_NO_ACCELERATOR_RESET', true);
define('PERFMON_STOP', true);

@set_time_limit(0);
@ignore_user_abort(true);

require $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/prolog_before.php';

@mb_http_output('utf-8');
@mb_internal_encoding('utf-8');
@mb_regex_encoding('utf-8');

$DBDebug = true;

function optimaze($filter)
{
    global $DB;
    $res = $DB->Query('SHOW TABLES LIKE '$filter%'');
    if ($row = $res->Fetch())
    {
        $keys = array_keys($row);
        $col = $keys[0];
        do
        {
            $tbl = $row[$col];
            $DB->Query('CHECK TABLE `$tbl`');
            $DB->Query('OPTIMIZE TABLE `$tbl`');
        } while($row = $res->Fetch());
    }
}

optimaze('b_');
\Bitrix\Main\Config\Option::set('main', 'LAST_DB_OPTIMIZATION_TIME', time());