文档首页
MySQL 8.4 参考手册
8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 10 优化 / 10.3 优化和索引 / 10.3.3 SPATIAL 索引优化
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

10.3.3 空间索引优化10.3.3 SPATIAL Index Optimization

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). Because SPATIAL indexes 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 column col_name, use the following query:

      SELECT DISTINCT ST_SRID(col_name) FROM tbl_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 explicit SRID attribute.

    • 重新创建 SPATIAL 索引。Recreate the SPATIAL index.