title: Inconsistent results when passing array to ActiveRecord#where
body: <p>I'm using Rails 4.2.7.1, Ruby 2.3.1 and MySQL as my DB. I have a background job that is responsible for collecting analytics about the database which performs some very costly read operations. One of them in particular is behaving inconsistently and it seems to be caused by inconsistent behaviour of the IN clause in the SQL query.</p>
  
  <p>The rails calls look something like this:</p>
  
  <pre><code>MyModel.some_scope.where(id: OtherModel.where(&lt;CONDITION&gt;).group(:my_model_id).select(:my_model_id)).count
  </code></pre>
  
  <p>Which boils down to an <code>IN</code> clause in the resulting SQL query. This works fine in general, but when the nested query (<code>OtherModel...</code>) gets large (~2000 results) I start to see inconsistent results from the <code>IN</code>. When running the query over and over again the values I see drift up and down independently of actual changes to the database.</p>
  
  <p>If I re-write the query above to use joins, like so:</p>
  
  <pre><code>MyModel.some_scope.joins(:other_model).where(&lt;CONDITION&gt;).select(:id).uniq.count
  </code></pre>
  
  <p>I see a consistent result from the query every time. I have no problem re-writing the query in this case and, therefore, have a solution to my problem, but I'd really like to understand what's going on in MySQL to cause the strange behaviour.</p>
  
  <p>It's worth noting that the working query (with the join) returns ~1300 records, while the broken query is in the ~1400-1600 range.</p>