title: SQL schema optimization, how to avoid unions?
body: <p>Imagine that one have the following schema:</p>
  
  <p><img src="https://i.stack.imgur.com/cBg5l.png" alt="SQL Schema"></p>
  
  <p>Now in this schema it will have 10000 fridges, 10000 televisions and 10000 computers. Then it will have 5000 spares for each.
  To get all spares related to computers, one would query like this:</p>
  
  <pre><code>SELECT SP.name, C.name as computer
  FROM spare_parts SP, computer_spare_part CSP, computers C
  WHERE   CSP.spare_parts_id = SP.id
  AND     CSP.computers_id = C.id
  </code></pre>
  
  <p>Now with a given spare ID = 1, one wants to get the details and the related object(fridge, tv, computer).</p>
  
  <pre><code>SELECT SP.name, F.name as equipment
  FROM spare_parts SP, fridge_spare_part FSP, fridges F
  WHERE   FSP.spare_parts_id = SP.id
  AND     FSP.fridges_id = F.id
  AND     SP.id = 1
  
  UNION
  
  SELECT SP.name, C.name as equipment
  FROM spare_parts SP, computer_spare_part CSP, computers C
  WHERE   CSP.spare_parts_id = SP.id
  AND     CSP.computers_id = C.id
  AND     SP.id = 1
  
  UNION
  
  SELECT SP.name, T.name as equipment
  FROM spare_parts SP, television_spare_part TSP, television T
  WHERE   TSP.spare_parts_id = SP.id
  AND     TSP.televisions_id = C.id
  AND     SP.id = 1
  </code></pre>
  
  <p>In this case there are 2 SELECTS that are going to fail. Imagine that spare ID = 1 is related to a computer. The fridge and television SELECT would not get any result. </p>
  
  <p>This will have extra processing time.</p>
  
  <p>How could one optimize this query or schema to solve this kind of problem?</p>