mysql批量SELECT有比较条件

mysql bulk SELECT with comparison conditions

我有一个很大的 ip_locations table,行数超过 200 万。给定一个特定的ip,给select一条ip_location记录:

SELECT * FROM ip_locations WHERE ip >= start_ip and ip <= end_ip;

现在的问题是如何在给定一个 ip 数组 [ip1, ip2, ip3, ...]. The size of the batch can be 1000+ 的情况下批量 select 高效地执行此操作。一个简单的方法可以是

SELECT * FROM ip_locations WHERE (
   (ip1 >= start_ip and ip1 <= end_ip) ||
   (ip2 >= start_ip and ip2 <= end_ip) ||
   ....
);

但我相信应该有更优雅、更有效的方法来做到这一点。另外,如何在批处理select结果中获取与相应记录关联的每个ip?

参考@Mihai idea,可以通过以下方式实现:

SELECT 
    ip_locations.* 
FROM 
    ip_locations 
    INNER JOIN  
    (
     SELECT ip1 AS ip_to_check
     UNION 
     SELECT ip2 AS ip_to_check
     ....
     UNION 
     SELECT ipN AS ip_to_check
    ) AS ip_tab
      ON (ip_tab.ip_to_check >= ip_locations.start_ip 
         and ip_tab.ip_to_check <= ip_locations.end_ip)