金子邦彦研究室情報工学全般MySQL の利用MySQL の空間索引を使ってみる

MySQL の空間索引を使ってみる

テーブル定義の例

create table GDATA (
  x DOUBLE not null,
  y DOUBLE not null,
  pt POINT DEFAULT NULL
) ENGINE=MyISAM DEFAULT charset=utf8;

空間索引の作成の例

update GDATA set pt = PointFromText( concat( ‘POINT(’,x,' ',y,')' ) );
create table GDATA2 (
  x DOUBLE not null,
  y DOUBLE not null,
  SPATIAL KEY `sp_index` (`pt`)
) ENGINE=MyISAM DEFAULT charset=utf8;

空間索引を使うSQL問い合わせの例

insert into GDATA2(x,y,pt) select x,y,pt from GDATA;