title: querying based on foreign key's value
body: <p>I have these 3 tables:</p>
  <ul>
  <li><code>metric</code> - id, name, machine_id (FK to machine), createdAt</li>
  <li><code>metric_value</code> - timestamp, metric_id (FK to metric), value</li>
  <li><code>machine</code> - id, name, metadata, createdAt</li>
  </ul>
  <p>I want to efficiently query metric_value based on the machine id, so far I have thought of three possible ways of doing it.</p>
  <ol>
  <li><p>A left join i.e.</p>
  <pre><code>SELECT timestamp, machine_id, metric_id, value, metadata 
  FROM metric_value 
  LEFT JOIN metric ON metric_id = metric.id 
  WHERE machine_id = 1;
  </code></pre>
  </li>
  <li><p>Select all the metric ids that correspond to the target machine, then another select</p>
  <p>SELECT ..
  WHERE metric_id = ANY(:metricIds)</p>
  </li>
  <li><p>Add a <code>machine_id</code> column to the metric value</p>
  </li>
  </ol>
  <p>Is there a better way of doing this? I believe</p>
  <ol>
  <li>The join will be slow when there are billions of metric values</li>
  <li>Passing possibly thousands of metric ids in the <code>ANY()</code> will be slow</li>
  <li>Unnecessary and redundant</li>
  </ol>