Skip to content

Commit

Permalink
Update database in SQL tutorials for is_anycast flag
Browse files Browse the repository at this point in the history
  • Loading branch information
christophermluna committed Dec 11, 2023
1 parent ebfa4cd commit afc3d94
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
37 changes: 20 additions & 17 deletions content/geoip/importing-databases/mysql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ create table geoip2_network (
latitude float,
longitude float,
accuracy_radius int,
is_anycast bool,
index(network_start),
index(network_end)
);
Expand All @@ -116,7 +117,7 @@ load data infile '/var/lib/mysql-files/GeoIP2-City-Blocks-IPv6-Hex.csv'
into table geoip2_network
fields terminated by ',' enclosed by '"' lines terminated by '\n' ignore 1 rows
(@network_start, @network_end, @geoname_id, @registered_country_geoname_id, @represented_country_geoname_id,
@is_anonymous_proxy, @is_satellite_provider, @postal_code, @latitude, @longitude, @accuracy_radius)
@is_anonymous_proxy, @is_satellite_provider, @postal_code, @latitude, @longitude, @accuracy_radius, @is_anycast)
set network_start = unhex(@network_start),
network_end = unhex(@network_end),
geoname_id = nullif(@geoname_id, ''),
Expand All @@ -127,7 +128,8 @@ set network_start = unhex(@network_start),
postal_code = nullif(@postal_code, ''),
latitude = nullif(@latitude, ''),
longitude = nullif(@longitude, ''),
accuracy_radius = nullif(@accuracy_radius, '');
accuracy_radius = nullif(@accuracy_radius, ''),
is_anycast = nullif(@is_anycast, '');
```

We can load the converted IPv4 data in the same way:
Expand All @@ -137,7 +139,7 @@ load data infile '/var/lib/mysql-files/GeoIP2-City-Blocks-IPv4-Hex.csv'
into table geoip2_network
fields terminated by ',' enclosed by '"' lines terminated by '\n' ignore 1 rows
(@network_start, @network_end, @geoname_id, @registered_country_geoname_id, @represented_country_geoname_id,
@is_anonymous_proxy, @is_satellite_provider, @postal_code, @latitude, @longitude, @accuracy_radius)
@is_anonymous_proxy, @is_satellite_provider, @postal_code, @latitude, @longitude, @accuracy_radius, @is_anycast)
set network_start = unhex(@network_start),
network_end = unhex(@network_end),
geoname_id = nullif(@geoname_id, ''),
Expand All @@ -148,7 +150,8 @@ set network_start = unhex(@network_start),
postal_code = nullif(@postal_code, ''),
latitude = nullif(@latitude, ''),
longitude = nullif(@longitude, ''),
accuracy_radius = nullif(@accuracy_radius, '');
accuracy_radius = nullif(@accuracy_radius, ''),
is_anycast = nullif(@is_anycast, '');
```

### Test our table by querying it
Expand All @@ -167,11 +170,11 @@ limit 1;
```

```
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
| geoname_id | registered_country_geoname_id | represented_country_geoname_id | postal_code | latitude | longitude | accuracy_radius |
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
| 4930956 | 6252001 | NULL | 02118 | 42.3388 | -71.0726 | 100 |
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+------------+
| geoname_id | registered_country_geoname_id | represented_country_geoname_id | postal_code | latitude | longitude | accuracy_radius | is_anycast |
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+------------+
| 4930956 | 6252001 | NULL | 02118 | 42.3388 | -71.0726 | 100 | NULL |
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+------------+
1 row in set (6.16 sec)
```

Expand Down Expand Up @@ -207,7 +210,7 @@ database:

```sql
select geoname_id, registered_country_geoname_id, represented_country_geoname_id,
postal_code, latitude, longitude, accuracy_radius
postal_code, latitude, longitude, accuracy_radius, is_anycast
from geoip2_network
where inet6_aton('127.0.0.1') between network_start and network_end
order by network_end
Expand All @@ -225,7 +228,7 @@ that MySQL will be able to use the indexes we've created more efficiently:

```sql
select geoname_id, registered_country_geoname_id, represented_country_geoname_id,
postal_code, latitude, longitude, accuracy_radius
postal_code, latitude, longitude, accuracy_radius, is_anycast
from (
select *
from geoip2_network
Expand All @@ -237,17 +240,17 @@ where inet6_aton('146.243.121.22') <= network_end
```

```
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
| geoname_id | registered_country_geoname_id | represented_country_geoname_id | postal_code | latitude | longitude | accuracy_radius |
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
| 4930956 | 6252001 | NULL | 02118 | 42.3388 | -71.0726 | 100 |
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+------------+
| geoname_id | registered_country_geoname_id | represented_country_geoname_id | postal_code | latitude | longitude | accuracy_radius | is_anycast |
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+------------+
| 4930956 | 6252001 | NULL | 02118 | 42.3388 | -71.0726 | 100 | NULL |
+------------+-------------------------------+--------------------------------+-------------+----------+-----------+-----------------+------------+
1 row in set (0.00 sec)
```

```sql
select geoname_id, registered_country_geoname_id, represented_country_geoname_id,
postal_code, latitude, longitude, accuracy_radius
postal_code, latitude, longitude, accuracy_radius, is_anycast
from (
select *
from geoip2_network
Expand Down
9 changes: 6 additions & 3 deletions content/geoip/importing-databases/postgresql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ create table geoip2_network (
postal_code text,
latitude numeric,
longitude numeric,
accuracy_radius int
accuracy_radius int,
is_anycast bool
);
```

Expand All @@ -87,14 +88,14 @@ We can now import the contents of `GeoIP2-City-Blocks-IPv4.csv` and
```sql
\copy geoip2_network(
network, geoname_id, registered_country_geoname_id, represented_country_geoname_id,
is_anonymous_proxy, is_satellite_provider, postal_code, latitude, longitude, accuracy_radius
is_anonymous_proxy, is_satellite_provider, postal_code, latitude, longitude, accuracy_radius, is_anycast
) from 'GeoIP2-City-Blocks-IPv4.csv' with (format csv, header);
```

```sql
\copy geoip2_network(
network, geoname_id, registered_country_geoname_id, represented_country_geoname_id,
is_anonymous_proxy, is_satellite_provider, postal_code, latitude, longitude, accuracy_radius
is_anonymous_proxy, is_satellite_provider, postal_code, latitude, longitude, accuracy_radius, is_anycast
) from 'GeoIP2-City-Blocks-IPv6.csv' with (format csv, header);
```

Expand Down Expand Up @@ -134,6 +135,7 @@ postal_code | 02118
latitude | 42.3388
longitude | -71.0726
accuracy_radius | 100
is_anycast |
Time: 514.003 ms
```
Expand Down Expand Up @@ -173,6 +175,7 @@ postal_code | 02118
latitude | 42.3388
longitude | -71.0726
accuracy_radius | 100
is_anycast |
Time: 9.671 ms
```
Expand Down

0 comments on commit afc3d94

Please sign in to comment.