optimise SQL wordpress call

The query itself may need to be optimized (SQL is not my speciality, though). However, don’t forget you can store the results of an expensive operation in cached memory. WordPress offers a Transients API to make things easy. Here’s a quick example:

// Try to load the value from cache
$transient_key = 'my_query';
$value = get_transient( $transient_key );

// If no cached value was found, perform the query and cache it
if ( false === $value ) {
    $value = $wpdb->get_results( 'SELECT * FROM ...' );
    set_transient( $transient_key, $value, HOUR_IN_SECONDS );
}