列过滤器 的存在会更改查询计划。列过滤器是 WHERE 表达式,该表达式减少表加入到连接的行数。
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 AND O.paid_date IS NULL
图: 使用列过滤器的查询计划
for each row in the orders table do: read the row into O if O.paid_date is null then for each row in the customer table do: read the row into C 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 row and return to user end if end for end if end for end if end for
SELECT COUNT(*) FROM orders WHERE paid_date IS NULL
图: 以伪码编写的备用查询计划
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.paid_date is null and 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 row and return to user end if end for end if end for
图 1 和 图 2 中的查询计划以不同的顺序产生相同的输出。这两个查询计划的不同之处在于:一个读取表 pdnull 次,而另一个读取表 SELECT COUNT (*)FROM customer 次。通过选择适当的计划,优化器可以将成千上万的磁盘存取保存在某个实际的应用程序中。