Note that there are some explanatory texts on larger screens.

plurals
  1. POIndexes, EXPLAIN PLAN, and record access in Oracle SQL
    primarykey
    data
    text
    <p>I have been learning about indexes in Oracle SQL, and I wanted to conduct a small experiment with a test table to see how indexes really worked. As I discovered from an earlier post made here, the best way to do this is with EXPLAIN PLAN. However, I am running into something which confuses me.</p> <p>My sample table contains attributes (EmpID, Fname, Lname, Occupation, .... etc). I populated it with 500,000 records using a java program I wrote (random names, occupations, etc). Now, here are some sample queries with and without indexes:</p> <p>NO INDEX:</p> <pre><code>SELECT Fname FROM EMPLOYEE WHERE Occupation = 'DOCTOR'; </code></pre> <p>EXPLAIN PLAN says:</p> <pre><code>OPERATION OPTIMIZER COST TABLE ACCESS(FULL) TEST.EMPLOYEE ANALYZED 1169 </code></pre> <p>Now I create index:</p> <pre><code>CREATE INDEX occupation_idx ON EMPLOYEE (Occupation); </code></pre> <p>WITH INDEX "occupation_idx":</p> <pre><code>SELECT Fname FROM EMPLOYEE WHERE Occupation = 'DOCTOR'; </code></pre> <p>EXPLAIN PLAN says:</p> <pre><code>OPERATION OPTIMIZER COST TABLE ACCESS(FULL) TEST.EMPLOYEE ANALYZED 1169 </code></pre> <p>So... the cost is STILL the same, 1169? Now I try this:</p> <p>WITH INDEX "occupation_idx":</p> <pre><code>SELECT Occupation FROM EMPLOYEE WHERE Occupation = 'DOCTOR'; </code></pre> <p>EXPLAIN PLAN says:</p> <pre><code>OPERATION OPTIMIZER COST INDEX(RANGE SCAN) TEST.OCCUPATION_IDX ANALYZED 67 </code></pre> <p>So, it appears that the index only is utilized when that column is the only one I'm pulling values from. But I thought that the point of an index was to unlock the entire record using the indexed column as the key? The search above is a pretty pointless one... it searches for values which you already know. The only worthwhile query I can think of which ONLY involves an indexed column's value (and not the rest of the record) would be an aggregate such as COUNT or something.</p> <p>What am I missing?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload