Example of a join with indexes
The presence of indexes and constraints in query plans provides the optimizer with options that can greatly improve query-execution time.
- All rows of the customer table once
- All rows of the orders table once (because each order is associated with only one customer)
- Only rows in the items table that match pdnull rows from the customer-orders pairs
This query plan achieves a great reduction in cost compared with plans that do not use indexes. An inverse plan, reading orders first and looking up rows in the customer table by its index, is also feasible by the same reasoning.
The physical order of rows in a table also affects the cost of index use. To the degree that a table is ordered relative to an index, the overhead of accessing multiple table rows in index order is reduced. For example, if the orders table rows are physically ordered according to the customer number, multiple retrievals of orders for a given customer would proceed more rapidly than if the table were ordered randomly.
In some cases, using an index might incur additional costs. For more information, see Index lookup costs.