EXPLAIN PLAN output is the debug output of the Oracle query optimizer. COST is the end result of a cost optimizer (CBO) whose goal is to choose which of the many possible plans should be used to trigger the request. CBO calculates the relative cost for each plan, then selects the plan with the lowest cost.
(Note: in some cases, CBO does not have enough time to evaluate all possible plans, in these cases, it simply selects the plan with the lowest cost found so far)
In general, one of the biggest contributions to a slow query is the number of lines read to service the request (more precisely, the blocks), so the cost will be partially based on the number of lines that the optimizer estimates will need to be read.
For example, let's say you have the following query:
SELECT emp_id FROM employees WHERE months_of_service = 6;
(The months_of_service column has a NOT NULL constraint on it and a regular index on it.)
There are two main plans that the optimizer can choose here:
- Plan 1: Read all the lines from the "employees" table, for each check whether the predicate is true (
months_of_service=6 ). - Plan 2. Read the index where
months_of_service=6 (this results in a set of ROWIDs), then access the table based on the returned ROWIDs.
Suppose that the employees table contains 1,000,000 (1 million) rows. Suppose further that the values for months_of_service vary from 1 to 12 and for some reason are fairly evenly distributed.
The cost of Plan 1 , which includes a FULL SCAN, will cost you to read all the rows in the employee table, which is approximately equal to 1,000,000; but since Oracle can often read blocks using multi-block reads, the actual cost will be lower (depending on how your database is configured) - for example, suppose the number of samples with multiple blocks is 10 - the estimated cost of a full scan is 1,000,000 / 10 ; Total cost = 100,000.
The cost of Plan 2 , which includes an INDEX RANGE SCAN scan and a table search by ROWID, will cost an index scan, as well as the cost of accessing the table using a ROWID. I will not go into how a range index scan will cost, but suppose that the cost of scanning an index index is 1 per row; we expect to find a match in 1 out of 12 cases, so the cost of scanning the index is 1,000,000 / 12 = 83,333; plus the cost of accessing the table (suppose that 1 block is read for access, we cannot use multi-block reads here) = 83.333; Total cost = 166 666.
As you can see, the cost of plan 1 (full scan) is less than the cost of plan 2 (index check + rowid access) - this means that CBO will choose FULL scan.
If the assumptions made here by the optimizer are true, then actually Plan 1 will be preferable and much more effective than Plan 2 - which refutes the myth that FULL scans are "always bad."
The results would be completely different if the optimizer’s goal was FIRST_ROWS (n) instead of ALL_ROWS - in this case, the optimizer would prefer plan 2 because it will often return the first few rows faster, at the cost of less efficiency for the entire query.