mysql - Queries run fast separately, slowly under UNION -


i had query taking several minutes run. thought might able run faster 4 separate queries, union. @ least right 4 separate queries part. run, slowest fastest, ~7 seconds, ~1 second, < 1 second , < 1 second.

so think union of these run, @ slowest, 7 + 1 + 1 + 1 = 10 seconds. however, when run union query, takes on 100 seconds.

(select id view_macatawa_prospect_match_first_and_last_name) union (select id view_macatawa_prospect_match_full_name) union (select id view_macatawa_prospect_match_just_address) union  (select id view_macatawa_prospect_match_name_and_address) 

why union make these slower? seems me mysql has execute 4 queries concatenate results.

edit: guess didn't ask i'm after: way make query run quickly.

the problem union removes duplicates. it's not running queries.

try using union all instead of union.

that way, duplicates won't removed:

(select id view_macatawa_prospect_match_first_and_last_name) union (select id view_macatawa_prospect_match_full_name) union (select id view_macatawa_prospect_match_just_address) union (select id view_macatawa_prospect_match_name_and_address) 

with union all should times closer you've got when run queries separately.


Comments