我们已将MySql 5.7数据库的副本迁移到Amazon RDS上的MySql 8.0.11。如果可能,所有内容都相同。包含几何数据的表已被修改,因此几何列被限制为SRID 0,并且重建了空间索引。两个数据库的执行计划是相同的,并表明查询正在使用空间索引。
在MySql 5.7上,以下查询在MySql 8上花费0.001s,在108s上花费。在MySql 8上,查看执行统计信息“发送数据”的时间占100%。
No index = foo
[0] = foo
[1] = bar
The value is: foo bar
The value is now: bar
我们有许多不同的空间查询,并且所有查询的行为都与此类似,但是特别是MBRINTERSECTS和ST_INTERSECTS似乎非常慢。更改为ST_CONTAINS(在可能的情况下)可以带来显着的改进(即,上面的过程用3s而不是108s),但这不适用于许多查询,并且仍然比5.7慢很多。
SELECT r.roadid
FROM geocoder.osm_road r
WHERE mbrintersects(ST_Buffer(ST_GEOMETRYFROMTEXT('Point(-1.91289 52.58260)',0),0.0005), r.geometry)
从osm_road显示索引(5.7)
Show Create Table for 5.7
CREATE TABLE `osm_road` (
`roadid` bigint(20) NOT NULL,
`reference` varchar(20) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`dictionary` longtext,
`road_speed_limit` tinyint(4) unsigned NOT NULL,
`road_speed_unitid` tinyint(1) NOT NULL,
`road_type` tinyint(4) NOT NULL,
`is_toll_road` bit(1) NOT NULL DEFAULT b'0',
`is_one_way` bit(1) NOT NULL DEFAULT b'0',
`countryid` smallint(6) DEFAULT NULL,
`geometry` geometry NOT NULL,
`datemodified` datetime DEFAULT CURRENT_TIMESTAMP,
`datecreated` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`roadid`),
UNIQUE KEY `id_roadid` (`roadid`),
SPATIAL KEY `ix_road_geometry` (`geometry`),
KEY `ix_road_reference` (`reference`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AVG_ROW_LENGTH=207
Show Create Table for 8.0.11
CREATE TABLE `osm_road` (
`roadid` bigint(20) NOT NULL,
`reference` varchar(20) DEFAULT NULL,
`name` varchar(100) DEFAULT NULL,
`dictionary` longtext,
`road_speed_limit` tinyint(4) unsigned NOT NULL,
`road_speed_unitid` tinyint(1) NOT NULL,
`road_type` tinyint(4) NOT NULL,
`is_toll_road` bit(1) NOT NULL DEFAULT b'0',
`is_one_way` bit(1) NOT NULL DEFAULT b'0',
`countryid` smallint(6) DEFAULT NULL,
`geometry` geometry NOT NULL /*!80003 SRID 0 */,
`datemodified` datetime DEFAULT CURRENT_TIMESTAMP,
`datecreated` datetime DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`roadid`),
UNIQUE KEY `id_roadid` (`roadid`),
KEY `ix_road_reference` (`reference`),
SPATIAL KEY `ix_road_geometry` (`geometry`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AVG_ROW_LENGTH=207
从osm_road显示索引(8.0.11)
Table,Non_unique,Key_name,Seq_in_index,Column_name,Collation,Cardinality,Sub_part,Packed,Null,Index_type,Comment,Index_comment
osm_road,0,PRIMARY,1,roadid,A,18973144,NULL,NULL,,BTREE,,
osm_road,1,ix_road_geometry,1,geometry,A,18973154,32,NULL,,SPATIAL,,
osm_road,1,ix_road_reference,1,reference,A,199900,NULL,NULL,YES,BTREE,,
显示全局状态(8.0.11)Click Here
显示全局变量(8.0.11)Click Here
-添加了2019-01-27 -----------------
Table,Non_unique,Key_name,Seq_in_index,Column_name,Collation,Cardinality,Sub_part,Packed,Null,Index_type,Comment,Index_comment,Visible
osm_road,0,PRIMARY,1,roadid,A,16194921,NULL,NULL,,BTREE,,,YES
osm_road,1,ix_road_reference,1,reference,A,86215,NULL,NULL,YES,BTREE,,,YES
osm_road,1,ix_road_geometry,1,geometry,A,16194921,32,NULL,,SPATIAL,,,YES
返回:
EXPLAIN SELECT r.roadid FROM geocoder.osm_road r WHERE mbrintersects(ST_Buffer(ST_GEOMETRYFROMTEXT('Point(-1.91289 52.58260)',0),0.0005), r.geometry);
答案 0 :(得分:0)
MBRIntersects
和ST_Intersects
在MySQL 8.0(在8.0.15中进行测试)中已损坏,与MySQL 5.7相比,运行速度慢了10倍以上。对于我来说,禁止使用空间索引(使用IGNORE INDEX
,强制执行全表扫描,可以在某种程度上加快我的查询速度。请参见此错误报告https://bugs.mysql.com/bug.php?id=94655
答案 1 :(得分:0)
我也遇到类似的问题,答案是确保空间列包含SRID。参见https://mysqlserverteam.com/upgrading-to-mysql-8-0-with-spatial-data/。完成之后,查询开始使用空间索引,这是他们以前没有的