2016-03-26 11 views
1

Hier habe ich eine Abfrage Szenario, erklärt den Umfang in Inline-Kommentare:Vervielfältigung Entfernung mit Gruppe von oder verschieden

select 
    -- selecting both entity ids 
    entity_a.id as entity_a_id, 
    entity_b.id as entity_b_id, 
    concat(entity_a.id, entity_b.id) as `key` 

from `entity_b` 

-- Following are few one to many relations to match entity a with b 
inner join `entity_b_function` on 
    `entity_b`.`id` = `entity_b_function`.`entity_b_id` 
inner join `entity_b_category` on 
    `entity_b`.`id` = `entity_b_category`.`entity_b_id` 
inner join `entity_b_scope` on 
    `entity_b`.`id` = `entity_b_scope`.`entity_b_id` 

inner join `entity_a` on 
    `entity_a`.`category_id` = `entity_b_category`.`category_id` and 
    `entity_a`.`scope_id` = `entity_b_scope`.`scope_id` 
inner join `entity_a_function` on 
    `entity_b_function`.`function_id` = `entity_a_function`.`function_id` 


-- pivot of entity a and b 
-- making sure matching entities are finally related in pivot 
left join `entity_a_b_pivot` on 
    `entity_a_b_pivot`.`entity_a_id` = `entity_a`.`id` and 
    `entity_a_b_pivot`.`entity_b_id` = `entity_b`.`id` 

where 
    -- we need only matching entities which are not yet related in pivot 
    `entity_a_b_pivot`.`id` is null and 
    -- when both entities are active in the system 
    `entity_b`.`status` = 1 and 
    `entity_a`.`status` = 1 
LIMIT 5000; 

Derzeit wie unten geführt:
(spitze Gegenstände sind Vervielfältigung aufgrund schließt sich von einem zu vielen Beziehungen)

entity_a_id, entity_b_id  key 
    1    1   11 
> 1    1   11 
    1    2   12 
    2    1   21 
    2    2   22 
> 2    2   22 

Hier wird, wenn entweder ich GROUP BY key oder DISTINCT(key) verwenden, um die Duplikate zu beseitigen, die Abfrageverarbeitung für immer mit 100% CPU-Auslastung fest, aber ohne diese ist es nur ein Blinzeln RETU rn 5K Datensätze, aber mit 90% Duplikaten.

Wie kann die Abfrage für eindeutige Ergebnisse optimiert werden?

+0

Beispieldaten und gewünschte Ergebnisse würden helfen. Es gibt wahrscheinlich einen einfacheren Weg, um zu tun, was Sie wollen. –

+0

Bitte senden Sie EXPLAIN-Ergebnisse für beide Fälle. –

+0

Ist die Überschneidung mit 'concat (entity_a.id, entity_b.id) als Schlüssel 'absichtlich btw? Wenn 'entity_a.id' 1 ist und 'entity_b.id' 12 ist, hat es den gleichen Schlüssel wie bei 'entity_a.id' ist 11 und 'entity_b.id' ist 2. –

Antwort

1

Wie wäre es, einfach DISTINCT am Anfang der Auswahlliste hinzufügen?

select 
    -- selecting both entity ids 
    distinct 
    entity_a.id as entity_a_id, 
    entity_b.id as entity_b_id, 
    concat(entity_a.id, entity_b.id) as `key` 

from `entity_b` 

-- Following are few one to many relations to match entity a with b 
inner join `entity_b_function` on 
    `entity_b`.`id` = `entity_b_function`.`entity_b_id` 
inner join `entity_b_category` on 
    `entity_b`.`id` = `entity_b_category`.`entity_b_id` 
inner join `entity_b_scope` on 
    `entity_b`.`id` = `entity_b_scope`.`entity_b_id` 

inner join `entity_a` on 
    `entity_a`.`category_id` = `entity_b_category`.`category_id` and 
    `entity_a`.`scope_id` = `entity_b_scope`.`scope_id` 
inner join `entity_a_function` on 
    `entity_b_function`.`function_id` = `entity_a_function`.`function_id` 


-- pivot of entity a and b 
-- making sure matching entities are finally related in pivot 
left join `entity_a_b_pivot` on 
    `entity_a_b_pivot`.`entity_a_id` = `entity_a`.`id` and 
    `entity_a_b_pivot`.`entity_b_id` = `entity_b`.`id` 

where 
    -- we need only matching entities which are not yet related in pivot 
    `entity_a_b_pivot`.`id` is null and 
    -- when both entities are active in the system 
    `entity_b`.`status` = 1 and 
    `entity_a`.`status` = 1 
LIMIT 5000; 
+0

DISTINCT am Anfang funktionierte wirklich gut, aber Abfrage Ausführungszeit ist 44s im Vergleich zu 0,5s ohne es. :) –

+0

Diese Zeit ist auf 'sort' Operation. 'DISTINCT' wird eine Art machen. –

Verwandte Themen