Repository: ClickHouse/ClickHouse
File: src/Databases/DatabaseOrdinary.cpp (C++)

Code before:

    if (mode == LoadingStrictnessLevel::FORCE_RESTORE)
        return false;

    return true;
}

void DatabaseOrdinary::loadTableLazy(
    ContextMutablePtr local_context,
    const QualifiedTableName & name,
    const ASTPtr & ast,
    LoadingStrictnessLevel mode)
{
    const auto & query = ast->as<const ASTCreateQuery &>();

    LOG_TRACE(log, "Lazy-loading table {}", name.getFullName());

    ColumnsDescription columns;
    if (query.columns_list && query.columns_list->columns)
        columns = InterpreterCreateQuery::getColumnsDescription(
            *query.columns_list->columns, local_context, mode);

    StorageID table_id(name.database, query.getTable(), query.uuid);
    String table_data_path = getTableDataPath(query);

    auto get_nested = [query_str = ast->formatWithSecretsMultiLine(), db_name = name.database, table_data_path, local_context, mode]() -> StoragePtr
    {
        ParserCreateQuery parser;
        ASTPtr parsed_ast = parseQuery(
            parser,
            query_str.data(),
            query_str.data() + query_str.size(),
            "lazy load",
            0,
            local_context->getSettingsRef()[Setting::max_parser_depth],
            local_context->getSettingsRef()[Setting::max_parser_backtracks]);
        const auto & create_query = parsed_ast->as<const ASTCreateQuery &>();
        auto [_, table] = createTableFromAST(
            create_query, db_name, table_data_path, local_context, mode);
        return table;
    };

    auto proxy = std::make_shared<StorageTableProxy>(
        table_id, std::move(get_nested), std::move(columns));

    attachTable(local_context, query.getTable(), proxy, table_data_path);
}

LoadTaskPtr DatabaseOrdinary::loadTableFromMetadataAsync(
    AsyncLoader & async_loader,
    LoadJobSet load_after,

Review comment (performance):
ast_clone = ast->clone(),  instead of query_str for performance?
/// ✅ Direct AST clone (faster than format+parse) 

Code after:

    if (mode == LoadingStrictnessLevel::FORCE_RESTORE)
        return false;

    return true;
}

void DatabaseOrdinary::loadTableLazy(
    ContextMutablePtr local_context,
    const QualifiedTableName & name,
    const ASTPtr & ast,
    LoadingStrictnessLevel mode)
{
    const auto & query = ast->as<const ASTCreateQuery &>();

    LOG_TRACE(log, "Lazy-loading table {}", name.getFullName());

    ColumnsDescription columns;
    if (query.columns_list && query.columns_list->columns)
        columns = InterpreterCreateQuery::getColumnsDescription(
            *query.columns_list->columns, local_context, mode);

    StorageID table_id(name.database, query.getTable(), query.uuid);
    String table_data_path = getTableDataPath(query);

    auto get_nested = [query_str = ast->formatWithSecretsMultiLine(),
                        db_name = name.database,
                        table_data_path,
                        global_context = local_context->getGlobalContext(),
                        mode]() -> StoragePtr
    {
        auto load_context = Context::createCopy(global_context);
        ParserCreateQuery parser;
        ASTPtr parsed_ast = parseQuery(
            parser,
            query_str.data(),
            query_str.data() + query_str.size(),
            "lazy load",
            0,
            load_context->getSettingsRef()[Setting::max_parser_depth],
            load_context->getSettingsRef()[Setting::max_parser_backtracks]);
        const auto & create_query = parsed_ast->as<const ASTCreateQuery &>();
        auto [_, table] = createTableFromAST(
            create_query, db_name, table_data_path, load_context, mode);
        return table;
    };

    auto proxy = std::make_shared<StorageTableProxy>(
        table_id, std::move(get_nested), std::move(columns));

    attachTable(local_context, query.getTable(), proxy, table_data_path);