2017-07-11 4 views
0

Ich habe versucht, die MySQL-Abfrage unten ohne Erfolg zu optimieren. Es dauert noch bis zu 3 Sekunden auszuführen:UNION ODER UNION ALLE MYSQL OPTIMIERUNG

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 

Was will ich tun:

  • Suchen drei Spalten in 3 Tabellen für ein Keyword und geben die Distinct Count

Grundlegende Tabellenstruktur:

1. sub_jobs

  • id
  • Titel
  • PARENT_ID
    • id
    • job_title [i dieser Spalte suchen möchten]

    2. main_jobs [Ich will t o suchen diese Spalte]

  • company_id [einige Zeilen einen Eintrag für diese Spalte]

3. Unternehmen

  • id
  • company_name [Ich möchte nicht suchen haben diese Spalte]

ERKLÄREN

------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 

Ich habe versucht, UNION ohne die DISTINCT ohne signifikante Gewinne zu verwenden.

Wie kann ich die Laufzeit dieser Abfrage reduzieren.

Antwort

0

Versucht, eine Bedingung zu reduzieren.Bitte versuchen Sie und mich

select count(DISTINCT v.id) from 
( 
    select sub_jobs.id 
    from sub_jobs inner join main_jobs 
    on main_jobs.id=sub_jobs.parent_id 
    where (sub_jobs.title LIKE '%abc%' OR job_title LIKE '%abc%') 

    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 

bearbeiten nach Frage Update

wissen zu lassen, ist es möglich, zwei Abfragen wie folgt zu kombinieren:

select count(DISTINCT v.id) from 
( 
    select sub_jobs.id 
    from sub_jobs 
     inner join main_jobs 
      on main_jobs.id=sub_jobs.parent_id 
     inner join companies 
      on main_jobs.company_id=companies.id 
    where companies.company_name LIKE '%abc%' 
      OR 
      sub_jobs.title LIKE '%abc%' 
      OR 
      job_title LIKE '%abc%'  
) 
as v 

Lassen Sie mich wissen, ob es funktioniert oder wirft Fehler

+0

Vielen Dank für Ihre Hilfe Plan ein Blick auf, erklären. Keine sichtbare Veränderung. –

0

können Sie versuchen, eine einzelne quert mit oder anstelle der 3 Abfrage mit Union

aber Sie sollten

select count(dictinnct sub_jobs.id) 
    from sub_jobs 
    INNER JOIN main_jobs on main_jobs.id=sub_jobs.parent_id 
    INNER JOIN companies on main_jobs.company_id=companies.id 
    where companies.company_name LIKE '%abc%' 
    OR main_jobs.job_title LIKE '%abc%' 
    OR sub_jobs.title LIKE '%abc%'