MySQL 允许在 NOT NULL 几何值列上创建 SPATIAL 索引(参见 第 13.4.10 节,“创建空间索引”)。优化器检查索引列的 SRID 属性,以确定用于比较的空间参考系统(SRS),并使用适用于该 SRS 的计算方法。(在 MySQL 8.4 之前,优化器使用笛卡尔计算比较 SPATIAL 索引值;如果列包含非笛卡尔 SRID 的值,则此类操作的结果未定义。)MySQL permits creation of SPATIAL indexes on
NOT NULL geometry-valued columns (see
Section 13.4.10, “Creating Spatial Indexes”). The optimizer
checks the SRID attribute for indexed columns
to determine which spatial reference system (SRS) to use for
comparisons, and uses calculations appropriate to the SRS.
(Prior to MySQL 8.4, the optimizer performs
comparisons of SPATIAL index values using
Cartesian calculations; the results of such operations are
undefined if the column contains values with non-Cartesian
SRIDs.)
为了比较能正确进行,SPATIAL 索引中的每一列都必须有 SRID 限制。也就是说,列定义必须包含一个明确的 SRID 属性,并且所有列值必须具有相同的 SRID。For comparisons to work properly, each column in a
SPATIAL index must be SRID-restricted. That
is, the column definition must include an explicit
SRID attribute, and all column values must
have the same SRID.
优化器仅考虑对 SRID 限制的列使用 SPATIAL 索引:The optimizer considers SPATIAL indexes only
for SRID-restricted columns:
限制为笛卡尔 SRID 的列上的索引使得可以进行笛卡尔边界框计算。Indexes on columns restricted to a Cartesian SRID enable Cartesian bounding box computations.
限制为地理 SRID 的列上的索引使得可以进行地理边界框计算。Indexes on columns restricted to a geographic SRID enable geographic bounding box computations.
优化器会忽略没有 SRID 属性的列上的 SPATIAL 索引(因此不受 SRID 限制)。MySQL 仍然会维护这些索引,如下所示:The optimizer ignores SPATIAL indexes on
columns that have no SRID attribute (and thus
are not SRID-restricted). MySQL still maintains such indexes, as
follows:
它们会在表修改时更新(
INSERT、UPDATE、DELETE等)。即使该列可能包含笛卡尔值和地理值的混合,更新仍然像索引是笛卡尔索引一样进行。They are updated for table modifications (INSERT,UPDATE,DELETE, and so forth). Updates occur as though the index was Cartesian, even though the column might contain a mix of Cartesian and geographical values.它们仅用于向后兼容(例如,在 MySQL 8.2 中执行转储并在 MySQL 8.3 中恢复的能力)。因为对没有 SRID 限制的列的
SPATIAL索引对优化器没有用,所以每个这样的列都应进行修改:They exist only for backward compatibility (for example, the ability to perform a dump in MySQL 8.2 and restore in MySQL 8.3). BecauseSPATIALindexes on columns that are not SRID-restricted are of no use to the optimizer, each such column should be modified:验证列中的所有值是否具有相同的 SRID。要确定几何列
col_name中包含的 SRID,请使用以下查询:Verify that all values within the column have the same SRID. To determine the SRIDs contained in a geometry columncol_name, use the following query:SELECT DISTINCT ST_SRID(
col_name) FROMtbl_name;如果查询返回多于一行,则该列包含混合的 SRID。在这种情况下,修改其内容以使所有值具有相同的 SRID。If the query returns more than one row, the column contains a mix of SRIDs. In that case, modify its contents so all values have the same SRID.
重新定义该列以具有明确的
SRID属性。Redefine the column to have an explicitSRIDattribute.重新创建
SPATIAL索引。Recreate theSPATIALindex.