For example, if I do
SELECT ROWID, name from emp where age > 30;
As ROWID does not take any storage space, is it calculated every time a query like this runs?
From here,
Each table in an Oracle database
internally has a pseudocolumn named
ROWID. This pseudocolumn is not
evident when listing the structure of
a table by executing a SELECT * FROM
… statement, or a DESCRIBE …
statement using SQL*Plus, nor does the
pseudocolumn take up space in the
table.
Taking clue from the comments,
ROWID is stored when you create an index. Suppose I have no other index than the implicit one for the primary key (emp_id). In this case, will the above query go to this implicit index? How will the ROWID calculation happen?
Please note that name and age columns are not a part of the index.
Firstly, the “implicit index” is a real index. If we create a primary or unique key on a table and no index exists on the key column(s) Oracle we create an index, with the same name as the constraint.
Secondly, the query filters on the AGE column. So the optimizer would ignore any index on EMP_ID. On that case the database will do a full table scan of EMP, evaluating the value of each AGE column it retrieves. For each record where
AGE < 30it will concatenate the table’s object number, the block number, the slot number and the file number into a ROWID.If you want to understand more about ROWID have a play around with the DBMS_ROWID package. René Nyffenegger has a useful tutorial on his website. Find out more.
There’s one easy way to tell: experimentation. First we create an index on a table with a lot of records, and freshen the statistics:
Then we see how Oracle tackles the query:
If Oracle can satisfy the query with just indexed columns it doesn’t touch the table. Clearly here it is retreiving the ROWID from the index.