我有一个大的WP站点,有大约2k个帖子和150k个Posteta。当浏览到一个大类别时,归档文件的加载速度非常慢,因为WP查询会首先显示特色帖子。我知道最终的目标是优化Posteta表,但现在我想知道是否有任何可能的方法来优化下面的元查询(底部的一个)。
我做了一些调试(参见//注释),发现“比较”=>“!=”导致查询速度非常慢。我还发现“或”<;\'查询速度大大加快,结果也一样,但我不确定这样做是否合适。请让我知道是否有办法加快此查询。谢谢
function appthemes_addon_on_top_query( $wp_query ){
$addon_type = $wp_query->get( \'addon_on_top\' );
if( ! $addon_type || ! appthemes_addon_exists( $addon_type ) ) {
return;
}
$addon_info = appthemes_get_addon_info( $addon_type );
$flag_key = $addon_info[\'flag_key\']; //custom field for featured posts
$meta_query = (array) $wp_query->get( \'meta_value\', 1 );
$meta_query = array_filter( $meta_query );
$meta_query[] = array(
\'relation\' => \'OR\',
array(
\'key\' => $flag_key,
\'compare\' => \'NOT EXISTS\',
),
array(
\'relation\' => \'OR\',
//Deleting the array below makes the query quick, but i need it to show featured posts first
array(
\'key\' => $flag_key,
\'value\' => 1,
),
//Deleting the array below makes the query quick, but i need it to properly sorting the posts by date (see below: $wp_query->set( \'orderby\')
array(
\'key\' => $flag_key,
\'value\' => 1,
\'compare\' => \'!=\', //It is about this comparison. \'\' or \'<\' makes the query quick, but i am not sure if that is a proper replacement. Also not sure why \'!=\' makes it this slow.
),
),
);
$wp_query->set( \'meta_query\', $meta_query );
$wp_query->set( \'orderby\', array( \'meta_value\' => \'DESC\', \'date\' => \'DESC\' ) );
}