优化sql查询 - wordpress - sql_big_selects

时间:2018-03-21 21:55:30

标签: mysql sql wordpress

我有一个大的SQL查询,需要以某种方式简化它,任何帮助赞赏。

我收到此错误: WordPress数据库错误:[SELECT将检查多于MAX_JOIN_SIZE行;检查你的WHERE并使用SET SQL_BIG_SELECTS = 1或SET MAX_JOIN_SIZE =#如果SELECT没问题的话

我设置$ wpdb->查询('SET SQL_BIG_SELECTS = 1')并且每次都会永久加载网站。

不想使用“SET SQL_BIG_SELECTS = 1”或wordpress meta_query,只是为了简化此查询。

我开了一个新问题,因为我没有适当的sql技能。

我正在使用的查询:

        $querystr = "SELECT $wpdb->posts.ID
            FROM
               $wpdb->posts,
               $wpdb->postmeta AS app_date,        
               $wpdb->postmeta AS provider,
               $wpdb->postmeta AS time1,
               $wpdb->postmeta AS time2,
               $wpdb->postmeta AS client

            WHERE
               $wpdb->posts.ID = app_date.post_id          
            AND
               $wpdb->posts.ID = provider.post_id
            AND
               $wpdb->posts.ID = time1.post_id
            AND
               $wpdb->posts.ID = time2.post_id
            AND
               $wpdb->posts.ID = client.post_id

            AND $wpdb->posts.post_type = 'ga_appointments'
            AND $wpdb->posts.post_status IN ('completed', 'publish', 'payment', 'pending')                 

            AND app_date.meta_key   = 'ga_appointment_date'
            AND app_date.meta_value = 2018-03-27

            AND provider.meta_key   = 'ga_appointment_provider'
            AND provider.meta_value = 55

            AND time1.meta_key = 'ga_appointment_time_end'
            AND time1.meta_value > 13:05

            AND time2.meta_key = 'ga_appointment_time'
            AND time2.meta_value < 14:05

            OR  client.meta_key = 'ga_appointment_client'
            AND client.meta_value REGEXP '\"email\";s:[1-9]+:\"jdoe@mail.com\"'

            OR  client.meta_key = 'ga_appointment_new_client'
            AND client.meta_value REGEXP '\"phone\";s:[1-9]+:\"999-656-5656\"'
        ";

1 个答案:

答案 0 :(得分:1)

看起来您需要在您的“或”附近放置正确的括号。命令

$querystr = "SELECT $wpdb->posts.ID
        FROM
           $wpdb->posts,
           $wpdb->postmeta AS app_date,        
           $wpdb->postmeta AS provider,
           $wpdb->postmeta AS time1,
           $wpdb->postmeta AS time2,
           $wpdb->postmeta AS client

        WHERE
           $wpdb->posts.ID = app_date.post_id          
        AND
           $wpdb->posts.ID = provider.post_id
        AND
           $wpdb->posts.ID = time1.post_id
        AND
           $wpdb->posts.ID = time2.post_id
        AND
           $wpdb->posts.ID = client.post_id

        AND $wpdb->posts.post_type = 'ga_appointments'
        AND $wpdb->posts.post_status IN ('completed', 'publish', 'payment', 'pending')                 

        AND app_date.meta_key   = 'ga_appointment_date'
        AND app_date.meta_value = 2018-03-27

        AND provider.meta_key   = 'ga_appointment_provider'
        AND provider.meta_value = 55

        AND time1.meta_key = 'ga_appointment_time_end'
        AND time1.meta_value > 13:05

        AND time2.meta_key = 'ga_appointment_time'
        AND (time2.meta_value < 14:05

        OR  client.meta_key = 'ga_appointment_client')
        AND (client.meta_value REGEXP '\"email\";s:[1-9]+:\"jdoe@mail.com\"'

        OR  client.meta_key = 'ga_appointment_new_client')
        AND client.meta_value REGEXP '\"phone\";s:[1-9]+:\"999-656-5656\"'
    ";