if ($pgdb) {
        $sql = "BEGIN;\nDELETE FROM sq_ast_lnk_tree;";
    } else {
        $sql = "DELETE FROM sq_ast_lnk_tree;";
    }
} else {
    if ($pgdb) {
        $sql = 'TRUNCATE sq_ast_lnk_tree;';
    } else {
        $sql = 'TRUNCATE TABLE sq_ast_lnk_tree;';
    }
}
echo $sql, "\n";
// Work out how many significant links we actually have
$sql = 'SELECT COUNT(*) FROM sq_ast_lnk l WHERE	' . db_extras_bitand(MatrixDAL::getDbType(), 'l.link_type', MatrixDAL::quote(SQ_SC_LINK_SIGNIFICANT)) . ' > 0';
$num_links = MatrixDAL::executeSqlOne($sql);
echo_headline('ANALYSING ' . $num_links . ' SIGNIFICANT LINKS');
// Because DAL doesn't support row-by-row fetch, we'll do chunked fetch instead,
// 2000 at a time. If we don't get that many results, we'll do one last query
// in case anything had been added since - if we get zero results, we are done
$base_sql = 'SELECT l.majorid, l.linkid, l.minorid
		FROM
			sq_ast_lnk l
		WHERE
			' . db_extras_bitand(MatrixDAL::getDbType(), 'l.link_type', MatrixDAL::quote(SQ_SC_LINK_SIGNIFICANT)) . ' > 0
		ORDER BY
			l.sort_order, l.linkid, l.majorid, l.minorid';
$offset = 0;
$chunk_size = 2000;
$echo_i = 0;
$index = array();