如何优化这个复杂的MYSQL查询?

时间:2016-01-08 00:27:26

标签: mysql join

由于复杂的MySQL查询,我们的网站有时会导致极端的服务器压力。该网站实际上已经下降。

webhoster警告说,如果我们不按顺序将其暂停,我们将暂停我们的帐户。

有人可以指出这个查询的哪些部分占用了大部分资源吗?

有关改善这一点的任何建议吗?

SELECT SQL_CALC_FOUND_ROWS  wp_s3mv0r_posts.ID 
FROM wp_s3mv0r_posts 
INNER JOIN wp_s3mv0r_term_relationships ON (wp_s3mv0r_posts.ID = wp_s3mv0r_term_relationships.object_id) 
INNER JOIN wp_s3mv0r_postmeta ON ( wp_s3mv0r_posts.ID = wp_s3mv0r_postmeta.post_id ) 
INNER JOIN wp_s3mv0r_postmeta AS mt1 ON ( wp_s3mv0r_posts.ID = mt1.post_id )  
INNER JOIN wp_s3mv0r_postmeta AS mt2 ON ( wp_s3mv0r_posts.ID = mt2.post_id )  
INNER JOIN wp_s3mv0r_postmeta AS mt3 ON ( wp_s3mv0r_posts.ID = mt3.post_id )     
INNER JOIN wp_s3mv0r_postmeta AS mt4 ON ( wp_s3mv0r_posts.ID = mt4.post_id )  
INNER JOIN wp_s3mv0r_postmeta AS mt5 ON ( wp_s3mv0r_posts.ID = mt5.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt6 ON ( wp_s3mv0r_posts.ID = mt6.post_id )  
INNER JOIN wp_s3mv0r_postmeta AS mt7 ON ( wp_s3mv0r_posts.ID = mt7.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt8 ON ( wp_s3mv0r_posts.ID = mt8.post_id )  
INNER JOIN wp_s3mv0r_postmeta AS mt9 ON ( wp_s3mv0r_posts.ID = mt9.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt10 ON ( wp_s3mv0r_posts.ID = mt10.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt11 ON ( wp_s3mv0r_posts.ID = mt11.post_id )  
INNER JOIN wp_s3mv0r_postmeta AS mt12 ON ( wp_s3mv0r_posts.ID = mt12.post_id ) 
WHERE 1=1  AND (
  wp_s3mv0r_term_relationships.term_taxonomy_id IN (11,24,25)
) AND (
  ( wp_s3mv0r_postmeta.meta_key = 'acf_house_minprice' AND CAST(wp_s3mv0r_postmeta.meta_value AS SIGNED) BETWEEN '274990' AND '599990' )
  AND
  ( mt1.meta_key = 'acf_house_minlotwidth' AND CAST(mt1.meta_value AS SIGNED) BETWEEN '16' AND '16' )
  AND
  ( mt2.meta_key = 'acf_location_area' AND CAST(mt2.meta_value AS CHAR) IN ('South of river') )
  AND
  ( mt3.meta_key = 'acf_house_bedroom' AND CAST(mt3.meta_value AS CHAR) = '4' )
  AND
  ( mt4.meta_key = 'acf_house_studyroom' AND CAST(mt4.meta_value AS SIGNED) > '0' )
  AND
  ( mt5.meta_key = 'acf_house_theaterroom' AND CAST(mt5.meta_value AS SIGNED) > '0' )
  AND
  ( mt6.meta_key = 'acf_house_alfresco' AND CAST(mt6.meta_value AS SIGNED) > '0' )
  AND
  ( mt7.meta_key = 'acf_house_activityroom' AND CAST(mt7.meta_value AS SIGNED) > '0' )
  AND
  ( mt8.meta_key = 'acf_house_doublegarage' AND CAST(mt8.meta_value AS SIGNED) > '0' )
  AND
  ( mt9.meta_key = 'acf_house_reargarage' AND CAST(mt9.meta_value AS SIGNED) > '0' )
  AND
  ( mt10.meta_key = 'acf_house_islbeninkitchen' AND CAST(mt10.meta_value AS SIGNED) > '0' )
  AND
  ( mt11.meta_key = 'acf_house_frontmasterbedroom' AND CAST(mt11.meta_value AS SIGNED) > '0' )
  AND
  ( mt12.meta_key = 'acf_house_rearmaster' AND CAST(mt12.meta_value AS SIGNED) > '0' )
) AND wp_s3mv0r_posts.post_type = 'house' AND (wp_s3mv0r_posts.post_status = 'publish') 
 GROUP BY wp_s3mv0r_posts.ID 
 ORDER BY wp_s3mv0r_posts.post_date DESC 
 LIMIT 0, 10

1 个答案:

答案 0 :(得分:0)

这个怎么样:

SELECT p.ID, count(*) AS 'PostCount'
FROM wp_s3mv0r_posts p INNER JOIN wp_s3mv0r_term_relationships r ON (p.ID = r.object_id) 
    INNER JOIN wp_s3mv0r_postmeta pm ON (p.ID = pm.post_id)
WHERE 
    r.term_taxonomy_id IN (11,24,25) AND p.post_type = 'house' AND p.post_status = 'publish' AND (
    (pm.meta_key = 'acf_house_minprice' AND pm.meta_value BETWEEN '274990' AND '599990') OR
    (pm.meta_key = 'acf_house_minlotwidth' AND pm.meta_value BETWEEN '16' AND '16') OR
    (pm.meta_key = 'acf_location_area' AND pm.meta_value = 'South of river') OR
    (pm.meta_key = 'acf_house_bedroom' AND pm.meta_value = '4') OR
    (pm.meta_key IN ('acf_house_studyroom', 'acf_house_theaterroom', 'acf_house_alfresco', 'acf_house_activityroom', 
        'acf_house_doublegarage', 'acf_house_reargarage', 'acf_house_islbeninkitchen', 'acf_house_frontmasterbedroom', 
        'acf_house_rearmaster') AND pm.meta_value > '0'))
GROUP BY p.ID 
ORDER BY p.post_date DESC 
LIMIT 0, 10