here have query scenario, explained scope in inline comments:
select -- selecting both entity ids entity_a.id entity_a_id, entity_b.id entity_b_id, concat(entity_a.id, entity_b.id) `key` `entity_b` -- following few 1 many relations match entity 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` , `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 , b -- making sure matching entities related in pivot left join `entity_a_b_pivot` on `entity_a_b_pivot`.`entity_a_id` = `entity_a`.`id` , `entity_a_b_pivot`.`entity_b_id` = `entity_b`.`id` -- need matching entities not yet related in pivot `entity_a_b_pivot`.`id` null , -- when both entities active in system `entity_b`.`status` = 1 , `entity_a`.`status` = 1 limit 5000;
currently resulted below:
(pointed items duplication due joins of 1 many relations)
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
here, if either use group key
or distinct(key)
eliminate duplicates, query processing stuck forever 100% cpu usage without these blink return 5k records 90% duplicates.
how optimize query distinct results?
how add distinct
@ beginning of select list?
select -- selecting both entity ids distinct entity_a.id entity_a_id, entity_b.id entity_b_id, concat(entity_a.id, entity_b.id) `key` `entity_b` -- following few 1 many relations match entity 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` , `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 , b -- making sure matching entities related in pivot left join `entity_a_b_pivot` on `entity_a_b_pivot`.`entity_a_id` = `entity_a`.`id` , `entity_a_b_pivot`.`entity_b_id` = `entity_b`.`id` -- need matching entities not yet related in pivot `entity_a_b_pivot`.`id` null , -- when both entities active in system `entity_b`.`status` = 1 , `entity_a`.`status` = 1 limit 5000;
Comments
Post a Comment