优化具有大量数据的MYSQL查询

时间:2018-01-28 14:36:23

标签: mysql left-join query-optimization

有没有更好的方法来编写此查询,因为它在大约400k行中搜索并花费50秒来执行?

SELECT pfile_print_media_id
     , market.market_name
     , full_name
     , image
     , pfile_date
  FROM pfile_print_media
  LEFT 
  JOIN pfiles 
    ON pfiles.pfile_id = pfile_print_media.pfile_id
  LEFT 
  JOIN publications 
    ON publications.publication_id = pfile_print_media.publication_id
  LEFT 
  JOIN clients 
    ON pfiles.client_id = clients.client_id
  LEFT 
  JOIN client_markets 
    ON client_markets.client_id = clients.client_id
  LEFT 
  JOIN market 
    ON market.market_id = clients.market
 WHERE pfiles.media_type =  'PRINT'
   AND clients.market = 17
    OR client_markets.market_id = 17
    OR client_type = 'INTERNATIONAL'
 GROUP 
    BY pfiles.pfile_id
 ORDER 
    BY pfiles.pfile_date DESC
 LIMIT 4

解释查询输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

如果在publication表中没有使用任何列 - 可能正在用子查询替换join,只是检查key的存在是否会加快查询速度。