Skip to content

Instantly share code, notes, and snippets.

@alindeman
Created January 11, 2012 05:16
Show Gist options
  • Select an option

  • Save alindeman/1593170 to your computer and use it in GitHub Desktop.

Select an option

Save alindeman/1593170 to your computer and use it in GitHub Desktop.
ALTER TABLE ip_ranges add ip_range POLYGON NULL;
UPDATE ip_ranges
SET ip_range=Polygon(
LineString(
Point(ip_start, 1),
Point(ip_start, 0),
Point(ip_end, 0),
Point(ip_end, 1),
Point(ip_start, 1)
)
);
ALTER TABLE ip_ranges MODIFY ip_range POLYGON NOT NULL;
CREATE SPATIAL INDEX index_ip_ranges_on_ip_range on ip_ranges (ip_range);
SELECT * FROM ip_ranges
WHERE ip_start <= @new_ip_end AND
@new_ip_start <= ip_end;
SELECT * FROM ip_ranges
WHERE Intersects(ip_range,
Polygon(
LineString(
Point(@new_ip_start, 1),
Point(@new_ip_start, 0),
Point(@new_ip_end, 0),
Point(@new_ip_end, 1),
Point(@new_ip_start, 1)
)
)
);
@adam741
Copy link

adam741 commented Nov 22, 2018

Hi,

Thank you for your great tutorial, it really helped me to optimize the queries.
https://www.bignerdranch.com/blog/using-mysql-spatial-extensions-for-range-queries/

I was wondering if we could store the version of the IP (4,6) as a Point in the LineString? Is that possible?

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment