Created
January 11, 2012 05:16
-
-
Save alindeman/1593170 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| SELECT * FROM ip_ranges | |
| WHERE ip_start <= @new_ip_end AND | |
| @new_ip_start <= ip_end; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| ) | |
| ) | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.