<?

es normal? que la consulta query en procesarPublicaciones dure más que la de obtenerDatosFeed($userId)? una dura 0.7 seg (procesarPublicaciones) y la otra 0.6 (obtenerDatosFeed), y yo siento que la de obtenerDatosFeed es mas compleja

function publicaciones($args = [], $is_ajax = false, $paged = 1)
{
    $user_id = obtenerUserId($is_ajax);
    $current_user_id = get_current_user_id();

    $defaults = [
        'filtro' => '',
        'tab_id' => '',
        'posts' => 12,
        'exclude' => [],
        'post_type' => 'social_post',
        'similar_to' => null,
    ];
    $args = array_merge($defaults, $args);
    $query_args = configuracionQueryArgs($args, $paged, $user_id, $current_user_id);
    $output = procesarPublicaciones($query_args, $args, $is_ajax);

    if ($is_ajax) {
        echo $output;
        die();
    } else {
        return $output;
    }
}

function configuracionQueryArgs($args, $paged, $user_id, $current_user_id)
{
    global $FALLBACK_USER_ID;
    if (!isset($FALLBACK_USER_ID)) {
        $FALLBACK_USER_ID = 44;
    }

    $is_authenticated = $current_user_id && $current_user_id != 0;
    $is_admin = current_user_can('administrator');
    if (!$is_authenticated) {
        $current_user_id = $FALLBACK_USER_ID;
    }

    $identifier = $_POST['identifier'] ?? '';
    $posts = $args['posts'];
    $similar_to = $args['similar_to'] ?? null;
    $filtroTiempo = (int)get_user_meta($current_user_id, 'filtroTiempo', true);

    $query_args = construirQueryArgs($args, $paged, $current_user_id, $identifier, $is_admin, $posts, $filtroTiempo, $similar_to);
    $query_args = aplicarFiltrosUsuario($query_args, $current_user_id);
    $query_args = aplicarFiltroGlobal($query_args, $args, $current_user_id);
    return $query_args;
}

function obtenerDatosFeed($userId)
{
    global $wpdb;
    $table_likes = "{$wpdb->prefix}post_likes";
    $table_intereses = INTERES_TABLE;
    $siguiendo = (array) get_user_meta($userId, 'siguiendo', true);
    $interesesUsuario = $wpdb->get_results($wpdb->prepare(
        "SELECT interest, intensity FROM $table_intereses WHERE user_id = %d",
        $userId
    ), OBJECT_K);
    $vistas_posts = get_user_meta($userId, 'vistas_posts', true);

    $args = [
        'post_type'      => 'social_post',
        'posts_per_page' => 5000,
        'date_query'     => [
            'after' => date('Y-m-d', strtotime('-100 days'))
        ],
        'fields'         => 'ids',
        'no_found_rows'  => true,
    ];
    $posts_ids = get_posts($args);

    if (empty($posts_ids)) {
        return [];
    }
    $placeholders = implode(', ', array_fill(0, count($posts_ids), '%d'));
    $meta_keys = ['datosAlgoritmo', 'Verificado', 'postAut'];
    $meta_keys_placeholders = implode(',', array_fill(0, count($meta_keys), '%s'));
    $sql_meta = "
        SELECT post_id, meta_key, meta_value
        FROM {$wpdb->postmeta}
        WHERE meta_key IN ($meta_keys_placeholders) AND post_id IN ($placeholders)
    ";

    $prepared_sql_meta = $wpdb->prepare($sql_meta, array_merge($meta_keys, $posts_ids));
    $meta_results = $wpdb->get_results($prepared_sql_meta);
    $meta_data = [];
    foreach ($meta_results as $meta_row) {
        $meta_data[$meta_row->post_id][$meta_row->meta_key] = $meta_row->meta_value;
    }
    $sql_likes = "
        SELECT post_id, COUNT(*) as likes_count
        FROM $table_likes
        WHERE post_id IN ($placeholders)
        GROUP BY post_id
    ";

    $likes_results = $wpdb->get_results($wpdb->prepare($sql_likes, $posts_ids));
    $likes_by_post = [];
    foreach ($likes_results as $like_row) {
        $likes_by_post[$like_row->post_id] = $like_row->likes_count;
    }
    $sql_posts = "
        SELECT ID, post_author, post_date, post_content
        FROM {$wpdb->posts}
        WHERE ID IN ($placeholders)
    ";
    $posts_results = $wpdb->get_results($wpdb->prepare($sql_posts, $posts_ids), OBJECT_K);
    $post_content = [];
    foreach ($posts_results as $post) {
        $post_content[$post->ID] = $post->post_content;
    }

    return [
        'siguiendo'        => $siguiendo,
        'interesesUsuario' => $interesesUsuario,
        'posts_ids'        => $posts_ids,
        'likes_by_post'    => $likes_by_post,
        'meta_data'        => $meta_data,
        'author_results'   => $posts_results,
        'post_content'     => $post_content,
    ];
}

function calcularFeedPersonalizado($userId, $identifier = '', $similar_to = null)
{
    $datos = obtenerDatosFeedConCache($userId); #Aqui se obtiene obtenerDatosFeed
    if (empty($datos)) {
        return [];
    }
    $usuario = get_userdata($userId);
    if (!$usuario || !is_object($usuario)) {
        return [];
    }
    $posts_personalizados = [];
    $current_timestamp = current_time('timestamp');
    $vistas_posts_processed = obtenerYProcesarVistasPosts($userId);
    $esAdmin = in_array('administrator', (array)$usuario->roles);
    $decay_factors = [];
    foreach ($datos['author_results'] as $post_data) {
        $post_date = $post_data->post_date;
        $post_timestamp = is_string($post_date) ? strtotime($post_date) : $post_date; 
        $diasDesdePublicacion = floor(($current_timestamp - $post_timestamp) / (3600 * 24));
        if (!isset($decay_factors[$diasDesdePublicacion])) {
            $decay_factors[$diasDesdePublicacion] = getDecayFactor($diasDesdePublicacion);
        }
    }
    $posts_data = $datos['author_results'];
    $puntos_por_post = calcularPuntosPostBatch(
        $posts_data,
        $datos,
        $esAdmin,
        $vistas_posts_processed,
        $identifier,
        $similar_to,
        $current_timestamp,
        $userId,
        $decay_factors 
    );

    if (!empty($puntos_por_post)) {
        arsort($puntos_por_post);
    }
    return $puntos_por_post;
}

function procesarPublicaciones($query_args, $args, $is_ajax)
{
    ob_start();
    $user_id = get_current_user_id();
    $cache_key = 'posts_count_' . md5(serialize($query_args)) . '_user_' . $user_id;
    $posts_count = 0;
    $total_posts = get_transient($cache_key);
    if ($total_posts === false) {
        $query_args['no_found_rows'] = false; 
        $query = new WP_Query($query_args);
        $total_posts = $query->found_posts;
        set_transient($cache_key, $total_posts, 12 * HOUR_IN_SECONDS);
    }

    echo '<input type="hidden" class="total-posts total-posts-' . esc_attr($args['filtro']) . '" value="' . esc_attr($total_posts) . '" />';
    if ($query->have_posts()) {
        $filtro = !empty($args['filtro']) ? $args['filtro'] : $args['filtro'];
        $tipoPost = $args['post_type'];

        if (!wp_doing_ajax()) {
            $clase_extra = 'clase-' . esc_attr($filtro);
            if (in_array($filtro, ['rolasEliminadas', 'rolasRechazadas', 'rola', 'likes'])) {
                $clase_extra = 'clase-rolastatus';
            }

            echo '<ul class="social-post-list ' . esc_attr($clase_extra) . '" 
                  data-filtro="' . esc_attr($filtro) . '" 
                  data-posttype="' . esc_attr($tipoPost) . '" 
                  data-tab-id="' . esc_attr($args['tab_id']) . '">';
        }

        while ($query->have_posts()) {
            $query->the_post();
            $posts_count++;

            if ($tipoPost === 'social_post') {
                echo htmlPost($filtro);
            } elseif ($tipoPost === 'colab') {
                echo htmlColab($filtro);
            } else {
                echo '<p>Tipo de publicación no reconocido.</p>';
            }
        }

        if (!wp_doing_ajax()) {
            echo '</ul>';
        }
    } else {
        echo nohayPost($filtro, $is_ajax);
    }
    wp_reset_postdata();
    return ob_get_clean();
}

function construirQueryArgs($args, $paged, $current_user_id, $identifier, $is_admin, $posts, $filtroTiempo, $similar_to)
{
    global $wpdb;
    $query_args = [
        'post_type' => $args['post_type'],
        'posts_per_page' => $posts,
        'paged' => $paged,
        'ignore_sticky_posts' => true,
        'suppress_filters' => false,
    ];
    if (!empty($identifier)) {
        $query_args = filtrarIdentifier($identifier, $query_args);
    }
    if ($args['post_type'] === 'social_post') {
        $query_args = ordenamientoQuery($query_args, $filtroTiempo, $current_user_id, $identifier, $similar_to, $paged, $is_admin, $posts);
    }
    return $query_args;
}