Advertisement
khanhtran3005

union vs or

Aug 14th, 2020 (edited)
2,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --- Requirement: Select all bookings that are shared to factory_id = 194592 or supplier_id = 201670 ---
  2.  
  3. --- Using OR condition ---
  4. explain analyze
  5. select count(*) from inspections_booking ib left outer join inspections_customorgcontactlocation f on ib.factory_information_id = f.id
  6. left outer join inspections_customorgcontactlocation s on ib.supplier_information_id = s.id
  7. where f.org_id = 194592 or s.org_id = 201670
  8.  
  9.  
  10. --- Using UNION ---
  11. explain analyze
  12. select count(*) from (
  13. select ib.* from inspections_booking ib inner join inspections_customorgcontactlocation f on ib.factory_information_id = f.id where f.org_id = 194592
  14. union
  15. select ib.* from inspections_booking ib inner join inspections_customorgcontactlocation s on ib.supplier_information_id = s.id where s.org_id = 201670
  16. ) as ib
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement