Indexes and View and Other Important Interview Questions
Indexes and View and Other Important Interview Questions
Indexes are used to access data in the table faster. A book is like a table of contents.
When searching for a topic in the book, we can quickly find out the page number of the
topic we’re looking for from the table of contents.
If the book doesn’t have a table of contents, we’ll cycle through the pages until we find
the topic we’re looking for. The indexes in our database are the same. Stores a row
in our table and the ROWID information that corresponds to that row.
When a data is searched in a table, Oracle first checks the presence of the index. If
there is no index, it reads all the records in the table until you find the desired record.
This is known as FULL TABLE SCAN
When we encounter a low-performing SQL, the first thing we usually check is that the
index we’ve used in the WHERE condition of the query is indexed?
B-tree Index
The default index type in the Oracle database is B-tree (Balanced Tree) index. –
always use for key columns
CREATE INDEX JOB_INDX ON OT.EMPLOYEES(JOB_ID);
Bitmap Index
This index type is the most commonly used index type after B-Tree. This type of index is
used when certain values repeat continuously.
CREATE BITMAP INDEX EXP_1 ON HR.EMPLOYEES(GENDER);
Unique Index
This type of index is the advanced version of the B-Tree index structure. Normally, the
B-Tree index structure is Non-Unique.
When we use the Unique keyword to create this index type, Oracle will create a Unique
index.
This type of index is especially used on columns with unique records. It is created as
follows.
Create unique index MTC_IX on customer(SSID);
Employees may have the same name or surname. We’ve done compression on the
concatenated index.
Simple views
Complex views
Eg 2:
CREATE VIEW employeeview1 AS
SELECT
employee_id,
first_name || ' ' || last_name full_name,
phone_NUMBER
FROM
employees;
We can now check the just created above Oracle VIEW by using this query.
Select * from employeeview;
SELECT *
FROM employeeview
WHERE years > 10
ORDER BY full name;
INLINE VIEW ( IMPORTANT INTERVEW QUESTION)
An inline view is not a real view but a subquery in the FROM clause of a SELECT
statement. Consider the following SELECT statement:
SELECT
column_list
FROM
(
SELECT
*
FROM
table_name
) t;
The subquery specified in the FROM clause of a query is called an inline view.
Because an inline view can replace a table in a query, it is also called a derived
table. It is also called as subselect.
Eg 1 :
The following query retrieves the top 10 most expensive products from the
products table:
SELECT
*
FROM
(
SELECT
product_id,
product_name,
list_price
FROM
products
ORDER BY
list_price DESC
)
WHERE
ROWNUM <= 10;
In this example, first, the inline view returns all products sorted by list prices in
descending order. And then the outer query retrieves the first 10 rows from the
inline view.