I am very new to Oracle exploring things,reading about how index access ,i got to come to a conclusion that ,suppose i have a table emp.
table emp
---------------------------
empno ename salary mgrid
---------------------------
1 ab 200 2
2 bn 900 3
3 bh 900 1
4 ah 890 4
5 kk 67 0
6 ac 9090
and this is how unique index on primary key empno is created
index table(suppose)
--------------------
rowid index value
---------------------
xyzz 1
- 2
- 3
- 4
- 5
- 6
where ‘-‘ means some hex value ,suppose i write a query that
select * from emp where empno ='5'
then it will take out the rowid corresponds to index value 5 and fetch the row corresponds to 5 empno from emp table
Now my doubt is that here it is fetching from index_table ,but to find rowid it is also scanning the whole table (index_table) till 5 ,and the same usses rowid to find the row,then how it is different from the table without index?,there also we are scanning the whole table till 5 empno.
only advantage is index are stored in ascending order.
i know i am wrong ,but can someone explain this
**Doubt**
Does a full table scan ,scans each and every rows column irrespective of it filter condition ?
lets take the query that i used above ,if there is no primary key on that table emp,then it will do full table scan ,then for empno=5 ,will it scan each and every row empno value or it will scan empname,salary and mrgid also?
Oracle (or any other database) is not scanning the entire index in the example you posted. And that’s the difference between a heap table and an index.
In very broad terms, you can compare an index with a phone book. If you want to find the phone number (the rowid) of Frank Miller in Georgetown (empno=5) you take the phone book and find the entries for Georgetown. You don’t have to read the entire phone book because you know the towns are ordered. So you open the phonebook in the middle and discover the town is Montana City. Since Montana City comes after Georgetown you open the phone book between your current opening and the beginning. With this tactic you go on until you find Georgetown. Then you do the same thing with the last name and the first name.
Again, this works because towns, last names and first names are alphabetically ordered. In the database, this is ensured with b-trees.
You probably want to read up on b-trees for further information, for example
on tahiti
Edit: You also asked if the database has to scan the entire table if there is no primary key. The answer is: yes, it has to if there is no index on empno, either.
In the phone book analogy, this would be a query like find all phone numbers whose address is 22 Elm Street. Since the phone book is not ordered by street name or house number, you have to read each entry in order to find the respective phone numbers.