2016-01-20 33 views
0

我使用MySQL 5.7.10,存儲引擎是InnoDB。 以下是SQL。
1.創建表爲什麼這個查詢不使用鍵(MySQL 5.7空間)?

CREATE TABLE `geo` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, 
`geo` GEOMETRY NOT NULL, 
PRIMARY KEY (`id`), 
SPATIAL INDEX `geo` (`geo`) 
) 
COLLATE='utf8mb4_general_ci' 
ENGINE=InnoDB 
AUTO_INCREMENT=1 

2.插入數據

insert into geo(geo) values(ST_GeomFromText('POINT(108.875000 34.216020)')); 
insert into geo(geo) values(ST_GeomFromText('POINT(109.569098 36.646357)')); 
insert into geo(geo) values(ST_GeomFromText('POINT(109.550988 36.633384)')); 
insert into geo(geo) values(ST_GeomFromText('POINT(109.472800 36.624116)')); 
insert into geo(geo) values(ST_GeomFromText('POINT(109.487460 36.563614)')); 
insert into geo(geo) values(ST_GeomFromText('POINT(109.532016 36.686191)')); 
insert into geo(geo) values(ST_GeomFromText('POINT(109.319010 36.987505)')); 

3.創建一個多邊形

SET @g3 = ST_GeomFromText('Polygon((108 36.5,108 36.7,109.5 36.7,109.5 36.5,108 36.5))'); 

4.講解SQL

mysql> explain select st_x(geo),st_y(geo) from geo where mbrcontains(@g3,geo)>0\G 
*************************** 1. row *************************** 
id: 1 
select_type: SIMPLE 
table: geo 
partitions: NULL 
type: ALL 
possible_keys: NULL 
key: NULL 
key_len: NULL 
ref: NULL 
rows: 8 
filtered: 100.00 
Extra: Using where 
1 row in set, 1 warning (0.00 sec) 

mysql> show warnings\G 
*************************** 1. row *************************** 
Level: Note 
Code: 1003 
Message: /* select#1 */ select st_x(`map`.`geo`.`geo`) AS `st_x(geo)`,st_y(`map`.`geo`.`geo`) AS `st_y(geo)` from `map`.`geo` where (mbrcontains((@`g3`),`map`.`geo`.`geo`) > 0) 
1 row in set (0.00 sec) 

爲什麼沒有按」 t thi s查詢使用密鑰?

+0

你'EXPLAIN'返回一個警告。它以前如何? (查詢後運行'SHOW WARNINGS'顯示警告。) – duskwuff

+0

'mysql> show warnings \ G ************************** * 1. row *************************** 等級:備註 代碼:1003 消息:/ * select#1 */select st_x(''map'.'geo'.'geo')AS'st_x(geo)',st_y('map'.'geo'.'geo')AS'st_y(geo)'來自 m'map' .'geo'其中(mbrcontains((@'g3'),'map'.'geo'.''''>> 0) 集合中的第1行(0.00秒)' – user2400825

+0

請在您的問題中包含警告,而不是在評論中。 – duskwuff

回答

1

在此基礎上Percona post

「的空間索引(RTREE)只支持MyISAM表,我們可以使用的功能InnoDB表,但它不會使用空間鍵」

+0

「對於MyISAM和(從MySQL 5.7.5開始)InnoDB表,MySQL可以使用類似於創建常規索引的語法創建空間索引,但使用SPATIAL關鍵字。」---- https://dev.mysql。 com/doc/refman/5.7/en/creating-spatial-indexes.html – user2400825

+0

我和MySQL都犯了錯誤。我的錯誤是,功能「mbrcontains」conld不與「>」一起使用。 MySQL的錯誤是5.7.10無法支持空間索引。當我將表的存儲引擎更改爲MyISAM時,查詢使用了索引。因此它必須是MySQL的錯誤。 – user2400825

相關問題