Window functions affect the strategies the optimizer considers:窗口函数影响优化器考虑的策略:
Derived table merging for a subquery is disabled if the subquery has window functions. 如果子查询具有窗口函数,则子查询的派生表合并将被禁用。The subquery is always materialized.子查询总是具体化的。
Semijoins are not applicable to window function optimization because semijoins apply to subqueries in 半联接不适用于窗口函数优化,因为半联接适用于WHERE
and JOIN ... ON
, which cannot contain window functions.WHERE
和JOIN ... ON
中的子查询,它不能包含窗口函数。
The optimizer processes multiple windows that have the same ordering requirements in sequence, so sorting can be skipped for windows following the first one.优化器按顺序处理具有相同排序要求的多个窗口,因此可以跳过第一个窗口之后的窗口的排序。
The optimizer makes no attempt to merge windows that could be evaluated in a single step (for example, when multiple 优化器不会尝试合并可以在单个步骤中计算的窗口(例如,当多个OVER
clauses contain identical window definitions). OVER
子句包含相同的窗口定义时)。The workaround is to define the window in a 解决方法是在WINDOW
clause and refer to the window name in the OVER
clauses.WINDOW
子句中定义窗口,并在OVER
子句中引用窗口名称。
An aggregate function not used as a window function is aggregated in the outermost possible query. 未用作窗口函数的聚合函数在最外层的查询中聚合。For example, in this query, MySQL sees that 例如,在这个查询中,MySQL看到COUNT(t1.b)
is something that cannot exist in the outer query because of its placement in the WHERE
clause:COUNT(t1.b)
在外部查询中不存在,因为它位于WHERE
子句中:
SELECT * FROM t1 WHERE t1.a = (SELECT COUNT(t1.b) FROM t2);
Consequently, MySQL aggregates inside the subquery, treating 因此,MySQL在子查询中聚合,将t1.b
as a constant and returning the count of rows of t2
.t1.b
视为常量并返回t2
的行数。
Replacing 将WHERE
with HAVING
results in an error:WHERE
替换为HAVING
导致错误:
mysql> SELECT * FROM t1 HAVING t1.a = (SELECT COUNT(t1.b) FROM t2);
ERROR 1140 (42000): In aggregated query without GROUP BY, expression #1
of SELECT list contains nonaggregated column 'test.t1.a'; this is
incompatible with sql_mode=only_full_group_by
The error occurs because 发生此错误的原因是COUNT(t1.b)
can exist in the HAVING
, and so makes the outer query aggregated.COUNT(t1.b)
可以存在于HAVING
中,从而使外部查询聚合。
Window functions (including aggregate functions used as window functions) do not have the preceding complexity. 窗口函数(包括用作窗口函数的聚合函数)不具有上述复杂性。They always aggregate in the subquery where they are written, never in the outer query.它们总是聚集在写入它们的子查询中,而不是在外部查询中。
Window function evaluation may be affected by the value of the 窗口函数计算可能会受到windowing_use_high_precision
system variable, which determines whether to compute window operations without loss of precision. windowing_use_high_precision
系统变量值的影响,该系统变量决定是否在不损失精度的情况下计算窗口操作。By default, 默认情况下,将启用windowing_use_high_precision
is enabled.windowing_use_high_precision
。
For some moving frame aggregates, the inverse aggregate function can be applied to remove values from the aggregate. 对于某些移动帧聚合,可以应用反向聚合函数从聚合中移除值。This can improve performance but possibly with a loss of precision. 这可以提高性能,但可能会降低精度。For example, adding a very small floating-point value to a very large value causes the very small value to be “hidden” by the large value. 例如,将非常小的浮点值添加到非常大的值会导致非常小的值被大的值“隐藏”。When inverting the large value later, the effect of the small value is lost.稍后反转大值时,小值的效果将丢失。
Loss of precision due to inverse aggregation is a factor only for operations on floating-point (approximate-value) data types. 由于反向聚合而导致的精度损失仅是浮点(近似值)数据类型操作的一个因素。For other types, inverse aggregation is safe; this includes 对于其他类型,反向聚合是安全的;这包括DECIMAL
, which permits a fractional part but is an exact-value type.DECIMAL
,它允许小数部分,但是一种精确的值类型。
For faster execution, MySQL always uses inverse aggregation when it is safe:为了更快地执行,MySQL总是在安全的情况下使用反向聚合:
For floating-point values, inverse aggregation is not always safe and might result in loss of precision. 对于浮点值,反向聚合并不总是安全的,可能会导致精度损失。The default is to avoid inverse aggregation, which is slower but preserves precision. 默认设置是避免反向聚合,反向聚合速度较慢,但保留精度。If it is permissible to sacrifice safety for speed, 如果允许为速度牺牲安全性,则可以禁用windowing_use_high_precision
can be disabled to permit inverse aggregation.windowing_use_high_precision
以允许反向聚合。
For nonfloating-point data types, inverse aggregation is always safe and is used regardless of the 对于非浮点数数据类型,反向聚合始终是安全的,并且无论windowing_use_high_precision
value.windowing_use_high_precision
值如何,都会使用反向聚合。
windowing_use_high_precision
has no effect on MIN()
and MAX()
, which do not use inverse aggregation in any case.windowing_use_high_precision
对MIN()
和MAX()
没有影响,它们在任何情况下都不使用反向聚合。
For evaluation of the variance functions 对于方差函数STDDEV_POP()
, STDDEV_SAMP()
, VAR_POP()
, VAR_SAMP()
, and their synonyms, evaluation can occur in optimized mode or default mode. STDDEV_POP()
、STDDEV_SAMP()
、VAR_POP()
、VAR_SAMP()
及其同义词的求值,可以在优化模式或默认模式下进行求值。Optimized mode may produce slightly different results in the last significant digits. 优化模式可能会在最后一个有效数字中产生略有不同的结果。If such differences are permissible, 如果这种差异是允许的,可以禁用windowing_use_high_precision
can be disabled to permit optimized mode.windowing_use_high_precision
以允许优化模式。
For 对于EXPLAIN
, windowing execution plan information is too extensive to display in traditional output format. EXPLAIN
,窗口化执行计划信息过于广泛,无法以传统的输出格式显示。To see windowing information, use 要查看窗口信息,请使用EXPLAIN FORMAT=JSON
and look for the windowing
element.EXPLAIN FORMAT=JSON
并查找windowing
元素。