Understanding SELECT_LEX & SELECT_LEX_UNIT
2013-01-16 13:09 心中无码 阅读(1057) 评论(0) 编辑 收藏 举报
Understanding SELECT_LEX & SELECT_LEX_UNIT
Louis Hust
0 Preface
SELECT_LEX(st_select_lex)&SELECT_LEX_UNIT(st_select_lex_unit) are two important structs in Optimizer. They are used from yacc grammer to senmantics and to optimizer at last. So they are also the basic of understanding the Optimizer.This article will explain what they stand for, but we will not deep into the concrete elements of each struct.
0 Introduction
SELECT_LEX: representing SELECT itself, that is, if there is a 'SELECT' keywork in SQL, there is a corresponding SELECT_LEX struct.
SELECT_LEX_UNIT: for grouping several selects in a bunch(in union), also can represent a single select.
All of the two structs are stored in LEX struct.There is a SELECT_LEX_UNIT and SELECT_LEX struct by default. If the SQL is a single SELECT or a union of several SELECTs, the default struct in LEX is enough, but if there is a subquery, there must be new SELECT_LEX AND SELECT_LEX_UNIT.
0 Examples
SELECT * FROM t1; unit | V select t1 SELECT * FROM t1 UNION SELECT * FROM t2; unit | V next select t1 --------> select t2 SELECT * FROM t1 where c1 in (select * from t11 union select * from t12); unit | V select t1 | V unit1.1 | V next select t11 ---------> select t12 SELECT * FROM t1 WHERE c1 in (SELECT * FROM t11) and c2 in (SELECT * FROM t12); unit1 | V select t1 | V next unit1.1 ------> unit1.2 | | V V select t11 select t12 At last, i give a sample in sql_lex.h. select * from table1 where table1.field IN (select * from table1_1_1 union select * from table1_1_2) union select * from table2 where table2.field=(select (select f1 from table2_1_1_1_1 where table2_1_1_1_1.f2=table2_1_1.f3) from table2_1_1 where table2_1_1.f1=table2.f2) union select * from table3; unit | V select table1------------> select table2 ------------>select table3 | | V |__________ unit1 | | | V | select table 1_1_1--->select table 1_1_2 V unit2 | V select table2_1_1 | V unit2.1 | V select table2_1_1_1_1
There is a funny thing i found that SELECT_LEX_UNIT alternates with SELECT_LEX.
0 Code Inspection
What we care about is when the new SELECT_LEX and SELECT_LEX_UNIT will be created. The answer is the same function: mysql_new_select.
This function will create a new SELECT_LEX, but SELECT_LEX_UNIT will be created only when the flag 'move_down' is true.
if move_down is true, it means that the SQL come into a SUBQUERY; otherwise, the SQL comes into a UNION.
References
File translated from
TEX
by
TTH,
version 4.03.
On 16 Jan 2013, 13:06.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库