简体   繁体   中英

Best performance for mySQL and PHP: Lots of small queries, or a large one?

So i have this technicall dilemma:

I have a Wordpress site, with more than 50 categories (and rising). I want to get just the six categories with the newest article in them, and -if i get this right- i can't do it with a single typical WP Query, i'll have to improvise:

a) get from the database the last 200-300 posts, and by elimination in PHP filter by category, and show the six last updated categories, or

b) get from the database all my categories, and for each category ask again the database the last article by date - and show the six last updated categories.

i know that the first solution is not very safe, and will strech a little my server calculating with PHP, but the second will have more than 50+1 queries (and rising).

Considering that the site i'm working on has a couple of million visitors each month, which one do you think will give, from a technical point of view, the less stress to my servers?

Or i'm missing an more obvius solution?

Thanks for your time: :)

Every time you post something, you could save the taxonomy's name / slug / id in a transient on publishing/updating through set_transient() and save_post action hook.

Saving the data as an array and slicing it once it reach 6... array_search before updating...

To display them you would simply loop through the transient's value and get_taxonomy() or build a custom WP_Query looping through each of them.

Which would avoid you a lot of queries. Pretty much none...

here is an untested demo:

<?php

//Retieve the transient through get_transient( '_transient_wpso_73900689' );
add_action( 'save_post', function ( $post_ID, $post, $update ) {

    $transient = '_transient_wpso_73900689';

    $expiration = 0;

    $prev_value  = ! empty( get_transient( $transient ) ) ? get_transient( $transient ) : array();

    $next_value = get_post_taxonomies( $post_ID );

    $buffer = array();

    if ( ! empty( $next_value ) ) {

        foreach ( $next_value as $taxonomy ) {

            //Search the previous transient array.
            if ( array_search( $taxonomy, $prev_value ) === false ) {
    
                //If the value doesn't exist, push the value to the array.
                array_push( $buffer, $taxonomy );

            };

        };

        $value = array_merge( $prev_value, $buffer );

        //Reverse the array.
        $value = array_reverse( $value );

        //Retrieve the last 6.
        $value = array_slice( $value, 0, 6 );

        //Set the transient.
        set_transient( $transient, $value, 0 );

    };

}, 10, 3 );

Can,t you just join the articles and sort by the joined table ordered by date desc?

Something like: Select from categories c Left join articles a on a.category = c.id Order by a.date desc group by c.id limit 6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM