2017-07-10 139 views
0

是今天在所有可能的優化下面的查詢,這是生產JOIN:優化查詢與LEFT多WHERE子句

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子句中檢索的字段中包含了多列索引。

有什麼想法嗎?謝謝。

+0

查看'EXPLAIN select ...'輸出 –

+0

雖然'x%'可以使用索引,'%x'不能,所以對於這種查詢,性能總是會受到限制。 – Strawberry

+0

我懷疑你有'... AND a.id_job_spec = 441 AND a.id_job_spec IN(SELECT ...)中的錯誤'也許你想'OR'? –

回答

0

我會從一個索引開始:j5xap_jbjobs_job(id_job_spec, is_active, publish_date, is_expire)。對於所有其他表,您需要連接鍵上的索引。

這應該允許使用索引進行過濾,這應該是性能上的勝利。