使用同一表上的多个联接优化SQL脚本

时间:2018-07-20 07:01:16

标签: mysql join

我的搜索查询速度很慢,大约需要28秒才能运行。我想将其运行时间减少到1秒以内,我确信它运行缓慢的原因可能与我多次在同一张桌子上进行内部联接有关。

任何能为我指明正确方向的建议都将受到赞赏。

这是我的查询:

SELECT
  wp_posts.ID,
  wp_posts.post_title

FROM wp_posts

  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
  INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id AND ( mt1.meta_key = 'wpcm_frdate' AND mt1.meta_value IN ('2017','2018','2014') ))
  INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id AND ( mt2.meta_key = 'vehicle_type' AND mt2.meta_value IN ('HATCHBACK','SEDAN') ) )
  INNER JOIN wp_postmeta AS mt3 ON ( wp_posts.ID = mt3.post_id AND ( mt3.meta_key = 'vehicle_transmission' AND mt3.meta_value IN ('AUTOMATIC','MANUAL','DSG') ) )
  INNER JOIN wp_postmeta AS mt4 ON ( wp_posts.ID = mt4.post_id AND ( mt4.meta_key = 'symbol' AND mt4.meta_value IN ('!','%','*') ) )
  INNER JOIN wp_postmeta AS mt5 ON ( wp_posts.ID = mt5.post_id AND ( mt5.meta_key = 'stock_code'   AND mt5.meta_value IN ('2020','3380','4563','12063') ))
  INNER JOIN wp_postmeta AS mt6 ON ( wp_posts.ID = mt6.post_id AND ( mt6.meta_key = 'wpcm_mileage' AND CAST(mt6.meta_value AS SIGNED) BETWEEN '0'  AND '305000' ))
  INNER JOIN wp_postmeta AS mt7 ON ( wp_posts.ID = mt7.post_id AND ( mt7.meta_key = 'wpcm_price'   AND CAST(mt7.meta_value AS SIGNED) BETWEEN '0'  AND '1500000' )
                                     AND ( wp_postmeta.meta_key = 'wpcm_make'    AND wp_postmeta.meta_value  IN ('68','74') ))
  INNER JOIN wp_postmeta AS mt8 ON ( wp_posts.ID = mt8.post_id AND ( mt8.meta_key = 'wpcm_model'   AND mt8.meta_value IN ('84','75','151','69','91','223','247','70') ) )
  INNER JOIN wp_postmeta AS mt9 ON ( wp_posts.ID = mt9.post_id AND ( ( mt9.meta_key = 'wpcm_condition' AND mt9.meta_value IN ('new','pre-owned','top-marques') )))
WHERE 1=1 AND wp_posts.post_type = 'wpcm_vehicle' AND ((wp_posts.post_status = 'publish'))

GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC;

0 个答案:

没有答案