MySQL---从代码角度理解SQL语句结构
2017-03-13 19:06
826 查看
MySQL的SQL语句,遵循SQL标准, 可以从语法定义的角度,了解SQL语句的构成并推测其执行方式(需要知道SQL原理).本文则是从内核的角度, 通过源码层来剖析SQL语句的构成(只以SELECT语句为例), 本文的意义,在于帮助内核实现者或者对内核对SQL实现感兴趣者掌握如何理解SQL的剖析(抛砖引玉, ^_^).如下是MySQL源码sql_lex.h的注释,非常重要, 本文以黑色字体给出MySQL原文, 蓝色字体标识出深入解析的内容, 红色标示特别强调的内容. /* (): Class . Class . (more than one means that we have a UNION query). These classes are connected as follows: Both classes have a master, a slave, a next and a prev field. For class SELECT_LEX, master and slave connect to objects of type SELECT_LEX_UNIT, whereas for class SELECT_LEX_UNIT, they connect to SELECT_LEX.
master is pointer to outer node. slave is pointer to the first inner node
neighbors are two SELECT_LEX or SELECT_LEX_UNIT objects on the same level.
The structures are linked with the following pointers: - list of neighbors (next/prev) (prev of first element point to slave pointer of outer structure) - For SELECT_LEX, this is a list of query blocks. - For SELECT_LEX_UNIT, this is a list of subqueries.
- pointer to outer node (master), which is If this is SELECT_LEX_UNIT - pointer to outer select_lex. If this is SELECT_LEX - pointer to outer SELECT_LEX_UNIT.
- pointer to inner objects (slave), which is either: If this is an SELECT_LEX_UNIT: - first query block that belong to this query expression. If this is an SELECT_LEX - first query expression that belong to this query block (subqueries).
- list of all SELECT_LEX objects (link_next/link_prev) This is to be used for things like derived tables creation, where we go through this list and create the derived tables.
If query expression contain several query blocks (UNION now, INTERSECT etc later) then it has a special select_lex called . It used for storing global parameters (like ORDER BY, LIMIT) and .
Subqueries used in global ORDER BY clause will be attached to this fake_select_lex, which will allow them to correctly resolve fields of the containing UNION and outer selects.
For example for following query:
*/
相关文章推荐
- mysql 显示SQL语句执行时间的代码
- MYSQL5.0 脚本测试笔记【复制表结构和数据SQL语句】
- MySQL复制表结构和内容到另一张表中的SQL语句
- MySQL复制表结构和内容到另一张表中的SQL语句
- MySQL复制表结构和数据SQL语句
- MySQL复制表结构和数据SQL语句
- mysql中查看表结构的sql语句
- MySql的sql语句中添加存储过程或者存储函数来实现Oracle中的start with ……connect by prior……递归(树形结构数据)查询
- MySQL复制表结构和内容到另一张表中的SQL语句
- sqlserver数据导入mysql二: 根据sqlserver表结构创建mysql表的perl代码
- SAP-COPA-从SQL语句的角度来理解 获利能力分析报表的特征及特征值
- mysql复制一个表结构的sql语句
- MySQL复制表结构和数据SQL语句
- MySQL复制表结构和表数据的SQL语句和时间函数
- MySQL复制表结构和数据SQL语句
- php与mysql建立连接并执行SQL语句的代码
- sql语句结构之查询语句(内含代码示例)
- mysql 查询指定日期时间内sql语句实现原理与代码
- MySQL复制表结构和内容到另一张表中的SQL语句
- Spark SQL深度理解篇:模块实现、代码结构及执行流程总览