查询计划执行示例

本主题包含用调用三向连接的 SELECT 语句进行的查询的示例,并描述一个可能的查询计划。

以下 SELECT 语句要求三向连接:
SELECT C.customer_num, O.order_num
   FROM customer C, orders O, items I
   WHERE C.customer_num = O.customer_num
      AND O.order_num = I.order_num
还假定三个表都没有索引。假设优化器为两个连接都选择 customer-orders-items 路径和嵌套循环连接(实际上,优化器常常为两个没有连接列索引的表选择散列连接)。图 1 显示了查询计划,该查询计划是以伪码编写的。 有关说明查询计划信息的信息,请参阅显示由优化器选择的查询计划的报告

图: 以伪码编写的查询计划

      for each row in the customer table do:
            read the row into C
   for each row in the orders table do:
         read the row into O
               if O.customer_num = C.customer_num then
                     for each row in the items table do:
                           read the row into I
                           if I.order_num = O.order_num then
                              accept the row and send to user
               end if
            end for 
         end if
      end for 
   end for 
该过程读取以下行:

该示例没有描述唯一可能的查询计划。 另一个计划只是将 customerorders 的角色对调了一下:对于 orders 的每一行,它读取 customer 的所有行,以寻找匹配的 customer_num。 该计划按不同的顺序读取相同数量的行并按不同的顺序产生相同的行集合。在该示例中,两个可能的查询计划需要做的工作量不存在差异。