且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PostgreSQL在何处处理 sql查询之五十八

更新时间:2022-09-22 22:57:36

看到了其源代码中的一段注释,似乎认识又提高了一层:

PostgreSQL在何处处理 sql查询之五十八
/*
 *     INTERFACE ROUTINES
 *        ExecInitNode    -        initialize a plan node and its subplans
 *        ExecProcNode    -        get a tuple by executing the plan node
 *        ExecEndNode        -        shut down a plan node and its subplans
 *
 *     NOTES
 *        This used to be three files.  It is now all combined into
 *        one file so that it is easier to keep ExecInitNode, ExecProcNode,
 *        and ExecEndNode in sync when new nodes are added.
 *
 *     EXAMPLE
 *        Suppose we want the age of the manager of the shoe department and
 *        the number of employees in that department.  So we have the query:
 *
 *                select DEPT.no_emps, EMP.age
 *                where EMP.name = DEPT.mgr and
 *                      DEPT.name = "shoe"
 *
 *        Suppose the planner gives us the following plan:
 *
 *                        Nest Loop (DEPT.mgr = EMP.name)
 *                        /        \
 *                       /         \
 *                   Seq Scan        Seq Scan
 *                    DEPT          EMP
 *                (name = "shoe")
 *
 *        ExecutorStart() is called first.
 *        It calls InitPlan() which calls ExecInitNode() on
 *        the root of the plan -- the nest loop node.
 *
 *      * ExecInitNode() notices that it is looking at a nest loop and
 *        as the code below demonstrates, it calls ExecInitNestLoop().
 *        Eventually this calls ExecInitNode() on the right and left subplans
 *        and so forth until the entire plan is initialized.    The result
 *        of ExecInitNode() is a plan state tree built with the same structure
 *        as the underlying plan tree.
 *
 *      * Then when ExecutorRun() is called, it calls ExecutePlan() which calls
 *        ExecProcNode() repeatedly on the top node of the plan state tree.
 *        Each time this happens, ExecProcNode() will end up calling
 *        ExecNestLoop(), which calls ExecProcNode() on its subplans.
 *        Each of these subplans is a sequential scan so ExecSeqScan() is
 *        called.  The slots returned by ExecSeqScan() may contain
 *        tuples which contain the attributes ExecNestLoop() uses to
 *        form the tuples it returns.
 *
 *      * Eventually ExecSeqScan() stops returning tuples and the nest
 *        loop join ends.  Lastly, ExecutorEnd() calls ExecEndNode() which
 *        calls ExecEndNestLoop() which in turn calls ExecEndNode() on
 *        its subplans which result in ExecEndSeqScan().
 *
 *        This should show how the executor works by having
 *        ExecInitNode(), ExecProcNode() and ExecEndNode() dispatch
 *        their work to the appopriate node support routines which may
 *        in turn call these routines themselves on their subplans.
PostgreSQL在何处处理 sql查询之五十八

那么,我能否一定制造出 Nest Loop效果呢






本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2013/06/10/3130693.html,如需转载请自行联系原作者