您的位置:首页 > 数据库 > MySQL

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:
  
 
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: