title: SQLAlchemy and empty IN clause
body: <p>I found out that SQLAlchemy translates</p>
  
  <pre><code>db.query(...).filter(A.id.in_(ids))
  </code></pre>
  
  <p>into</p>
  
  <pre><code>SELECT ...
  FROM a
  WHERE a.id != a.id
  </code></pre>
  
  <p>if <code>ids</code> is empty. That results in sequential scan on <code>a</code> table, which is obviously disastrous for performance.</p>
  
  <p>The first question is: why? Why not just <code>1 = 0</code> or anything that does not require sequential scan?</p>
  
  <p>The second, more important: is is there a commonly used workaround (other than <code>if</code> near every <code>in_</code>)? </p>
  
  <p>I guess that <code>in_</code> cannot be easily reimplemented to cover all cases without causing that problem, but I cannot be the first to face it and there may be some solution covering simple, common use cases of <code>in_</code>.</p>
  
  <h2>EDIT</h2>
  
  <p>SQLAlchemy logs a warning every time it happens:</p>
  
  <p><code>"The IN-predicate on 'foo.bar' was invoked with an empty sequence. This results in a contradiction, which nonetheless can be expensive to evaluate. Consider alternative strategies for improved performance."</code></p>