select * from orders where (customer_num=104 AND order_num>1001) OR order_num=1008 虽然在customer_num和order_num上建有索引,但是在上面的语句中优化器还是使用顺序存取路径扫描整个表。因为这个语句要检索的是分离的行的集合,所以应该改为如下语句:
select * from orders where customer_num=104 AND order_num>1001 UNION select * from orders where order_num=1008 这样就能利用索引路径处理查询。
select cust.name,rcvbles.balance,……other columns from cust,rcvbles where cust.customer_id = rcvlbes.customer_id AND rcvblls.balance>0 AND cust.postcode>“98000” ORDER BY cust.name 如果这个查询要被执行多次而不止一次,可以把所有未付款的客户找出来放在一个临时文件中,并按客户的名字进行排序:
select cust.name,rcvbles.balance,……other columns from cust,rcvbles where cust.customer_id = rcvlbes.customer_id AND rcvblls.balance>0 ORDER BY cust.name INTO TEMP cust_with_balance 然后以下面的方式在临时表中查询:
select * from cust_with_balance where postcode>“98000” 临时表中的行要比主表中的行少,而且物理顺序就是所要求的顺序,减少了磁盘I/O,所以查询工作?可以得到大幅减少。