This section discusses general characteristics of derived tables. 本节讨论派生表的一般特性。For information about lateral derived tables preceded by the 有关以LATERAL
keyword, see Section 13.2.11.9, “Lateral Derived Tables”.LATERAL
关键字开头的横向派生表的信息,请参阅第13.2.11.9节,“横向派生表”。
A derived table is an expression that generates a table within the scope of a query 派生表是在查询FROM
clause. FROM
子句范围内生成表的表达式。For example, a subquery in a 例如,SELECT
statement FROM
clause is a derived table:SELECT
语句FROM
子句中的子查询是派生表:
SELECT ... FROM (subquery
) [AS]tbl_name
...
The JSON_TABLE()
function generates a table and provides another way to create a derived table:JSON_TABLE()
函数的作用是:生成一个表,并提供另一种创建派生表的方法:
SELECT * FROM JSON_TABLE(arg_list
) [AS]tbl_name
...
The [AS]
clause is mandatory because every table in a tbl_name
FROM
clause must have a name. [AS]
子句是必需的,因为tbl_name
FROM
子句中的每个表都必须有一个名称。Any columns in the derived table must have unique names. 派生表中的任何列都必须具有唯一的名称。Alternatively, 或者,tbl_name
may be followed by a parenthesized list of names for the derived table columns:tbl_name
后面可以是派生表列的带圆括号的名称列表:
SELECT ... FROM (subquery
) [AS]tbl_name
(col_list
) ...
The number of column names must be the same as the number of table columns.列名数必须与表列数相同。
For the sake of illustration, assume that you have this table:为了便于说明,假设您有以下表格:
CREATE TABLE t1 (s1 INT, s2 CHAR(5), s3 FLOAT);
Here is how to use a subquery in the 下面是如何使用FROM
clause, using the example table:FROM
子句中的子查询的示例表:
INSERT INTO t1 VALUES (1,'1',1.0); INSERT INTO t1 VALUES (2,'2',2.0); SELECT sb1,sb2,sb3 FROM (SELECT s1 AS sb1, s2 AS sb2, s3*2 AS sb3 FROM t1) AS sb WHERE sb1 > 1;
Result:结果:
+------+------+------+ | sb1 | sb2 | sb3 | +------+------+------+ | 2 | 2 | 4 | +------+------+------+
Here is another example: Suppose that you want to know the average of a set of sums for a grouped table. 下面是另一个示例:假设您想知道分组表的一组和的平均值。This does not work:这不起作用:
SELECT AVG(SUM(column1)) FROM t1 GROUP BY column1;
However, this query provides the desired information:但是,此查询提供了所需的信息:
SELECT AVG(sum_column1) FROM (SELECT SUM(column1) AS sum_column1 FROM t1 GROUP BY column1) AS t1;
Notice that the column name used within the subquery (注意,在外部查询中可以识别子查询中使用的列名(sum_column1
) is recognized in the outer query.sum_column1
)。
The column names for a derived table come from its select list:派生表的列名来自其选择列表:
mysql> SELECT * FROM (SELECT 1, 2, 3, 4) AS dt;
+---+---+---+---+
| 1 | 2 | 3 | 4 |
+---+---+---+---+
| 1 | 2 | 3 | 4 |
+---+---+---+---+
To provide column names explicitly, follow the derived table name with a parenthesized list of column names:要显式提供列名,请在派生表名后面加上带圆括号的列名列表:
mysql> SELECT * FROM (SELECT 1, 2, 3, 4) AS dt (a, b, c, d);
+---+---+---+---+
| a | b | c | d |
+---+---+---+---+
| 1 | 2 | 3 | 4 |
+---+---+---+---+
A derived table can return a scalar, column, row, or table.派生表可以返回标量、列、行或表。
Derived tables are subject to these restrictions:派生表受以下限制:
A derived table cannot contain references to other tables of the same 派生表不能包含对同一SELECT
(use a LATERAL
derived table for that; see Section 13.2.11.9, “Lateral Derived Tables”).SELECT
的其他表的引用(为此使用横向派生表;请参阅第13.2.11.9节,“横向派生表”)。
Prior to MySQL 8.0.14, a derived table cannot contain outer references. 在MySQL8.0.14之前,派生表不能包含外部引用。This is a MySQL restriction that is lifted in MySQL 8.0.14, not a restriction of the SQL standard. 这是MySQL8.0.14中取消的MySQL限制,不是SQL标准的限制。For example, the derived table 例如,以下查询中的派生表dt
in the following query contains a reference t1.b
to the table t1
in the outer query:dt
包含对外部查询中的表t1
的引用t1.b
:
SELECT * FROM t1 WHERE t1.d > (SELECT AVG(dt.a) FROM (SELECT SUM(t2.a) AS a FROM t2 WHERE t2.b = t1.b GROUP BY t2.c) dt WHERE dt.a > 10);
The query is valid in MySQL 8.0.14 and higher. 该查询在MySQL 8.0.14及更高版本中有效。Before 8.0.14, it produces an error: 在8.0.14之前,它会产生一个错误:Unknown column 't1.b' in 'where clause'
The optimizer determines information about derived tables in such a way that 优化器以这样一种方式确定有关派生表的信息:EXPLAIN
does not need to materialize them. EXPLAIN
不需要具体化它们。See Section 8.2.2.4, “Optimizing Derived Tables, View References, and Common Table Expressions with Merging or Materialization”.请参阅第8.2.2.4节,“通过合并或物化优化派生表、视图引用和公共表表达式”。
It is possible under certain circumstances that using 在某些情况下,使用EXPLAIN SELECT
modifies table data. EXPLAIN SELECT
修改表数据是可能的。This can occur if the outer query accesses any tables and an inner query invokes a stored function that changes one or more rows of a table. 如果外部查询访问任何表,而内部查询调用更改表中一行或多行的存储函数,则会发生这种情况。Suppose that there are two tables 假设数据库t1
and t2
in database d1
, and a stored function f1
that modifies t2
, created as shown here:d1
中有两个表t1
和t2
,以及修改t2
的存储函数f1
,如下所示:
CREATE DATABASE d1; USE d1; CREATE TABLE t1 (c1 INT); CREATE TABLE t2 (c1 INT); CREATE FUNCTION f1(p1 INT) RETURNS INT BEGIN INSERT INTO t2 VALUES (p1); RETURN p1; END;
Referencing the function directly in an 在EXPLAIN SELECT
has no effect on t2
, as shown here:EXPLAIN SELECT
中直接引用函数对t2
没有影响,如下所示:
mysql>SELECT * FROM t2;
Empty set (0.02 sec) mysql>EXPLAIN SELECT f1(5)\G
*************************** 1. row *************************** id: 1 select_type: SIMPLE table: NULL partitions: NULL type: NULL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: NULL filtered: NULL Extra: No tables used 1 row in set (0.01 sec) mysql>SELECT * FROM t2;
Empty set (0.01 sec)
This is because the 这是因为SELECT
statement did not reference any tables, as can be seen in the table
and Extra
columns of the output. SELECT
语句没有引用任何表,这可以在输出的table
和Extra
列中看到。This is also true of the following nested 以下嵌套的SELECT
:SELECT
也是如此:
mysql>EXPLAIN SELECT NOW() AS a1, (SELECT f1(5)) AS a2\G
*************************** 1. row *************************** id: 1 select_type: PRIMARY table: NULL type: NULL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: NULL filtered: NULL Extra: No tables used 1 row in set, 1 warning (0.00 sec) mysql>SHOW WARNINGS;
+-------+------+------------------------------------------+ | Level | Code | Message | +-------+------+------------------------------------------+ | Note | 1249 | Select 2 was reduced during optimization | +-------+------+------------------------------------------+ 1 row in set (0.00 sec) mysql>SELECT * FROM t2;
Empty set (0.00 sec)
However, if the outer 但是,如果外部SELECT
references any tables, the optimizer executes the statement in the subquery as well, with the result that t2
is modified:SELECT
引用了任何表,优化器也会执行子查询中的语句,结果t2
被修改:
mysql>EXPLAIN SELECT * FROM t1 AS a1, (SELECT f1(5)) AS a2\G
*************************** 1. row *************************** id: 1 select_type: PRIMARY table: <derived2> partitions: NULL type: system possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 1 filtered: 100.00 Extra: NULL *************************** 2. row *************************** id: 1 select_type: PRIMARY table: a1 partitions: NULL type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 1 filtered: 100.00 Extra: NULL *************************** 3. row *************************** id: 2 select_type: DERIVED table: NULL partitions: NULL type: NULL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: NULL filtered: NULL Extra: No tables used 3 rows in set (0.00 sec) mysql>SELECT * FROM t2;
+------+ | c1 | +------+ | 5 | +------+ 1 row in set (0.00 sec)
This also means that an 这也意味着EXPLAIN SELECT
statement such as the one shown here may take a long time to execute because the BENCHMARK()
function is executed once for each row in t1
:EXPLAIN SELECT
语句(如此处所示)可能需要很长时间才能执行,因为BENCHMARK()
函数对t1
中的每一行执行一次:
EXPLAIN SELECT * FROM t1 AS a1, (SELECT BENCHMARK(1000000, MD5(NOW())));
The derived table optimization can also be employed with many correlated (scalar) subqueries (MySQL 8.0.24 and later). 派生表优化还可以用于许多相关(标量)子查询(MySQL 8.0.24及更高版本)。For more information and examples, see Section 13.2.11.7, “Correlated Subqueries”.有关更多信息和示例,请参阅第13.2.11.7节,“相关子查询”。