I'm using the excellent Sequel ORM, and at some point I've generated an array of IDs and I'm trying to retrieve the users that have those IDs. e.g.
user_ids = [1, 2, 3]
User.where(id: user_ids).all
# (0.004223s) SELECT * FROM "users" WHERE ("id" IN (1, 2, 3))
But sometimes, the list of users is empty, and what I effectively get is this:
User.where(id: []).all
# (0.421287s) SELECT * FROM "users" WHERE ("id" != "id")
The result is correct (i.e. no rows returned), but the query is two orders of magnitude slower.
On tables with millions of rows, it can take a couple of seconds to return.
I'm curious why this query is generated in the first place, but I'm also curious why the Postgres query planner doesn't seem to detect this contradiction and return an empty dataset instantly.
Is there a neat solution to this, or will I have to check all arrays for emptiness?
I'm using Ruby 2.0.0, Sequel 4.24.0 & Postgres 9.3.6.
allto retrieved all users