我一直在試圖優化下面的MySQL查詢無濟於事。目前,它最多需要3秒,執行:UNION或UNION ALL MYSQL OPTIMIZATION
select count(DISTINCT v.id) from
(
select sub_jobs.id from sub_jobs, main_jobs where sub_jobs.title LIKE '%abc%' and main_jobs.id=sub_jobs.parent_id
UNION ALL
select sub_jobs.id from sub_jobs, main_jobs where job_title LIKE '%abc%' and main_jobs.id=sub_jobs.parent_id
UNION ALL
select sub_jobs.id from sub_jobs, main_jobs, companies where companies.company_name LIKE '%abc%' and main_jobs.company_id=companies.id and main_jobs.id=sub_jobs.parent_id
)
as v
怎麼做我想做的事:
- 搜索三列3代表的關鍵字,並返回重複計數
基本表結構:
個1. sub_jobs
- ID
- 標題[I要搜索該列]
- PARENT_ID
2. main_jobs
- ID
- JOB_TITLE [我想要t Ø搜索此列]
- COMPANY_ID [某些行不具備此列中的條目]
3.公司
- ID
- COMPANY_NAME [我要搜索此列]
EXPLAIN
------column titles------
id
select_type:
table
type
possible_keys
key
key_len
ref
rows
Extra
--- row 1 ----
id: 1
select_type: PRIMARY
table: <derived2>
ALL
NULL
NULL
NULL
NULL
102642
NULL
---- row 2 ----
id: 2
select_type: DERIVED
table: main_jobs
index
PRIMARY,id_index,id_industry_featured_index
id_index
4
NULL
34214
Using index
---- row 3 ----
id: 2
select_type: DERIVED
table: sub_jobs
ref
parent_id,parent_id_category_index
parent_id
4
myjobmag_myjobdb.main_jobs.id
1
Using where
---- row 4 ----
id: 3
select_type: UNION
table: main_jobs
ALL
PRIMARY,id_index,id_industry_featured_index
NULL
NULL
NULL
34214
Using where
---- row 5 ----
id: 3
select_type: UNION
table: sub_jobs
ref
parent_id,parent_id_category_index
parent_id
4
main_jobs.id
1
Using index
---- row 6 ----
id: 4
select_type: UNION
table: main_jobs
ALL
PRIMARY,id_index,id_industry_featured_index
NULL
NULL
NULL
34214
NULL
---- row 7 ----
id: 4
select_type: UNION
table: companies
eq_ref
PRIMARY
PRIMARY
4
main_jobs.company_id
1
Using where
---- row 8 ----
id: 4
select_type: UNION
table: sub_jobs
ref
parent_id,parent_id_category_index
parent_id
4
main_jobs.id
1
Using index
---- row 9 ----
id: NULL
select_type: UNION RESULT
table: <union2,3,4>
ALL
NULL
NULL
NULL
NULL
NULL
Using temporary
我試過使用沒有DISTINCT的UNION沒有顯着的收益。
如何減少此查詢的運行時間。
謝謝您的幫助。沒有明顯的變化。 –