下面我们再来看下通过with as materialize优化subquery unnesting的例子。
首先看个Not in Subquery的SQL: // test_partition1 和 test_partition2为Hive外部分区表 select * from test_partition1...org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe, [id#7, name#8], [dt#9] 通过上述逻辑计划和物理计划可以看出,Spark SQL在对not in subquery...因此,在实际生产中,要尽可能利用其他效率相对高的SQL来避免使用Not in Subquery。...虽然通过改写Not in Subquery的SQL,进行低效率的SQL到高效率的SQL过渡,能够避免上面所说的问题。...这里笔者给出一个思路,就是解析Spark SQL计划,根据Spark SQL的join策略匹配条件等,来判断任务中是否使用了低效的Not in Subquery进行预警,然后通知业务方进行修改。
首先来说说相对简单一点子查询展开机能(Subquery Unnesting)。...子查询展开机能(Subquery Unnesting) 通常情况下,SQL 的特点是用到了 IN,NOT IN, EXISTS, NOT EXISTS 子句。...有以下两种方法: 隐含参数 _UNNEST_SUBQUERY 设置成 false OR 最开始例子里面用到的 NO_UNNEST hint。
子查询关联集展开机能(unnest correlation set subquery) 这个机能,我在 Google 上查了一下,分享的文章特别少,可能是很少被关注到吧。...没用到子查询关联集展开机能之前,执行计划和【子查询展开机能(Subquery Unnesting)】一节中讲到的没用子查询展开是的效果一样。
== RTE_SUBQUERY is_simple_subquery:不全部列举了,其中重要的是子查询不能带有一些特殊的语法: is_simple_subquery ......if (subquery->hasAggs || subquery->hasWindowFuncs || subquery->hasTargetSRFs || subquery->groupClause...|| subquery->groupingSets || subquery->havingQual || subquery->sortClause || subquery->distinctClause...|| subquery->limitOffset || subquery->limitCount || subquery->hasForUpdate || subquery->cteList...pull_up_simple_subquery return (Node *) subquery->jointree; 返回一个jointree带着条件。
简单子查询指简单的查询语句或者join语句组成,由函数is_simple_subquery判断: bool is_simple_subquery(PlannerInfo *root, Query *subquery...IsA(subquery, Query) || subquery->commandType != CMD_SELECT || subquery->utilityStmt !...(subquery->hasAggs || subquery->hasWindowFuncs || subquery->groupClause || subquery->havingQual...|| subquery->windowClause || subquery->sortClause || subquery->distinctClause || subquery...->limitOffset || subquery->limitCount || subquery->hasForUpdate || subquery->cteList ||
SUBQUERY First SELECT in subquery DEPENDENT SUBQUERY First SELECT in subquery, dependent on outer query...DERIVED Derived table UNCACHEABLE SUBQUERY A subquery for which the result cannot be cached and must...:subquery的子查询要受到外部表查询的影响 explain select * from emp e where e.deptno in (select distinct deptno from dept...:利用索引来关联子查询,不再扫描全表 explain select * from emp where emp.job in (select job from t_job); --unique_subquery...:该连接类型类似与index_subquery,使用的是唯一索引 explain select * from emp e where e.deptno in (select distinct deptno
SUBQUERY None First SELECT in subquery DEPENDENT SUBQUERY dependent (true) First SELECT in subquery,...dependent on outer query DERIVED None Derived table SELECT (subquery in FROM clause) MATERIALIZED materialized_from_subquery...Materialized subquery UNCACHEABLE SUBQUERY cacheable (false) A subquery for which the result cannot...:除了from字句中包含的子查询外,其他地方出现的子查询都可能是subquery G:dependent subquery:与dependent union类似,表示这个subquery的查询要受到外部表查询的影响...4、type 依次从好到差:system,const,eq_ref,ref,fulltext,ref_or_null,index_merge,unique_subquery,index_subquery
A) The subquery is executed for every row in the EMPLOYEES table....B) The subquery is not a correlated subquery....C) The subquery is executed before the DELETE statement is executed....E) The DELETE statement executes successfully even if the subquery selects multiple rows.
子查询(SubQuery)又如何处理?对表达式(Expression)如何进行优化?如何寻找最优的查询计划(Cheapest Plan)?...完成对tuple_faction的设置后,进入后续优化流程,subquery_planner的函数原型如下所示。 ? 这里也许读者会迷惑,为什么是subquery_planner呢?...因此,使用subquery_planner作为我们查询优化的入口,虽然从函数名上来看其似乎是用于子查询语句的处理。...那么subquery_planner函数似乎也应该有相应于这些语句的优化处理。就这点而言,subquery_planner与原始语法树到查询树的转换所采取的处理方式相似。...这里需要读者注意的一点就是查询计划的生成部分,PostgreSQL将查询计划的生成也归入subquery_planner中,但为了方便问题的讨论,我们并未将查询计划的生成部分在subquery_planner
例如: WITH subquery_1 AS ( SELECT a1, a2, a3 FROM Table_1 WHERE a3 between 20180101 and 20180131...), /*子查询subquery_1,注意:多个子查询需要用逗号分隔*/ subquery_2 AS ( SELECT b1, b2, b3 FROM Table...*/ SELECT subquery_1.a1, subquery_1.a2, subquery_2.b1, subquery_2.b2 FROM subquery_...1 JOIN subquery_2 ON subquery_1.a3 = subquery_2.b3; 5.4 利用子查询,减少读表的次数,尤其是大数据量的表 具体做法是,将使用频繁的表作为一个子查询抽离出来
A) The subquery is executed before the UPDATE statement is executed....C) The subquery is executed for every updated row in the ORDERS table....D) The UPDATE statement executes successfully even if the subquery selects multiple rows....E) The subquery is not a correlated subquery. Answer:BD (解析:这道题考的就是关联子查询,类似题目以前有考过。
、scala subquery都会使id递增 1.2 select type simple 不使用union或者subquery的简单query 子查询被优化器打开,失效了 primary 使用union...结合select时,第一个select type subquery的query union 使用union结合select除了第一个select type为primary,其余为union(extra中...union result是union去掉重复值的临时表) 5.7开始union all不会出现union result,因为不去重 subquery 不是用在from后面的subquery 和外部表无关联...subquery(标量子查询)执行计划没错误,不代表sql执行没错(不能超过1行数据,subquery return more than 1 row) dependent subquery 必须依附于外面的值...scala subquery(和外部有关系的标量子查询) exists derived unin/union all group by distinct 聚合函数 limit @ from位置之后的subquery
例如: WITH subquery_1 AS ( SELECT a1, a2, a3 FROM Table_1 WHERE a3 between 20180101 and 20180131...), /*子查询subquery_1,注意:多个子查询需要用逗号分隔*/ subquery_2 AS ( SELECT b1, b2, b3 FROM Table...*/ SELECT subquery_1.a1, subquery_1.a2, subquery_2.b1, subquery_2.b2 FROM subquery_...1 JOIN subquery_2 ON subquery_1.a3 = subquery_2.b3; 利用子查询,减少读表的次数,尤其是大数据量的表 具体做法是,将使用频繁的表作为一个子查询抽离出来
filtered 按表条件过滤的行百分比 Extra 执行情况的描述和说明 select_type 可以不记住 SIMPLE PRIMARY UNION、DEPENDENT UNION、UNION RESULT SUBQUERY...、DEPENDENT SUBQUERY 派生表 DERIVED、DEPENDENT DERIVED 物化子查询 MATERIALIZED 无法缓存结果子查询 UNCACHEABLE SUBQUERY、UNCACHEABLE...system 表只有一行 const 常量 eq_ref 唯一索引 ref 非唯一索引或or fulltext ref_or_null 可空索引 index_merge 索引合并优化 unique_subquery...唯一索引 in (select ...) index_subquery 非唯一索引 in (select ...) range 索引区间 index 全二级索引(同ALL) ALL 全表 range
SUBQUERY(subquery):子查询中的第一个SELECT,结果不依赖于外部查询。...DEPENDENT SUBQUERY(dependent subquery):子查询中的第一个SELECT,依赖于外部查询。...UNCACHEABLE SUBQUERY(uncacheable subquery):(一个子查询的结果不能被缓存,必须重新评估外链接的第一行) 3 table 输出结果集的表名称。...) unique_subquery是一个索引查找函数,可以完全替换子查询,效率更高。...【不常用】 index_subquery:该联接类型类似于unique_subquery。
predicate = cb.and(predicate,predicateOr); } return predicate; }, pageable); } 多表联查 Subquery... subquery = query.subquery(Long.class); Root cateXrefRoot = subquery.from(CategoryTreeXref.class...); subquery.select(cateXrefRoot.get("childId")); subquery.where(cb.equal(cateXrefRoot.get("parentId...caseInfo.getCategory().getId())); predicate = cb.and(predicate,cb.in(root.get("category").get("id")).value(subquery
SUBQUERY:表示子查询中的第一个查询语句。 DEPENDENT SUBQUERY:含义与SUBQUERY几乎相同,但是DEPENDENTSUBQUERY取决于外层的查询语句。...UNCACHEABLE SUBQUERY:表示不缓存子查询的结果数据,重新计算外部查询的每一行数据。...> index_subquery > range > index > ALL system:查询的数据表中只有一行数据,是const类型的特例。...unique_subquery:当查询语句的查询条件为IN的语句,并且IN语句中的查询字段为数据表的主键或者非空唯一索引字段时,type的取值为unique_subquery。...index_subquery:与unique_subquery类似,但是IN语句中的查询字段为数据表中的非唯一索引字段。
. row *************************** -> Nested loop inner join (cost=4.95 rows=9) -> Filter: (`subquery2...>`.b is not null) (cost=2.83..1.80 rows=9) -> Table scan on subquery2> (cost=0.29..2.61 rows...null) (cost=2.83..1.80 rows=9) (actual time=0.097..0.100 rows=9 loops=1) -> Table scan on subquery2...(cost=2.06 rows=9) (actual time=0.035..0.038 rows=9 loops=1) -> Index lookup on t1 using a (a=`subquery2...重要信息 以下面为例: Index lookup on t1 using a (a=`subquery2>`.b) (cost=2.35 rows=1) (actual time=0.015..0.017
, dependent on outer queryDERIVEDDerived tableUNCACHEABLE SUBQUERYA subquery for which the result cannot...(see UNCACHEABLE SUBQUERY) 上面的解释你肯定看的云里雾里的,我们实际来写SQL看看每种出现的情景。...> index_subquery > range > index > ALL 效率最低的为ALL,表示全表扫描了。...EXPLAIN SELECT id from member WHERE CODE = 99 OR CODE = 100; index_subquery:利用索引来关联子查询,不再扫描全表。...unique_subquery:该连接类型类似与index_subquery,使用的是唯一索引:该连接类型类似与index_subquery,使用的是唯一索引。 没有写出可验证的SQL。
领取专属 10元无门槛券
手把手带您无忧上云