• I am trying to add “Top posts” by views and I am using “meta fields” for filed registration and then using following code to display posts with thumbs and titles

    https://pastebin.com/TRMK6hEB

    Problem is that it’s taking almost 2x 3x load on a dedicated server. Normally its 3.0 load on the server but as soon as I add this query it becomes 6.0 to 7.0

    How can I optimize this query?

    • This topic was modified 5 years, 3 months ago by Rakesh Raja.
    • This topic was modified 5 years, 3 months ago by Rakesh Raja.
Viewing 2 replies - 1 through 2 (of 2 total)
  • $popularpost = new WP_Query( array( 'posts_per_page' => 8, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'rand meta_value_num', 'order' => 'DESC' ) );

    Does that query even give you what you want?
    Any use of rand is going to slow things down. And queries with meta fields will be slower than without since they are stored in another table.

    Since post titles can have HTML, your code will show the HTML. It might be better to use the_title() and make it truncate or wrap by using CSS. (have to account for all screen widths anyway)

    Dion

    (@diondesigns)

    The meta_value columns in the WP database are defined as LONGTEXT and are not indexed, so any use of them in selection and/or sorting conditions will be extremely slow.

    The only way to speed up your WP_Query instance is to remove the orderby stuff from the array, and then use PHP to sort the returned results. That is a lot of work, but it will be much faster than MySQL sorting a non-indexed LONGTEXT column.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Optimization of a query’ is closed to new replies.