Best way to join on a range?

前端 未结 3 1735
情歌与酒
情歌与酒 2021-01-19 23:30

I think this may be a common problem that may not have an answer for every tool. Right now we are trying to use amazons Redshift. The only problem we have now is we are tryi

3条回答
  •  一生所求
    2021-01-19 23:55

    Beginning with PostgreSQL 9.2 you could use one of the new range types,int4range or int8range.

    CREATE TABLE city (
      city_id serial PRIMARY KEY 
     ,ip_range int4range
     ,city text
     ,zip  text
    );
    

    Then your query could simply be:

    SELECT c.zip
    FROM   city_ip 
    WHERE  $intip <@ i.ip_range;
    

    <@ .. "element is contained by"

    To make this fast for a big table use a GiST index:

    CREATE INDEX city_ip_range_idx ON city USING gist (ip_range);
    

    But I doubt Amazon Redshift is up to date. We had other people with problems recently:
    Using sql function generate_series() in redshift

提交回复
热议问题