• Understanding SELECT_LEX & SELECT_LEX_UNIT


    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

    [1]
    Structure Of Complex Select




    File translated from TEX by TTH, version 4.03.
    On 16 Jan 2013, 13:06.

    踏着落叶,追寻着我的梦想。转载请注明出处
  • 相关阅读:
    C#下编程完成IIS网络App的权限设置
    IIS6与IIS7在编程实现HTTPS绑定时的细微差别
    Android 对话框(Dialog)大全
    Android 开发中使用Intent传递数据的方法
    设计模式--模版设计模式
    android 布局页面文件出错故障排除Exception raised during rendering: java.lang.System.arraycopy([CI[CII)V
    viewPager的切换动画
    设计模式--状态模式
    git学习
    二〇一五年五月二十二日--bug--启动页面出现模糊的问题
  • 原文地址:https://www.cnblogs.com/nocode/p/2862560.html
Copyright © 2020-2023  润新知