• Context
    Job_Postings_Helper::get_new_entries(); is called on is_admin(), this to show new entries bubble.

    Issue
    When site is big or have big “new” entries, the query become super slow, and causing memory to be exhausted. And what makes matter worst is, because front end also using admin-ajax, so that’s also is_admin() and this query slow it down.

    Proposed Solution
    Specify only fields that needed

    $args = array(
        'post_type' => 'job-entry',
        'meta_key' => 'job_entry_viewed',
        'meta_query' => array(
            array(
                'key'     => 'job_entry_viewed',
                'value'   => 'no',
                'compare' => '==',
            ),
        ),
        'posts_per_page' => -1,
        'fields'         => 'ids', // id only, so it doesn't initiate full Post object to all of it
    );
    $new = new WP_Query( $args );
    $new = $new->found_posts;
    
    wp_reset_postdata();
    wp_reset_query();

    And cache should be implemented here as well, and break the cache only if the counter possibly changed:

    • ajax_submit
    • render_job_entry_metabox
Viewing 1 replies (of 1 total)
  • Thread Starter pentatonicfunk

    (@pentatonicfunk)

    note, it might need better approach overall, even specify only ids, in general that query is slow.

    SELECT wp_qe_posts.ID
    FROM wp_qe_posts
    INNER JOIN wp_qe_postmeta
    ON ( wp_qe_posts.ID = wp_qe_postmeta.post_id )
    INNER JOIN wp_qe_postmeta AS mt1
    ON ( wp_qe_posts.ID = mt1.post_id )
    WHERE 1=1
    AND ( wp_qe_postmeta.meta_key = 'job_entry_viewed'
    AND ( ( mt1.meta_key = 'job_entry_viewed'
    AND mt1.meta_value = 'no' ) ) )
    AND ((wp_qe_posts.post_type = 'job-entry'
    AND (wp_qe_posts.post_status = 'publish'
    OR wp_qe_posts.post_status = 'future'
    OR wp_qe_posts.post_status = 'draft'
    OR wp_qe_posts.post_status = 'pending')))
    GROUP BY wp_qe_posts.ID
    ORDER BY wp_qe_posts.post_date DESC
    ---	
    WP_Query->get_posts
    WP_Query->query
    WP_Query->__construct
    Job_Postings_Helper::get_new_entries
    JobPostType::register
    do_action('init')
    require_once('wp-settings.php')
    require_once('wp-config.php')
    require_once('wp-load.php')
    require_once('wp-admin/admin.php')
    ---	
    5.1495 seconds to complete
Viewing 1 replies (of 1 total)
  • The topic ‘get_new_entries function is not performance friendly’ is closed to new replies.