SlideShare a Scribd company logo
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7 GIS(地理情報システム)
Yoshiaki Yamasaki / 山﨑 由章
MySQL Senior Sales Consultant, Asia Pacific and Japan
updated: 2017/07/14
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Safe Harbor Statement
以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。
また、情報提供を唯一の目的とするものであり、いかなる契約にも組み込むことはでき
ません。以下の事項は、マテリアルやコード、機能を提供することをコミットメントするも
のではない為、購買決定を行う際の判断材料になさらないで下さい。
オラクル製品に関して記載されている機能の開発、リリースおよび時期については、
弊社の裁量により決定されます。
2
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
GISとは?
3
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
GISとは?
• GIS(Geographic Information System:地理情報システム)とは、
位置や空間に関する様々な情報をコンピュータ上で扱うシステムのこと
• 世界標準を推進する団体としてOGC(Open Geospatial Consortium)があり、
様々な規格が定められている
– 例:WKT(Well-Known Text) ⇒ ジオメトリデータを扱うためのフォーマット
• MySQLでは、OGCの規格に則った機能が利用できる
4
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MySQLのGIS機能の歴史
5
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
要点
• MySQL 5.7では、GIS関連機能が刷新され、InnoDBで以下の機能が
利用できるようになった
– GEOMETRYデータ型
– 空間インデックス(Rツリーインデックス)
– ST_Contains()、ST_Distance()などのSpatial関数
– GeoHash関数
– GeoJSON関数
• MySQL 5.7で、ジオメトリ同士の関係を比較する関数について、
プレフィックスが付かない関数は今後廃止予定となった
– Contains()、Distance()、など
6
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 7
MySQLの歴史
4.0
全文検索 (MyISAM)
複数テーブルUPDATE/DELETE
組み込みライブラリ型サーバ
OracleMySQL Sun
3.23
MyISAM
InnoDB
レプリケーション
5.1
プラグガブル・
ストレージエンジン・
アーキテクチャ
パーティショニング
タスクスケジューラ
5.6
全文検索(InnoDB)
memcached API
UNDO表領域
Global Transaction ID
マルチスレッドスレーブ
オンラインALTER TABLE
トランスポータブル表領域
5.5
InnoDBがデフォルトに
準同期型レプリケーション
PERFORMANCE_SCHEMA
1.0-3.22以前
ストレージエンジン (ISAM, HEAP)
マルチスレッド
Windows対応/64bit対応
日本語文字コード (SJIS/UJIS)
5.0
ストアドプロシージャ
ストアドファンクション
カーソル/トリガ/ビュー
XAトランザクション
INFORMATION_SCHEMA
4.1
GIS(MyISAM)
Unicode対応
サブクエリ
CSV, ARCHIVE
ndbcluster
1995 2000 2005 2010 2015
5.7
2015年10月21日 GA
全文検索CJK対応/GIS (InnoDB)
新コストモデル オプティマイザ
ロスレス レプリケーション
マルチソース レプリケーション
グループ レプリケーション
セキュリティ強化
データディクショナリ
NoSQLオプション
・ 1995年:スウェーデンにてMySQL AB設立
・(2005年:オラクルがInnobase Oyを買収)
・ 2008年:Sun MicrosystemsがMySQL ABを買収
・ 2010年:オラクルがSun Microsystemsを買収
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
GIS機能の歴史
• MySQL 4.1
– MyISAMでのみGIS機能が使えるようになった
• geometryデータ型
• Spatialインデックス
• Spatial関数
• MySQL 5.0
– InnoDBでもgeometryデータ型、Spatial関数を扱えるようになった
• Spatialインデックスは使用できず
※地理情報システムに関するオープンな規格であるOpenGISに基づいた実装が
されていたが、MySQL開発チームによる独自実装であった
8
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
GIS機能の歴史
• MySQL 5.7
– Boost.GeometryというC++のオープンソースライブラリを採用して独自実装をやめ、
InnoDBでGIS関連機能を再実装した
• geometryデータ型
• Spatialインデックス
• Spatial関数(使える関数の種類も増加)
• GeoHashサポート
• GeoJSONサポート
※Boost.Geometryコミュニティとも活発に交流し、MySQLチームから
Boost.Geometryへのコントリビュートも行っている
9
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
GIS機能の歴史
• MySQL 5.7
– 挙動が不明確で分かり難いSpatial関数は非推奨となった
• 例:Contains()は、以下のどちらの関数と同じ動きをするか分かり難いので廃止予定となった
– MBRContains
– ST_Contains()
– SRIDについては、現時点で利用できる機能は限られている
• SRID(Spatial Reference ID:空間参照ID):特定の測地系、座標系に関連付けられた一意なID
10
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
GIS機能の歴史
• 現在開発中のMySQL 8.0では、引き続きGIS機能の拡張を実施中
– OpenGIS標準準拠
– 演算、データ変換に役立つ各種のSpatial関数
• st_x(geom, x)
• st_y(geom, y)
• st_srid(geom, srid)
– MySQL 5.7で非推奨になった関数の廃止
–Geography サポート
• st_distance() 関数
–Spatial Reference Systems (SRS) サポート
11
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
GIS機能の紹介
12
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
geometryデータ型
• OpenGISジオメトリモデルに基づいた以下のデータが使用可能
– GEOMETRY、GEOMETRYCOLLECTION
– 点:POINT、MULTIPOINT
– 線:LINESTRING、MULTILINESTRING
– 多角形:POLYGON、MULTIPOLYGON
※GEOMETRYには、POINT、LINESTRING、POLYGONを格納可能
※GEOMETRYCOLLECTIONには、MULTIPOINT、MULTILINESTRING、MULTIPOLYGONを
格納可能
13
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
補足:MySQL 5.7で扱えるのはベクターデータ
• ベクター形式
– 点、線、面(ポリゴン)で表現される
– 一般的にデータベースで扱われるデータ形式
– .Shp, .OSM, .KML, .GeoJSON, など
• ラスター形式
– セルの塗りつぶしで表現される
– 人工衛星、イメージファイルなどで使用される
データ形式
– .tiff, .jpg, .gif, など
14
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Spatialインデックス
• MBR(Minimum Bounding Rectangle:最小外接矩形)に基づく
Rツリーインデックス
15
(0,0) (0,1) (0,2)
(1,0) (1,1) (1,2)
(2,0) (2,1) (2,2)
GEOMETRY型の図形
MBR
(隣接する最小の長方形)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Spatialインデックス
• Rツリーインデックスでは、MBR同士の
抱合関係を保持
• MBRContains()、MBRWithin()、
ST_Contains()、ST_Within()などの
関数で利用される
– ST_関数の場合は、最初のスクリーニング
ステップでRツリーインデックスが使用され、
その後正確な形に基づいた判定がされる
• 現時点では2次元データのみをサポート
16
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
補足:MBRとST_の違い(例:Contains関数)
• MBRContains(g1,g2)
– g1の最小外接矩形にg2の最小外接矩形が含まれているかどうかを示す
• ST_Contains(g1,g2)
– g1にg2が含まれているかどうかを示す
17
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Spatial関数
• ジオメトリデータを扱える各種の関数
• 例
– ST_Distance()
• 2つのジオメトリを入力し、2地点間の距離を出力
(単位:角度)
– ST_Distance_Sphere()
• 2つのジオメトリを入力し、2地点間の距離を地球を球体であると仮定して計算して出力
(単位:メートル)
– ST_Contains()
• 前ページ参照
18
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
GeoHash
• 経緯度の情報を文字列化したもの
– 例:青山OMセンター(経度:139.718490、緯度:35.671497)のGeoHash
⇒ xn76gmgcsc10hf7v664d8ebpbpbpbp<<後略>>
• 特徴
– 文字列なので、データベースで扱いやすい
(インデックスも活用可能)
– GeoHashの情報は点ではなく範囲(矩形のエリア)を表す
– 文字列の桁数で精度を変えることができる
• 10桁を超えると矩形の一辺が1m以下になるので、経緯度の変わりとしても十分利用可能
• 近接エリアの判定等に利用可能
19
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
GeoHash
• MySQL 5.7では、GeoHashを扱うための関数が使用できる
– ST_GeoHash():経度、緯度(POINT型のデータでも可)を入力し、GeoHashを出力
– ST_LatFromGeoHash():GeoHashを入力し、経度を出力
– ST_LongFromGeoHash():GeoHashを入力し、緯度を出力
– ST_PointFromGeoHash():GeoHashを入力し、POINT型のデータを出力
20
※詳細を解説しているマニュアル
MySQL 5.7 Reference Manual:13.15.10 Spatial Geohash Functions
https://dev.mysql.com/doc/refman/5.7/en/spatial-geohash-functions.html#function_st-geohash
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
GeoJSON
• 空間データを扱うためのフォーマットの一つ
• JSONによる表現で空間データを扱える
– 例:青山OMセンター(経度:139.718490、緯度:35.671497)のGeoJSONによる表現
⇒ {"type": "Point", "coordinates": [139.71849, 35.671497]}
21
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
GeoJSON
• MySQL 5.7では、GeoJSONを扱うための関数が使用できる
– ST_AsGeoJSON():ジオメトリ型のデータを入力し、GeoJSONデータを出力
– ST_GeomFromGeoJSON():GeoJSONデータを入力し、ジオメトリ型のデータを出力
22
※詳細を解説しているマニュアル
MySQL 5.7 Reference Manual:13.15.11 Spatial GeoJSON Functions
https://dev.mysql.com/doc/refman/5.7/en/spatial-geojson-functions.html
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MySQL Workbenchの活用
23
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MySQL Workbench
• 管理ツール
• SQLエディタ
• Performance Dashboard
• Visual Explain
• GIS Viewer
• ER図作成、フォワード/
リバースエンジニアリング、
など豊富な機能
24
データベースアーキテクト、開発者、DBA のための統合ビジュアルツール
商用版のみの機能:
DBドキュメント出力、データモデルの検証、
MySQL Enterprise Backup GUI、MySQL Enterprise Audit GUI
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MySQL Workbench – 起動後の画面
• クライアントPCにインストールし、
MySQLデータベースへの
接続定義を作成して接続
25
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MySQL Workbench – 管理
• サーバーステータス
• クライアントコネクション
一覧
• ユーザー管理
• データDump/Import
• サーバー起動/停止
• パフォーマンスレポート
• パフォーマンススキーマの
設定変更、など
26
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MySQL Workbench – 開発支援:SQL Editor
• SQLエディタ
– キーワードハイライト
– SQL整形
– スニペット、など
• Visual Explain
• オブジェクト確認
• データ確認/編集
• オブジェクト作成、など
27
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MySQL Workbench – モデリング
• ER図作成
• フォワードエンジニアリング
• リバースエンジニアリング
• ドキュメント出力(商用版のみ)
• データモデルの検証
(商用版のみ)
• データモデルとDBの同期、
など
28
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
MySQL Workbench – データベース・マイグレーション・ウィザード
• 移行元
– Microsoft SQL Server, PostgreSQL,
Sybase ASE, Sybase SQL Anywhere,
SQLite, Microsoft Access, MySQL, and more
• オブジェクトとデータの移行
• MySQLバージョンアップグレード
(テスト環境用)
29
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
"Spatial Viewer" and "Geometry Viewer"
• ジオメトリ型のデータを図示可能
30
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Appendix:Some Real World Examples
31
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
A Starting Point
• My old apartment in
Brooklyn, NY
– 33 Withers Street
Brooklyn, NY 11211
– POINT(<LONG>,<LAT>)
• -73.951353,40.716914
32
https://www.google.com/maps/place/33+Withers+St,+Brooklyn,+NY+11211/@40.7169144,-73.9513538
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
The Application Use Case
• I’m hungry and in the mood for Thai food
– What Thai restaurants are around me?
– What’s the closest one?
– Can I see the menu, contact info, yelp ratings, etc.?
– How would I get there?
33
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Getting Some Data In
• Download a NYC OSM extract:
– http://osm-extracted-metros.s3.amazonaws.com/new-york.osm.bz2
• Import the data using a customized OsmDB.pm Perl module
– http://wiki.openstreetmap.org/wiki/OsmDB.pm (original)
– https://www.dropbox.com/s/l17vj3wf9y13tee/osmdb-scripts.tar.gz (customized)
• Creates a Geometry column named ‘geom’
• Creates a spatial index on the ‘geom’ column
34
mysql -e "create database nyosm"
bunzip2 new-york.osm.bz2
./bulkDB.pl new-york.osm nyosm
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
The Generated Schema
• http://wiki.openstreetmap.org/wiki/Elements
35
mysql> show tables;
+-----------------+
| Tables_in_nyosm |
+-----------------+
| nodes |
| nodetags |
| relationmembers |
| relations |
| relationtags |
| waynodes |
| ways |
| waytags |
+-----------------+
– We’ll focus on nodes and nodetags for our
examples
– Nodes
• A point or location
– Nodetags
• Metadata about each location
• X name/value pairs
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
De-normalizing the Tag Data
• Greatly simplify our query
• Allow for the use of a full-text index
– Also improves performance
• Mimic better schema created by osm2pgsql
– http://wiki.openstreetmap.org/wiki/Osm2pgsql/schema#planet_osm_nodes
36
mysql> alter table nodes add column tags text, add fulltext index(tags);
mysql> update nodes set tags=(SELECT group_concat(concat(k, "=", v) SEPARATOR
';') from nodetags where nodetags.id=nodes.id group by nodes.id);
* Source: ESRI
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Final Nodes Table
37
mysql> show create table nodes¥G
*************************** 1. row ***************************
Table: nodes
Create Table: CREATE TABLE `nodes` (
`id` bigint(20) DEFAULT NULL,
`geom` geometry NOT NULL,
`user` varchar(50) DEFAULT NULL,
`version` int(11) DEFAULT NULL,
`timestamp` varchar(20) DEFAULT NULL,
`uid` int(11) DEFAULT NULL,
`changeset` int(11) DEFAULT NULL,
`tags` text,
UNIQUE KEY `i_nodeids` (`id`),
SPATIAL KEY `i_geomidx` (`geom`),
FULLTEXT KEY `tags` (`tags`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Creating a Distance Calculation Function
• Various great circle (orthodrome) distance formulas
– Haversine, Spherical Law of Cosines (my choice), …
– http://en.wikipedia.org/wiki/Great-circle_distance
– Necessary for calculating distances between two Geometries
• Need goes away when we support Geography and/or Projections (ST_Transform)
38
mysql> CREATE FUNCTION slc (lat1 double, lon1 double, lat2 double, lon2 double)
RETURNS double
RETURN 6371 * acos(cos(radians(lat1)) * cos(radians(lat2)) * cos(radians(lon2)
- radians(lon1)) + sin(radians(lat1)) * sin(radians(lat2)));
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Creating a Bounding Box For Our Search
• Utilize the r-tree index by limiting area
– Easy with future spatial reference systems support
• WGS84 or SRID 4326 being the most common
– Need to use some additional geographic formulas
• http://www.movable-type.co.uk/scripts/latlong.html
• Need should go away with full SRID support
39
${origlon} = -73.951368
${origlat} = 40.716743
${lon1} = ${origlon} + (${distance_in_km}/abs(cos(radians({$origlat}))*111))
${lat1} = ${origlat} + (${distance_in_km}/111)
${lon2} = ${origlon} - (${distance_in_km}/abs(cos(radians({$origlat}))*111))
${lat2} = ${origlat} - (${distance_in_km}/111)
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Calculating the Results
• Our final query, searching within ~ 10km radius
40
mysql> SELECT id,
slc(40.716743, -73.951368, y(geom), x(geom))*1000 as distance_in_meters,
tags, ST_AsText(geom)
FROM nodes
WHERE ST_Contains(ST_Envelope(linestring(point((-73.951368+(10/111)),
(40.716743+(10/111))), point((-73.951368-(10/111)), (40.716743-(10/111))))),
geom)
AND match(tags) against ("+thai +restaurant" IN BOOLEAN MODE)
ORDER BY distance_in_meters¥G
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Examining the Results
41
*************************** 1. row ***************************
id: 888976948
distance_in_meters: 614.4973960877276
tags: addr:street=Bedford Avenue;amenity=restaurant;name=Tai
Thai;addr:housenumber=206;phone=7185995556
astext(geom): POINT(-73.958637 40.717174)
*************************** 2. row ***************************
id: 2178443635
distance_in_meters: 2780.87697408101
tags: microbrewery=no;website=http://www.onemorethai.net/;name=One
More Thai;amenity=restaurant;opening_hours=12:00-22:30;cuisine=thai;phone=(212)
228-8858
astext(geom): POINT(-73.983871 40.7210541)
*************************** 3. row ***************************
…
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Mapping the Results
42
• From my old place
– -73.951353,40.716914
• To Tai Thai
– -73.958637,40.717174
• Maps APIs
– Google, Bing, Apple, …
https://www.google.com/maps/dir/40.716914,+-73.951353/40.717174,+-73.958637
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
Appendix:参考情報
43
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
参考情報
• マニュアル
– http://dev.mysql.com/doc/refman/5.7/en/spatial-extensions.html
• コミュニティフォーラム
– http://forums.mysql.com/list.php?23
• Boost.Geometry
– http://www.boost.org/libs/geometry
• バグ報告&機能追加リクエスト
– http://bugs.mysql.com/
44
Copyright © 2016, Oracle and/or its affiliates. All rights reserved.
参考情報
• MySQL 5.7 and GIS, an Example
http://mysqlserverteam.com/mysql-5-7-and-gis-an-example/
• Importing Raster Based Spatial Data into MySQL 5.7
http://mysqlserverteam.com/importing-raster-based-spatial-data-into-
mysql-5-7/
• MySQL GIS: Boost.Geometry
– http://mysqlserverteam.com/making-use-of-boost-geometry-in-mysql-gis/
– http://mysqlserverteam.com/why-boost-geometry-in-mysql/
– http://mysqlserverteam.com/building-mysql-with-boost/
• MySQL GIS: InnoDB R-Tree
– http://mysqlserverteam.com/innodb-spatial-indexes-in-5-7-4-lab-release/
45
Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 46
20170714_MySQL 5.7 GIS(地理情報システム) by 日本オラクル株式会社 MySQL GBU 山﨑由章

More Related Content

PDF
Where狙いのキー、order by狙いのキー
PDF
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
PDF
Rustに触れて私のPythonはどう変わったか
PDF
何となく勉強した気分になれるパーサ入門
PDF
MySQLで論理削除と正しく付き合う方法
PDF
イミュータブルデータモデル(入門編)
PPTX
マスタデータの管理と運用について
PDF
マイクロにしすぎた結果がこれだよ!
Where狙いのキー、order by狙いのキー
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
Rustに触れて私のPythonはどう変わったか
何となく勉強した気分になれるパーサ入門
MySQLで論理削除と正しく付き合う方法
イミュータブルデータモデル(入門編)
マスタデータの管理と運用について
マイクロにしすぎた結果がこれだよ!

What's hot (20)

PDF
MongoDBの脆弱性診断 - smarttechgeeks
PDF
ソフトウェア開発における『知の高速道路』
PDF
大規模ソーシャルゲームを支える技術~PHP+MySQLを使った高負荷対策~
PPTX
SQLチューニング入門 入門編
KEY
ラムダ計算入門
PDF
文字コードに起因する脆弱性とその対策(増補版)
PPT
UnicodeによるXSSと SQLインジェクションの可能性
PPTX
モノリスからマイクロサービスへの移行 ~ストラングラーパターンの検証~(Spring Fest 2020講演資料)
PPTX
ちゃんとした C# プログラムを書けるようになる実践的な方法~ Visual Studio を使った 高品質・低コスト・保守性の高い開発
PDF
『データ解析におけるプライバシー保護』勉強会 秘密計算
PDF
Twitterのsnowflakeについて
PDF
組み込み関数(intrinsic)によるSIMD入門
PPTX
やってはいけない空振りDelete
PDF
Confluenceショートカットキー表 v1
PPTX
LINEのMySQL運用について 修正版
PDF
130821 owasp zed attack proxyをぶん回せ
PDF
SQLアンチパターン - 開発者を待ち受ける25の落とし穴 (拡大版)
PDF
ソフトウェアにおける 複雑さとは何なのか?
PDF
PostgreSQLアンチパターン
PDF
忙しい人の5分で分かるMesos入門 - Mesos って何だ?
MongoDBの脆弱性診断 - smarttechgeeks
ソフトウェア開発における『知の高速道路』
大規模ソーシャルゲームを支える技術~PHP+MySQLを使った高負荷対策~
SQLチューニング入門 入門編
ラムダ計算入門
文字コードに起因する脆弱性とその対策(増補版)
UnicodeによるXSSと SQLインジェクションの可能性
モノリスからマイクロサービスへの移行 ~ストラングラーパターンの検証~(Spring Fest 2020講演資料)
ちゃんとした C# プログラムを書けるようになる実践的な方法~ Visual Studio を使った 高品質・低コスト・保守性の高い開発
『データ解析におけるプライバシー保護』勉強会 秘密計算
Twitterのsnowflakeについて
組み込み関数(intrinsic)によるSIMD入門
やってはいけない空振りDelete
Confluenceショートカットキー表 v1
LINEのMySQL運用について 修正版
130821 owasp zed attack proxyをぶん回せ
SQLアンチパターン - 開発者を待ち受ける25の落とし穴 (拡大版)
ソフトウェアにおける 複雑さとは何なのか?
PostgreSQLアンチパターン
忙しい人の5分で分かるMesos入門 - Mesos って何だ?
Ad

Similar to 20170714_MySQL 5.7 GIS(地理情報システム) by 日本オラクル株式会社 MySQL GBU 山﨑由章 (20)

PPTX
[OSC 2017 Tokyo/Fall] OSSコンソーシアム DB部会 MySQL 8.0
PDF
MySQL 8.0で強化されたGIS機能のご紹介:「FOSS4G 2018 Hokkaido」での発表資料
PDF
Oracle GoldenGate入門
PDF
Oracle Spatial 概要説明資料
PDF
20170714_MySQLドキュメントストア JSONデータ型&JSON関数 by 日本オラクル株式会社 MySQL GBU 山﨑由章
PDF
Windows環境でのMySQL
PDF
D22 目覚めよDBエンジニア 〜世界最速カラムナーデータベースは本物だ!〜 by Koji Shinkubo
PPTX
オラクルGo! 位置情報アプリをクラウドで簡単に作ってみた
PDF
オープンソース・データベースの最新事情
PDF
MySQLの公式GUIツール MySQL Workbench
PDF
ついにリリース!! MySQL 8.0 最新情報
PPTX
[中国地方DB勉強会] 第22回 Webアプリ開発をデータベース側から変革していく - MySQL 8.0新機能
PDF
[D37]MySQLの真のイノベーションはこれだ!MySQL 5.7と「実験室」 by Ryusuke Kajiyama
PDF
MySQLドキュメントストアとCTE
PDF
MySQL 8.0で強化されたGIS機能のご紹介と周辺ツールとの連携について
PPTX
MySQL Technology Cafe #14 MySQL Shellを使ってもっと楽をしようの会
PDF
MySQL 5.7 InnoDB 日本語全文検索
PDF
A13 MySQL & NoSQL~Best of both world~ by Philip Antoniades & Ryusuke Kajiyama
PDF
ITアーキテクトのためのOracle Cloud Platform設計・構築入門 [Oracle Cloud Days Tokyo 2016]
PDF
Oracle GoldenGate Veridata概要
[OSC 2017 Tokyo/Fall] OSSコンソーシアム DB部会 MySQL 8.0
MySQL 8.0で強化されたGIS機能のご紹介:「FOSS4G 2018 Hokkaido」での発表資料
Oracle GoldenGate入門
Oracle Spatial 概要説明資料
20170714_MySQLドキュメントストア JSONデータ型&JSON関数 by 日本オラクル株式会社 MySQL GBU 山﨑由章
Windows環境でのMySQL
D22 目覚めよDBエンジニア 〜世界最速カラムナーデータベースは本物だ!〜 by Koji Shinkubo
オラクルGo! 位置情報アプリをクラウドで簡単に作ってみた
オープンソース・データベースの最新事情
MySQLの公式GUIツール MySQL Workbench
ついにリリース!! MySQL 8.0 最新情報
[中国地方DB勉強会] 第22回 Webアプリ開発をデータベース側から変革していく - MySQL 8.0新機能
[D37]MySQLの真のイノベーションはこれだ!MySQL 5.7と「実験室」 by Ryusuke Kajiyama
MySQLドキュメントストアとCTE
MySQL 8.0で強化されたGIS機能のご紹介と周辺ツールとの連携について
MySQL Technology Cafe #14 MySQL Shellを使ってもっと楽をしようの会
MySQL 5.7 InnoDB 日本語全文検索
A13 MySQL & NoSQL~Best of both world~ by Philip Antoniades & Ryusuke Kajiyama
ITアーキテクトのためのOracle Cloud Platform設計・構築入門 [Oracle Cloud Days Tokyo 2016]
Oracle GoldenGate Veridata概要
Ad

More from Insight Technology, Inc. (20)

PDF
グラフデータベースは如何に自然言語を理解するか?
PDF
Docker and the Oracle Database
PDF
Great performance at scale~次期PostgreSQL12のパーティショニング性能の実力に迫る~
PDF
事例を通じて機械学習とは何かを説明する
PDF
仮想通貨ウォレットアプリで理解するデータストアとしてのブロックチェーン
PDF
MBAAで覚えるDBREの大事なおしごと
PDF
グラフデータベースは如何に自然言語を理解するか?
PDF
DBREから始めるデータベースプラットフォーム
PDF
SQL Server エンジニアのためのコンテナ入門
PDF
Lunch & Learn, AWS NoSQL Services
PDF
db tech showcase2019オープニングセッション @ 森田 俊哉
PDF
db tech showcase2019 オープニングセッション @ 石川 雅也
PDF
db tech showcase2019 オープニングセッション @ マイナー・アレン・パーカー
PPTX
難しいアプリケーション移行、手軽に試してみませんか?
PPTX
Attunityのソリューションと異種データベース・クラウド移行事例のご紹介
PPTX
そのデータベース、クラウドで使ってみませんか?
PPTX
コモディティサーバー3台で作る高速処理 “ハイパー・コンバージド・データベース・インフラストラクチャー(HCDI)” システム『Insight Qube』...
PDF
複数DBのバックアップ・切り戻し運用手順が異なって大変?!運用性の大幅改善、その先に。。
PPTX
Attunity社のソリューションの日本国内外適用事例及びロードマップ紹介[ATTUNITY & インサイトテクノロジー IoT / Big Data フ...
PPTX
レガシーに埋もれたデータをリアルタイムでクラウドへ [ATTUNITY & インサイトテクノロジー IoT / Big Data フォーラム 2018]
グラフデータベースは如何に自然言語を理解するか?
Docker and the Oracle Database
Great performance at scale~次期PostgreSQL12のパーティショニング性能の実力に迫る~
事例を通じて機械学習とは何かを説明する
仮想通貨ウォレットアプリで理解するデータストアとしてのブロックチェーン
MBAAで覚えるDBREの大事なおしごと
グラフデータベースは如何に自然言語を理解するか?
DBREから始めるデータベースプラットフォーム
SQL Server エンジニアのためのコンテナ入門
Lunch & Learn, AWS NoSQL Services
db tech showcase2019オープニングセッション @ 森田 俊哉
db tech showcase2019 オープニングセッション @ 石川 雅也
db tech showcase2019 オープニングセッション @ マイナー・アレン・パーカー
難しいアプリケーション移行、手軽に試してみませんか?
Attunityのソリューションと異種データベース・クラウド移行事例のご紹介
そのデータベース、クラウドで使ってみませんか?
コモディティサーバー3台で作る高速処理 “ハイパー・コンバージド・データベース・インフラストラクチャー(HCDI)” システム『Insight Qube』...
複数DBのバックアップ・切り戻し運用手順が異なって大変?!運用性の大幅改善、その先に。。
Attunity社のソリューションの日本国内外適用事例及びロードマップ紹介[ATTUNITY & インサイトテクノロジー IoT / Big Data フ...
レガシーに埋もれたデータをリアルタイムでクラウドへ [ATTUNITY & インサイトテクノロジー IoT / Big Data フォーラム 2018]

20170714_MySQL 5.7 GIS(地理情報システム) by 日本オラクル株式会社 MySQL GBU 山﨑由章

  • 1. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. | MySQL 5.7 GIS(地理情報システム) Yoshiaki Yamasaki / 山﨑 由章 MySQL Senior Sales Consultant, Asia Pacific and Japan updated: 2017/07/14
  • 2. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Safe Harbor Statement 以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。 また、情報提供を唯一の目的とするものであり、いかなる契約にも組み込むことはでき ません。以下の事項は、マテリアルやコード、機能を提供することをコミットメントするも のではない為、購買決定を行う際の判断材料になさらないで下さい。 オラクル製品に関して記載されている機能の開発、リリースおよび時期については、 弊社の裁量により決定されます。 2
  • 3. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. GISとは? 3
  • 4. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. GISとは? • GIS(Geographic Information System:地理情報システム)とは、 位置や空間に関する様々な情報をコンピュータ上で扱うシステムのこと • 世界標準を推進する団体としてOGC(Open Geospatial Consortium)があり、 様々な規格が定められている – 例:WKT(Well-Known Text) ⇒ ジオメトリデータを扱うためのフォーマット • MySQLでは、OGCの規格に則った機能が利用できる 4
  • 5. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MySQLのGIS機能の歴史 5
  • 6. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 要点 • MySQL 5.7では、GIS関連機能が刷新され、InnoDBで以下の機能が 利用できるようになった – GEOMETRYデータ型 – 空間インデックス(Rツリーインデックス) – ST_Contains()、ST_Distance()などのSpatial関数 – GeoHash関数 – GeoJSON関数 • MySQL 5.7で、ジオメトリ同士の関係を比較する関数について、 プレフィックスが付かない関数は今後廃止予定となった – Contains()、Distance()、など 6
  • 7. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 7 MySQLの歴史 4.0 全文検索 (MyISAM) 複数テーブルUPDATE/DELETE 組み込みライブラリ型サーバ OracleMySQL Sun 3.23 MyISAM InnoDB レプリケーション 5.1 プラグガブル・ ストレージエンジン・ アーキテクチャ パーティショニング タスクスケジューラ 5.6 全文検索(InnoDB) memcached API UNDO表領域 Global Transaction ID マルチスレッドスレーブ オンラインALTER TABLE トランスポータブル表領域 5.5 InnoDBがデフォルトに 準同期型レプリケーション PERFORMANCE_SCHEMA 1.0-3.22以前 ストレージエンジン (ISAM, HEAP) マルチスレッド Windows対応/64bit対応 日本語文字コード (SJIS/UJIS) 5.0 ストアドプロシージャ ストアドファンクション カーソル/トリガ/ビュー XAトランザクション INFORMATION_SCHEMA 4.1 GIS(MyISAM) Unicode対応 サブクエリ CSV, ARCHIVE ndbcluster 1995 2000 2005 2010 2015 5.7 2015年10月21日 GA 全文検索CJK対応/GIS (InnoDB) 新コストモデル オプティマイザ ロスレス レプリケーション マルチソース レプリケーション グループ レプリケーション セキュリティ強化 データディクショナリ NoSQLオプション ・ 1995年:スウェーデンにてMySQL AB設立 ・(2005年:オラクルがInnobase Oyを買収) ・ 2008年:Sun MicrosystemsがMySQL ABを買収 ・ 2010年:オラクルがSun Microsystemsを買収
  • 8. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. GIS機能の歴史 • MySQL 4.1 – MyISAMでのみGIS機能が使えるようになった • geometryデータ型 • Spatialインデックス • Spatial関数 • MySQL 5.0 – InnoDBでもgeometryデータ型、Spatial関数を扱えるようになった • Spatialインデックスは使用できず ※地理情報システムに関するオープンな規格であるOpenGISに基づいた実装が されていたが、MySQL開発チームによる独自実装であった 8
  • 9. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. GIS機能の歴史 • MySQL 5.7 – Boost.GeometryというC++のオープンソースライブラリを採用して独自実装をやめ、 InnoDBでGIS関連機能を再実装した • geometryデータ型 • Spatialインデックス • Spatial関数(使える関数の種類も増加) • GeoHashサポート • GeoJSONサポート ※Boost.Geometryコミュニティとも活発に交流し、MySQLチームから Boost.Geometryへのコントリビュートも行っている 9
  • 10. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. GIS機能の歴史 • MySQL 5.7 – 挙動が不明確で分かり難いSpatial関数は非推奨となった • 例:Contains()は、以下のどちらの関数と同じ動きをするか分かり難いので廃止予定となった – MBRContains – ST_Contains() – SRIDについては、現時点で利用できる機能は限られている • SRID(Spatial Reference ID:空間参照ID):特定の測地系、座標系に関連付けられた一意なID 10
  • 11. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. GIS機能の歴史 • 現在開発中のMySQL 8.0では、引き続きGIS機能の拡張を実施中 – OpenGIS標準準拠 – 演算、データ変換に役立つ各種のSpatial関数 • st_x(geom, x) • st_y(geom, y) • st_srid(geom, srid) – MySQL 5.7で非推奨になった関数の廃止 –Geography サポート • st_distance() 関数 –Spatial Reference Systems (SRS) サポート 11
  • 12. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. GIS機能の紹介 12
  • 13. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. geometryデータ型 • OpenGISジオメトリモデルに基づいた以下のデータが使用可能 – GEOMETRY、GEOMETRYCOLLECTION – 点:POINT、MULTIPOINT – 線:LINESTRING、MULTILINESTRING – 多角形:POLYGON、MULTIPOLYGON ※GEOMETRYには、POINT、LINESTRING、POLYGONを格納可能 ※GEOMETRYCOLLECTIONには、MULTIPOINT、MULTILINESTRING、MULTIPOLYGONを 格納可能 13
  • 14. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 補足:MySQL 5.7で扱えるのはベクターデータ • ベクター形式 – 点、線、面(ポリゴン)で表現される – 一般的にデータベースで扱われるデータ形式 – .Shp, .OSM, .KML, .GeoJSON, など • ラスター形式 – セルの塗りつぶしで表現される – 人工衛星、イメージファイルなどで使用される データ形式 – .tiff, .jpg, .gif, など 14
  • 15. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Spatialインデックス • MBR(Minimum Bounding Rectangle:最小外接矩形)に基づく Rツリーインデックス 15 (0,0) (0,1) (0,2) (1,0) (1,1) (1,2) (2,0) (2,1) (2,2) GEOMETRY型の図形 MBR (隣接する最小の長方形)
  • 16. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Spatialインデックス • Rツリーインデックスでは、MBR同士の 抱合関係を保持 • MBRContains()、MBRWithin()、 ST_Contains()、ST_Within()などの 関数で利用される – ST_関数の場合は、最初のスクリーニング ステップでRツリーインデックスが使用され、 その後正確な形に基づいた判定がされる • 現時点では2次元データのみをサポート 16
  • 17. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 補足:MBRとST_の違い(例:Contains関数) • MBRContains(g1,g2) – g1の最小外接矩形にg2の最小外接矩形が含まれているかどうかを示す • ST_Contains(g1,g2) – g1にg2が含まれているかどうかを示す 17
  • 18. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Spatial関数 • ジオメトリデータを扱える各種の関数 • 例 – ST_Distance() • 2つのジオメトリを入力し、2地点間の距離を出力 (単位:角度) – ST_Distance_Sphere() • 2つのジオメトリを入力し、2地点間の距離を地球を球体であると仮定して計算して出力 (単位:メートル) – ST_Contains() • 前ページ参照 18
  • 19. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. GeoHash • 経緯度の情報を文字列化したもの – 例:青山OMセンター(経度:139.718490、緯度:35.671497)のGeoHash ⇒ xn76gmgcsc10hf7v664d8ebpbpbpbp<<後略>> • 特徴 – 文字列なので、データベースで扱いやすい (インデックスも活用可能) – GeoHashの情報は点ではなく範囲(矩形のエリア)を表す – 文字列の桁数で精度を変えることができる • 10桁を超えると矩形の一辺が1m以下になるので、経緯度の変わりとしても十分利用可能 • 近接エリアの判定等に利用可能 19
  • 20. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. GeoHash • MySQL 5.7では、GeoHashを扱うための関数が使用できる – ST_GeoHash():経度、緯度(POINT型のデータでも可)を入力し、GeoHashを出力 – ST_LatFromGeoHash():GeoHashを入力し、経度を出力 – ST_LongFromGeoHash():GeoHashを入力し、緯度を出力 – ST_PointFromGeoHash():GeoHashを入力し、POINT型のデータを出力 20 ※詳細を解説しているマニュアル MySQL 5.7 Reference Manual:13.15.10 Spatial Geohash Functions https://dev.mysql.com/doc/refman/5.7/en/spatial-geohash-functions.html#function_st-geohash
  • 21. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. GeoJSON • 空間データを扱うためのフォーマットの一つ • JSONによる表現で空間データを扱える – 例:青山OMセンター(経度:139.718490、緯度:35.671497)のGeoJSONによる表現 ⇒ {"type": "Point", "coordinates": [139.71849, 35.671497]} 21
  • 22. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. GeoJSON • MySQL 5.7では、GeoJSONを扱うための関数が使用できる – ST_AsGeoJSON():ジオメトリ型のデータを入力し、GeoJSONデータを出力 – ST_GeomFromGeoJSON():GeoJSONデータを入力し、ジオメトリ型のデータを出力 22 ※詳細を解説しているマニュアル MySQL 5.7 Reference Manual:13.15.11 Spatial GeoJSON Functions https://dev.mysql.com/doc/refman/5.7/en/spatial-geojson-functions.html
  • 23. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MySQL Workbenchの活用 23
  • 24. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MySQL Workbench • 管理ツール • SQLエディタ • Performance Dashboard • Visual Explain • GIS Viewer • ER図作成、フォワード/ リバースエンジニアリング、 など豊富な機能 24 データベースアーキテクト、開発者、DBA のための統合ビジュアルツール 商用版のみの機能: DBドキュメント出力、データモデルの検証、 MySQL Enterprise Backup GUI、MySQL Enterprise Audit GUI
  • 25. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MySQL Workbench – 起動後の画面 • クライアントPCにインストールし、 MySQLデータベースへの 接続定義を作成して接続 25
  • 26. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MySQL Workbench – 管理 • サーバーステータス • クライアントコネクション 一覧 • ユーザー管理 • データDump/Import • サーバー起動/停止 • パフォーマンスレポート • パフォーマンススキーマの 設定変更、など 26
  • 27. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MySQL Workbench – 開発支援:SQL Editor • SQLエディタ – キーワードハイライト – SQL整形 – スニペット、など • Visual Explain • オブジェクト確認 • データ確認/編集 • オブジェクト作成、など 27
  • 28. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MySQL Workbench – モデリング • ER図作成 • フォワードエンジニアリング • リバースエンジニアリング • ドキュメント出力(商用版のみ) • データモデルの検証 (商用版のみ) • データモデルとDBの同期、 など 28
  • 29. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. MySQL Workbench – データベース・マイグレーション・ウィザード • 移行元 – Microsoft SQL Server, PostgreSQL, Sybase ASE, Sybase SQL Anywhere, SQLite, Microsoft Access, MySQL, and more • オブジェクトとデータの移行 • MySQLバージョンアップグレード (テスト環境用) 29
  • 30. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. "Spatial Viewer" and "Geometry Viewer" • ジオメトリ型のデータを図示可能 30
  • 31. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Appendix:Some Real World Examples 31
  • 32. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. A Starting Point • My old apartment in Brooklyn, NY – 33 Withers Street Brooklyn, NY 11211 – POINT(<LONG>,<LAT>) • -73.951353,40.716914 32 https://www.google.com/maps/place/33+Withers+St,+Brooklyn,+NY+11211/@40.7169144,-73.9513538
  • 33. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. The Application Use Case • I’m hungry and in the mood for Thai food – What Thai restaurants are around me? – What’s the closest one? – Can I see the menu, contact info, yelp ratings, etc.? – How would I get there? 33
  • 34. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Getting Some Data In • Download a NYC OSM extract: – http://osm-extracted-metros.s3.amazonaws.com/new-york.osm.bz2 • Import the data using a customized OsmDB.pm Perl module – http://wiki.openstreetmap.org/wiki/OsmDB.pm (original) – https://www.dropbox.com/s/l17vj3wf9y13tee/osmdb-scripts.tar.gz (customized) • Creates a Geometry column named ‘geom’ • Creates a spatial index on the ‘geom’ column 34 mysql -e "create database nyosm" bunzip2 new-york.osm.bz2 ./bulkDB.pl new-york.osm nyosm
  • 35. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. The Generated Schema • http://wiki.openstreetmap.org/wiki/Elements 35 mysql> show tables; +-----------------+ | Tables_in_nyosm | +-----------------+ | nodes | | nodetags | | relationmembers | | relations | | relationtags | | waynodes | | ways | | waytags | +-----------------+ – We’ll focus on nodes and nodetags for our examples – Nodes • A point or location – Nodetags • Metadata about each location • X name/value pairs
  • 36. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. De-normalizing the Tag Data • Greatly simplify our query • Allow for the use of a full-text index – Also improves performance • Mimic better schema created by osm2pgsql – http://wiki.openstreetmap.org/wiki/Osm2pgsql/schema#planet_osm_nodes 36 mysql> alter table nodes add column tags text, add fulltext index(tags); mysql> update nodes set tags=(SELECT group_concat(concat(k, "=", v) SEPARATOR ';') from nodetags where nodetags.id=nodes.id group by nodes.id); * Source: ESRI
  • 37. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Final Nodes Table 37 mysql> show create table nodes¥G *************************** 1. row *************************** Table: nodes Create Table: CREATE TABLE `nodes` ( `id` bigint(20) DEFAULT NULL, `geom` geometry NOT NULL, `user` varchar(50) DEFAULT NULL, `version` int(11) DEFAULT NULL, `timestamp` varchar(20) DEFAULT NULL, `uid` int(11) DEFAULT NULL, `changeset` int(11) DEFAULT NULL, `tags` text, UNIQUE KEY `i_nodeids` (`id`), SPATIAL KEY `i_geomidx` (`geom`), FULLTEXT KEY `tags` (`tags`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  • 38. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Creating a Distance Calculation Function • Various great circle (orthodrome) distance formulas – Haversine, Spherical Law of Cosines (my choice), … – http://en.wikipedia.org/wiki/Great-circle_distance – Necessary for calculating distances between two Geometries • Need goes away when we support Geography and/or Projections (ST_Transform) 38 mysql> CREATE FUNCTION slc (lat1 double, lon1 double, lat2 double, lon2 double) RETURNS double RETURN 6371 * acos(cos(radians(lat1)) * cos(radians(lat2)) * cos(radians(lon2) - radians(lon1)) + sin(radians(lat1)) * sin(radians(lat2)));
  • 39. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Creating a Bounding Box For Our Search • Utilize the r-tree index by limiting area – Easy with future spatial reference systems support • WGS84 or SRID 4326 being the most common – Need to use some additional geographic formulas • http://www.movable-type.co.uk/scripts/latlong.html • Need should go away with full SRID support 39 ${origlon} = -73.951368 ${origlat} = 40.716743 ${lon1} = ${origlon} + (${distance_in_km}/abs(cos(radians({$origlat}))*111)) ${lat1} = ${origlat} + (${distance_in_km}/111) ${lon2} = ${origlon} - (${distance_in_km}/abs(cos(radians({$origlat}))*111)) ${lat2} = ${origlat} - (${distance_in_km}/111)
  • 40. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Calculating the Results • Our final query, searching within ~ 10km radius 40 mysql> SELECT id, slc(40.716743, -73.951368, y(geom), x(geom))*1000 as distance_in_meters, tags, ST_AsText(geom) FROM nodes WHERE ST_Contains(ST_Envelope(linestring(point((-73.951368+(10/111)), (40.716743+(10/111))), point((-73.951368-(10/111)), (40.716743-(10/111))))), geom) AND match(tags) against ("+thai +restaurant" IN BOOLEAN MODE) ORDER BY distance_in_meters¥G
  • 41. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Examining the Results 41 *************************** 1. row *************************** id: 888976948 distance_in_meters: 614.4973960877276 tags: addr:street=Bedford Avenue;amenity=restaurant;name=Tai Thai;addr:housenumber=206;phone=7185995556 astext(geom): POINT(-73.958637 40.717174) *************************** 2. row *************************** id: 2178443635 distance_in_meters: 2780.87697408101 tags: microbrewery=no;website=http://www.onemorethai.net/;name=One More Thai;amenity=restaurant;opening_hours=12:00-22:30;cuisine=thai;phone=(212) 228-8858 astext(geom): POINT(-73.983871 40.7210541) *************************** 3. row *************************** …
  • 42. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Mapping the Results 42 • From my old place – -73.951353,40.716914 • To Tai Thai – -73.958637,40.717174 • Maps APIs – Google, Bing, Apple, … https://www.google.com/maps/dir/40.716914,+-73.951353/40.717174,+-73.958637
  • 43. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. Appendix:参考情報 43
  • 44. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 参考情報 • マニュアル – http://dev.mysql.com/doc/refman/5.7/en/spatial-extensions.html • コミュニティフォーラム – http://forums.mysql.com/list.php?23 • Boost.Geometry – http://www.boost.org/libs/geometry • バグ報告&機能追加リクエスト – http://bugs.mysql.com/ 44
  • 45. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 参考情報 • MySQL 5.7 and GIS, an Example http://mysqlserverteam.com/mysql-5-7-and-gis-an-example/ • Importing Raster Based Spatial Data into MySQL 5.7 http://mysqlserverteam.com/importing-raster-based-spatial-data-into- mysql-5-7/ • MySQL GIS: Boost.Geometry – http://mysqlserverteam.com/making-use-of-boost-geometry-in-mysql-gis/ – http://mysqlserverteam.com/why-boost-geometry-in-mysql/ – http://mysqlserverteam.com/building-mysql-with-boost/ • MySQL GIS: InnoDB R-Tree – http://mysqlserverteam.com/innodb-spatial-indexes-in-5-7-4-lab-release/ 45
  • 46. Copyright © 2016, Oracle and/or its affiliates. All rights reserved. 46