Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 元查询导致wordpress查询速度非常慢_Php_Mysql_Wordpress - Fatal编程技术网

Php 元查询导致wordpress查询速度非常慢

Php 元查询导致wordpress查询速度非常慢,php,mysql,wordpress,Php,Mysql,Wordpress,我试图在幻灯片中添加基于两个不同键的两种类型的幻灯片,只是元查询非常慢。我正在使用的wordpress查询是: $args = array( 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'sgt_slide', 'value' => 'on', 'compare' => '='

我试图在幻灯片中添加基于两个不同键的两种类型的幻灯片,只是元查询非常慢。我正在使用的wordpress查询是:

$args = array(
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'sgt_slide',
            'value' => 'on',
            'compare' => '='
        ),
        array(
            'key' => 'sgt_slide_home',
            'value' => 'on',
            'compare' => '='
        )
    ),
    'no_found_rows' => true, //exclude unnecessary paging calculations
    'numberposts' => -1,
    'post_status' => array( 'publish', 'inherit' ),
    'post_type' => array( 'post', 'attachment' )
    );
$slides = get_posts($args);
以及生成的sql匹配查询

SELECT   nd_posts.* FROM nd_posts  INNER JOIN nd_postmeta ON (nd_posts.ID = nd_postmeta.post_id)
INNER JOIN nd_postmeta AS mt1 ON (nd_posts.ID = mt1.post_id) WHERE 1=1  AND nd_posts.post_type IN ('post', 'attachment') AND ((nd_posts.post_status = 'publish' OR nd_posts.post_status = 'inherit')) AND ( (nd_postmeta.meta_key = 'sgt_slide' AND CAST(nd_postmeta.meta_value AS CHAR) = 'on')
OR  (mt1.meta_key = 'sgt_slide_home' AND CAST(mt1.meta_value AS CHAR) = 'on') ) GROUP BY nd_posts.ID ORDER BY nd_posts.post_date DESC
此sql在7秒内加载查询,并在phpmyadmin中测试。我怎样才能克服这个问题

解释SQL

尝试添加
“缓存结果”=>false,

到您的循环

确定如何基于此解决此问题:

并使用新的
WP\u query
get\u posts()
函数模拟了相同的
get\u posts
模式。

WordPress强制转换为char(),停止使用索引。以下代码使用筛选器删除强制转换:

/**
 * Remove casting a meta_query to a char as this stops it using the index!
 */

function my_filter_meta_query( $pieces ) {
 if ( !empty( $pieces['where'] ) ) {
     // remove casting to CHAR as this is already a string
     $pieces['where'] = preg_replace("@CAST\(([^.]*.meta_value) AS CHAR\)@", "$1",$pieces['where']);
 }
return $pieces;
}

add_filter( 'get_meta_sql', 'my_filter_meta_query' );

更多详细信息

您是否尝试过“解释”sql?@BenjaminPoignant添加了它。您需要在
post\u类型
上添加索引,例如
alter table和
建议:为了查看查询的瓶颈在哪里,开始逐个禁用
$args的组件
Ok,我重新研究了一些,发现
关系=>或
是问题所在。但如何解决我的案子。我需要有
get_posts
功能。它用1秒改进了它,从7秒加载到6秒加载。但我需要将其改进到不到1秒,根据我的研究,问题在于
meta_查询
=>
关系
子数组元素。
/**
 * Remove casting a meta_query to a char as this stops it using the index!
 */

function my_filter_meta_query( $pieces ) {
 if ( !empty( $pieces['where'] ) ) {
     // remove casting to CHAR as this is already a string
     $pieces['where'] = preg_replace("@CAST\(([^.]*.meta_value) AS CHAR\)@", "$1",$pieces['where']);
 }
return $pieces;
}

add_filter( 'get_meta_sql', 'my_filter_meta_query' );