title: How to optimize this "where IN" and "where NOT IN" query?
body: <p>The following query/queries get the cities the user has visited, get the places where the user has visited; and returns the places in those cities where the user hasn't been.</p>
  
  <pre><code>// I get the city_id and object_id. Each vote has the place_id and its city_id.
    SELECT DISTINCT city_id as city_id, object_id as object_id
      FROM vote
     WHERE object_model = 'Place'
       AND user_id = 20
  ORDER BY created_at desc
  
  // I build an array with city_ids and another with object_ids
  $city_ids = array(...);
  $place_ids = array(...);
  </code></pre>
  
  <p>I get the places where the user hasn't been in the cities he has been - 1 second</p>
  
  <pre><code>  SELECT id, title
      FROM place
     WHERE city_id IN ($city_ids)
       AND id NOT IN ($place_ids)
  ORDER BY points desc
     LIMIT 0,20
  </code></pre>
  
  <p>EXPLAIN SQL</p>
  
  <pre><code>select_type table   type    possible_keys           key             key_len   ref    ows     Extra
  -----------------------------------------------------------------------------------------------------------
  SIMPLE      p       range   PRIMARY,city_id_index    city_id_index    9         NULL  33583    Using where; Using filesort
  </code></pre>
  
  <p>Another attempt to optimize is to do it one query using LEFT JOIN / IS NULL and a subquery, but it takes much longer (30+ seconds)</p>
  
  <pre><code>   SELECT id, title 
       FROM place AS p
  LEFT JOIN vote v ON v.object_id = p.id
                  AND v.object_model = 'Place'
                  AND v.user_id = 20
      WHERE p.city_id IN (SELECT city_id 
                            FROM vote 
                           WHERE user_id = 20 
                             AND city_id != 0)
        AND v.id is null
   ORDER BY p.points desc
      LIMIT 0, 20
  </code></pre>
  
  <p>How would you do the query/queries thinking that we can have an array of 500 cities and 1000 places for each user? Which is the best alternative to where in and where NOT IN when there are many ids?</p>