8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

27.9 视图的限制27.9 Restrictions on Views

视图定义中可引用的最大表数为 61。The maximum number of tables that can be referenced in the definition of a view is 61.

视图处理未进行优化:View processing is not optimized:

  • 无法在视图上创建索引。It is not possible to create an index on a view.

  • 索引可以用于使用合并算法处理的视图。然而,使用临时表算法处理的视图无法利用其基础表上的索引(尽管在生成临时表期间可以使用索引)。Indexes can be used for views processed using the merge algorithm. However, a view that is processed with the temptable algorithm is unable to take advantage of indexes on its underlying tables (although indexes can be used during generation of the temporary tables).

有一个通用原则,即不能在子查询中修改表的同时从同一表中进行选择。参见 第 15.2.15.12 节,“子查询的限制”。There is a general principle that you cannot modify a table and select from the same table in a subquery. See Section 15.2.15.12, “Restrictions on Subqueries”.

如果选择的视图从表中进行选择,且视图在子查询中从表中选择,并且视图使用合并算法进行评估,则同样原则也适用。示例:The same principle also applies if you select from a view that selects from the table, if the view selects from the table in a subquery and the view is evaluated using the merge algorithm. Example:

CREATE VIEW v1 AS
SELECT * FROM t2 WHERE EXISTS (SELECT 1 FROM t1 WHERE t1.a = t2.a);

UPDATE t1, v2 SET t1.a = 1 WHERE t1.b = v2.b;

如果视图是使用临时表来评估的,你 可以 在视图子查询中从表中选择数据,同时在外部查询中修改该表。在这种情况下,视图存储在临时表中,因此你实际上并不是在同时从子查询中的表中选择数据并修改它。(这也是你可能希望通过在视图定义中指定 ALGORITHM = TEMPTABLE 来强制 MySQL 使用临时表算法的另一个原因。)If the view is evaluated using a temporary table, you can select from the table in the view subquery and still modify that table in the outer query. In this case, the view is stored in a temporary table and thus you are not really selecting from the table in a subquery and modifying it at the same time. (This is another reason you might wish to force MySQL to use the temptable algorithm by specifying ALGORITHM = TEMPTABLE in the view definition.)

您可以使用 DROP TABLE 或 ALTER TABLE 来删除或修改在视图定义中使用的表。即使这会使视图失效,DROP 或 ALTER 操作也不会产生警告。相反,错误会在稍后使用视图时发生。CHECK TABLE 可用于检查因 DROP 或 ALTER 操作而失效的视图。You can use DROP TABLE or ALTER TABLE to drop or alter a table that is used in a view definition. No warning results from the DROP or ALTER operation, even though this invalidates the view. Instead, an error occurs later, when the view is used. CHECK TABLE can be used to check for views that have been invalidated by DROP or ALTER operations.

关于视图的可更新性,视图的总体目标是,如果任何视图在理论上是可更新的,那么在实践中它也应该是可更新的。许多理论上可更新的视图现在可以更新,但仍然存在一些限制。详情请参见 第 27.5.3 节,“可更新和可插入视图”。With regard to view updatability, the overall goal for views is that if any view is theoretically updatable, it should be updatable in practice. Many theoretically updatable views can be updated now, but limitations still exist. For details, see Section 27.5.3, “Updatable and Insertable Views”.

当前视图实现存在一个缺陷。如果授予用户创建视图所需的基本权限(CREATE VIEW 和 SELECT 权限),该用户仍然无法在该对象上调用 SHOW CREATE VIEW,除非用户还被授予 SHOW VIEW 权限。There exists a shortcoming with the current implementation of views. If a user is granted the basic privileges necessary to create a view (the CREATE VIEW and SELECT privileges), that user cannot call SHOW CREATE VIEW on that object unless the user is also granted the SHOW VIEW privilege.

这个缺点可能导致使用 mysqldump 备份数据库时出现问题,因为可能由于权限不足而失败。这个问题在 Bug #22062 中有所描述。That shortcoming can lead to problems backing up a database with mysqldump, which may fail due to insufficient privileges. This problem is described in Bug #22062.

解决该问题的方式是管理员手动将 SHOW VIEW 权限授予被授权 CREATE VIEW 的用户,因为在创建视图时 MySQL 并不会隐式授予该权限。The workaround to the problem is for the administrator to manually grant the SHOW VIEW privilege to users who are granted CREATE VIEW, since MySQL doesn't grant it implicitly when views are created.

视图没有索引,因此索引提示不适用。在从视图中选择数据时不允许使用索引提示。Views do not have indexes, so index hints do not apply. Use of index hints when selecting from a view is not permitted.

SHOW CREATE VIEW 使用 AS alias_name 子句显示每列的视图定义。如果列是由表达式创建的,默认别名就是表达式文本,可能会非常长。在 CREATE VIEW 语句中,列名别名会根据最大列长度 64 个字符进行检查(而不是最大别名长度 256 个字符)。因此,如果任何列别名超过 64 个字符,从 SHOW CREATE VIEW 输出创建的视图会失败。对于别名过长的视图,这可能会在以下情况下造成问题:SHOW CREATE VIEW displays view definitions using an AS alias_name clause for each column. If a column is created from an expression, the default alias is the expression text, which can be quite long. Aliases for column names in CREATE VIEW statements are checked against the maximum column length of 64 characters (not the maximum alias length of 256 characters). As a result, views created from the output of SHOW CREATE VIEW fail if any column alias exceeds 64 characters. This can cause problems in the following circumstances for views with too-long aliases:

  • 视图定义无法复制到实施列长度限制的新副本。View definitions fail to replicate to newer replicas that enforce the column-length restriction.

  • 使用 mysqldump 创建的转储文件无法加载到实施列长度限制的服务器中。Dump files created with mysqldump cannot be loaded into servers that enforce the column-length restriction.

解决这两种问题的一种方法是修改每个有问题的视图定义,使用提供更短列名的别名。然后视图就可以正确复制,并且可以导出和重新加载而不会导致错误。要修改定义,可以使用 DROP VIEW 和 CREATE VIEW 删除并重新创建视图,或者使用 CREATE OR REPLACE VIEW 替换视图定义。A workaround for either problem is to modify each problematic view definition to use aliases that provide shorter column names. Then the view replicates properly, and can be dumped and reloaded without causing an error. To modify the definition, drop and create the view again with DROP VIEW and CREATE VIEW, or replace the definition with CREATE OR REPLACE VIEW.

对于在重新加载转储文件中的视图定义时出现的问题,另一种解决方法是编辑转储文件以修改其 CREATE VIEW 语句。然而,这不会更改原始视图定义,这可能会导致后续转储操作出现问题。For problems that occur when reloading view definitions in dump files, another workaround is to edit the dump file to modify its CREATE VIEW statements. However, this does not change the original view definitions, which may cause problems for subsequent dump operations.