Vaibhav
Vaibhav

Reputation: 289

How do i optimize this Union Query

I have a following query

    SELECT `id`,'event' as type FROM `oc_event` where `Owner_id` = 1
    UNION ALL
    SELECT `id` ,'business' as type FROM `oc_business` where `Owner_id` = 1
    UNION ALL
    SELECT `id`, 'parts'  as type FROM `oc_listing` where `Owner_id` = 1

Although, the result is accurate but its quiet slow when the number of rows fetched is around 1K or more.. Is there any kind of mysql keyword to optimize this simple query or do you thing "UNION ALL" is the right way ?

P.S : I am using PDO

Upvotes: 1

Views: 77

Answers (1)

vidyadhar
vidyadhar

Reputation: 3168

if you want to display duplicate data then it is good to use union all other wise use union it will stop when a single row found the union_result will go faster

to make your query better create index on owner_id all the tables

Upvotes: 1

Related Questions