DEPENDENT SUBQUERY” 和 “SUBQUERY”
Posted zengkefu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DEPENDENT SUBQUERY” 和 “SUBQUERY”相关的知识,希望对你有一定的参考价值。
http://blog.163.com/li_hx/blog/static/183991413201642410122327/
mysql> CREATE TABLE t1 (a INT, b INT); Query OK, 0 rows affected (0.22 sec) mysql> CREATE TABLE t2 (c INT, d INT); Query OK, 0 rows affected (0.21 sec) mysql> CREATE TABLE t3 (e INT); Query OK, 0 rows affected (0.22 sec) mysql> INSERT INTO t1 VALUES (1,10), (2,10); Query OK, 2 rows affected (0.19 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> INSERT INTO t2 VALUES (2,10), (2,20); Query OK, 2 rows affected (0.17 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> INSERT INTO t3 VALUES (10), (30); Query OK, 2 rows affected (0.18 sec) Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from t1; +------+------+ | a | b | +------+------+ | 1 | 10 | | 2 | 10 | +------+------+ 2 rows in set (0.00 sec) mysql> select * from t2; +------+------+ | c | d | +------+------+ | 2 | 10 | | 2 | 20 | +------+------+ 2 rows in set (0.00 sec) mysql> select * from t3; +------+ | e | +------+ | 10 | | 30 | +------+ 2 rows in set (0.00 sec)
mysql> EXPLAIN SELECT * FROM t1 WHERE t1.a IN (SELECT c FROM t2 WHERE (SELECT e FROM t3 as t31) < SOME (SELECT e FROM t3 as t32 WHERE t1.b)); +----+--------------------+-------+------+---------------+------+---------+------+------+--------------------------------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+--------------------+-------+------+---------------+------+---------+------+------+--------------------------------------------------------------------+ | 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 2 | Using where | | 1 | PRIMARY | t2 | ALL | NULL | NULL | NULL | NULL | 2 | Using where; FirstMatch(t1); Using join buffer (Block Nested Loop) | | 4 | DEPENDENT SUBQUERY | t32 | ALL | NULL | NULL | NULL | NULL | 2 | Using where | | 3 | SUBQUERY | t31 | ALL | NULL | NULL | NULL | NULL | 2 | NULL | +----+--------------------+-------+------+---------------+------+---------+------+------+--------------------------------------------------------------------+ 4 rows in set (0.00 sec)
mysql> EXPLAIN format=json SELECT * FROM t1 WHERE t1.a IN (SELECT c FROM t2 WHERE (SELECT e FROM t3 as t31) < SOME (SELECT e FROM t3 as t32 WHERE t1.b))\G *************************** 1. row *************************** EXPLAIN: { "query_block": { "select_id": 1, "nested_loop": [ { "table": { "table_name": "t1", "access_type": "ALL", "rows": 2, "filtered": 100, "attached_condition": "<nop>(<in_optimizer>((/* select#3 */ select `test`.`t31`.`e` from `test`.`t3` `t31`),<exists>(/* select#4 */ select 1 from `test`.`t3` `t32` where (`test`.`t1`.`b` and <if>(outer_field_is_not_null, ((<cache>((/* select#3 */ select `test`.`t31`.`e` from `test`.`t3` `t31`)) < `test`.`t32`.`e`) or isnull(`test`.`t32`.`e`)), true)) having <if>(outer_field_is_not_null, <is_not_null_test>(`test`.`t32`.`e`), true))))", "attached_subqueries": [ { "dependent": true, "cacheable": false, "query_block": { "select_id": 4, "table": { "table_name": "t32", "access_type": "ALL", "rows": 2, "filtered": 100, "attached_condition": "(`test`.`t1`.`b` and <if>(outer_field_is_not_null, ((<cache>((/* select#3 */ select `test`.`t31`.`e` from `test`.`t3` `t31`)) < `test`.`t32`.`e`) or isnull(`test`.`t32`.`e`)), true))" } } }, { "dependent": false, "cacheable": true, "query_block": { "select_id": 3, "table": { "table_name": "t31", "access_type": "ALL", "rows": 2, "filtered": 100 } } } ] } }, { "table": { "table_name": "t2", "access_type": "ALL", "rows": 2, "filtered": 100, "first_match": "t1", "using_join_buffer": "Block Nested Loop", "attached_condition": "(`test`.`t2`.`c` = `test`.`t1`.`a`)" } } ] } } 1 row in set, 2 warnings (0.00 sec)
mysql> SHOW WARNINGS\G *************************** 1. row *************************** Level: Note Code: 1276 Message: Field or reference ‘test.t1.b‘ of SELECT #4 was resolved in SELECT #1 *************************** 2. row *************************** Level: Note Code: 1003 Message: /* select#1 */ select `test`.`t1`.`a` AS `a`,`test`.`t1`.`b` AS `b` from `test`.`t1` semi join (`test`.`t2`) where ((`test`.`t2`.`c` = `test`.`t1`.`a`) and <nop>(<in_optimizer>((/* select#3 */ select `test`.`t31`.`e` from `test`.`t3` `t31`),<exists>(/* select#4 */ select 1 from `test`.`t3` `t32` where (`test`.`t1`.`b` and <if>(outer_field_is_not_null, ((<cache>((/* select#3 */ select `test`.`t31`.`e` from `test`.`t3` `t31`)) < `test`.`t32`.`e`) or isnull(`test`.`t32`.`e`)), true)) having <if>(outer_field_is_not_null, <is_not_null_test>(`test`.`t32`.`e`), true))))) 2 rows in set (0.00 sec)
以上是关于DEPENDENT SUBQUERY” 和 “SUBQUERY”的主要内容,如果未能解决你的问题,请参考以下文章
Field Dependent 在Apex里使用control 和 dependent list-Picklist
HIVE SubQuery 表达式同时引用 Parent 和 SubQuery 表达式
Rails :dependent => :destroy VS :dependent => :delete_all
SQL Server 中的 SUBQUERY 和 EXISTS