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;