使用具有多个WHERE子句的LEFT JOIN优化查询

时间:2017-07-10 21:16:15

标签: mysql sql optimization

是否可以优化下面的查询,该查询是今天制作的:

SELECT DISTINCT a.*,country,c.id 
id_employer,c.comp_name,c.show_comp_name,comp_type,b.id countryid,if(now() 
<a.hotjob_expire, true, false) is_hot 
FROM j5xap_jbjobs_job a 
LEFT JOIN j5xap_jbjobs_country b ON a.id_country = b.id 
LEFT JOIN j5xap_jbjobs_employer c ON a.employer_id = c.user_id 
LEFT JOIN j5xap_jbjobs_custom_field_value cv ON cv.jobid=a.id 
LEFT JOIN j5xap_jbjobs_comp_type d ON c.id_comp_type = d.id 
WHERE (a.job_title LIKE '%Web developer%' 
   OR a.state LIKE '%Web developer%' 
   OR a.city LIKE '%Web developer%' 
   OR b.country LIKE '%Web developer%' 
   OR c.comp_name LIKE '%Web developer%' 
   OR cv.value LIKE '%Web developer%' 
   OR cv.valuetext LIKE '%Web developer%' 
   OR a.short_desc LIKE '%Web developer%' 
   OR a.long_desc LIKE '%Web developer%') 
   OR (a.job_title LIKE '%kannur%' 
   OR a.state LIKE '%kannur%'
   OR a.city LIKE '%kannur%' 
   OR b.country LIKE '%kannur%' 
   OR c.comp_name LIKE '%kannur%' 
   OR cv.value LIKE '%kannur%' 
   OR cv.valuetext LIKE '%kannur%' 
   OR a.short_desc LIKE '%kannur%' 
   OR a.long_desc LIKE '%kannur%') 
   AND a.id_job_spec =441 AND a.id_job_spec 
   IN (SELECT id FROM j5xap_jbjobs_job_spec WHERE id_category='63') 
   AND 
   a.is_active='y' AND a.publish_date <= '2017-07-07 00:00:00' 
   AND
   expire_date  >= '2017-07-07 00:00:00' 
   AND 
   expire_date <> '0000-00-00 00:00:00' 
   ORDER BY is_hot DESC, a.is_featured DESC, a.publish_date DESC, a.id                      
   DESC LIMIT 0, 10;

我一直在广泛地看待它,但我有点想法。必须在此上下文中使用左连接。我已经在表和从SELECT子句中检索的字段中包含了多列索引。

有什么想法吗?谢谢。

1 个答案:

答案 0 :(得分:0)

我会从索引开始:j5xap_jbjobs_job(id_job_spec, is_active, publish_date, is_expire)。对于所有其他表,您需要连接键的索引。

这应该允许使用索引进行过滤,这应该是性能的胜利。