0

我正在尝试在 Wordpress 中设置警报,以便用户可以创建警报,以便在创建的新帖子(属性)符合他们设定的标准(警报)时收到通知。我的计划是使用两种帖子类型来做到这一点,一种用于属性,一种用于警报。然后,当保存属性帖子时,它将执行以下示例查询:

SELECT 
    post_author
    FROM 
        wp_posts
    INNER JOIN wp_postmeta m1
        ON wp_posts.ID = m1.post_id
        AND m1.meta_key = 'a_property_type'
        AND m1.meta_value = 'flat'
    INNER JOIN wp_postmeta m2
        on wp_posts.ID = m2.post_id
        AND m2.meta_key = 'a_bedrooms_min' 
        AND m2.meta_value <= '2'
    INNER JOIN wp_postmeta m3
        on wp_posts.ID = m3.post_id
        AND m3.meta_key = 'a_bedrooms_max' 
        AND m3.meta_value >= '2'
    INNER JOIN wp_postmeta m4
        on wp_posts.ID = m4.post_id
        AND m4.meta_key = 'a_bathrooms_min' 
        AND m4.meta_value <= '2'
    INNER JOIN wp_postmeta m5
        on wp_posts.ID = m5.post_id
        AND m5.meta_key = 'a_bathrooms_max' 
        AND m5.meta_value >= '2'      
    INNER JOIN wp_postmeta m6
        on wp_posts.ID = m6.post_id
        AND m6.meta_key = 'a_garden' 
        AND m6.meta_value >= '1'        
    INNER JOIN wp_postmeta m7
        on wp_posts.ID = m7.post_id
        AND m7.meta_key = 'a_garage' 
        AND m7.meta_value >= '1'    
    INNER JOIN wp_postmeta m8
        on wp_posts.ID = m8.post_id
        AND m8.meta_key = 'a_parking' 
        AND m8.meta_value >= '1'        
    INNER JOIN wp_postmeta m9
        on wp_posts.ID = m9.post_id
        AND m9.meta_key = 'a_double_glazing' 
        AND m9.meta_value >= '1'    
    INNER JOIN wp_postmeta m10
        on wp_posts.ID = m10.post_id
        AND m10.meta_key = 'a_country' 
        AND m10.meta_value LIKE 'Scotland'        
    WHERE
        wp_posts.post_type = 'alerts'
        AND wp_posts.post_status = 'publish'
    GROUP BY 
       wp_posts.ID
    ORDER BY 
        wp_posts.post_date DESC

这有效并且确实返回匹配标准集的属性,但是当我引入 LIKE 时,它确实使查询挂起。我正在使用默认的 Wordpress 表“wp_posts”和“wp_postmeta”。我尝试在“meta_value”列上设置索引,但因为它是一个“longtext”字段,它不允许我这样做。

是否有更好的解决方案可以做到这一点,或者我的查询可以更有效地编写?

谢谢

4

1 回答 1

1

除了 eggyal 提供的东西,我也会在 (post_id, meta_key, meta_value) 上添加一个索引。如果这对您的查询没有帮助,我会尝试另一件事。将除 LIKE 之外的所有元素预包装到预查询中,以便首先处理内部查询并返回一组非常小的 ID,然后加入 LIKE 条件。

假设您有 1000 个属性,但只有 35 个符合您的所有其他条件,并且在亚秒级时间内得到解决。现在您只有 35 条记录将被尝试添加到您的附加 LIKE 条件。就像是

select
      wp.Post_Author
   from
      ( SELECT 
              post_id,
              post_author
           FROM 
              wp_posts
                 INNER JOIN wp_postmeta m1
                    ON wp_posts.ID = m1.post_id
                   AND m1.meta_key = 'a_property_type'
                 INNER JOIN the m2 through m9 
           WHERE
               type and status criteria ) as PreQuery
      INNER JOIN your m10 version
         on  PreQuery.Post_ID = m10.post_id
        AND ... rest of join condition
于 2012-12-02T22:06:27.157 回答