本主题包含用调用三向连接的 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
图: 以伪码编写的查询计划
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
该示例没有描述唯一可能的查询计划。 另一个计划只是将 customer 和 orders 的角色对调了一下:对于 orders 的每一行,它读取 customer 的所有行,以寻找匹配的 customer_num。 该计划按不同的顺序读取相同数量的行并按不同的顺序产生相同的行集合。在该示例中,两个可能的查询计划需要做的工作量不存在差异。