使用group by或distinct删除重复

时间:2016-03-26 15:12:34

标签: mysql sql join unique

这里我有一个查询场景,解释了内联注释的范围:

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;

目前的结果如下:
(由于一对多关系的连接,指出的项目是重复的)

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

在这里,如果我使用GROUP BY keyDISTINCT(key)来消除重复项,那么查询处理将永远停留在100%的CPU使用率上但没有这些只是眨眼就能返回5K记录但是只有90 %重复。

如何针对不同结果优化查询?

1 个答案:

答案 0 :(得分:1)

如何在选择列表的开头添加DISTINCT呢?

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;