DATABASE MANAGEMENT SYSTEMS PRACTICE 1 DRAFT SOLUTION Remark: Solutions in Algebra reflects the choices and the values returned by the Oracle Optimizer. Query 1(a)
The Oracle optimizer prefers to use the hash join, even if Table DEPT is relatively small, because the hash join implementation in Oracle is very efficient. Indexes on primary keys are created by default, but they are not used for table access.
Query 1(b)
Changing the optimizer goal to /*+ FIRST_ROWS(1) */ (n=1) the optimizer prefers to use nested loop (with DEPT as inner table and EMP as outer table). The inner table is accessed using unique index scan, which exploits the index on the primary key. Increasing the value of n from 1 to 3 (or more) the optimizer chooses the same plan as in best throughout configuration (hash join and table access full for both tables).
Query 2 Since the hint is to avoid using hash, the Oracle optimizer prefers to use nested loop (with DEPT inner table and EMP outer table). The inner table is accessed using unique index scan, which exploits the index on the primary key. The outer table is accessed through a full table scan. After the join operation, the Oracle optimizer performs a group by hash.
Query 3 The Oracle optimizer performs two cascaded join operations: the first one between EMP and SALGRADE, the second one between DEPT and the result of the former join. Since the hint is to avoid using hash, the Oracle optimizer prefers to use nested loop to perform the last join (with DEPT as inner table). The inner table is accessed using unique index scan, which exploits the index on the primary key. The antijoin operation is still performed using hash. Table SALGRADE, which is a relatively small table, is accessed through full table scan.
Query 4 Without indexes:
Statistics related to Table EMP columns: After creating an index on attribute Sal of Table EMP (create index EmpSalInd ON EMP(sal);) and another index on attribute Deptno of Table EMP (create index DeptnoInd ON EMP(deptno);), the two indexes are used separately (index range scan) to perform selection operations on different attributes.
To improve query performance we investigate the use of a composite index composed of two attributes. From the analysis of the height-balanced histograms for columns DEPTNO and SAL on Table EMP, it turns out that DeptNo is more selective than Sal.
Hence, the overall cost (4) achieved by creating a composite index with Deptno as first attribute (create index DeptnoSalInd ON EMP(Deptno,sal);) is lower than the one (8) achieved by combining the same attributes in the opposite order (create index SalDeptnoInd ON EMP(Sal,Deptno);).
Query 5 Without indexes: Histograms related to attribute Job of EMP:
The selectivity of the selection criterion Job = PHILOSOPHER is high. Hence, an index on attribute Job of Table EMP is deemed useful for query optimization purposes. After creating an index on Job (create index EmpJobInd ON EMP(job);) the Oracle optimizer performs an index range scan on EMP.
Creating a joint index on Job and Deptno (create index EmpJobDeptnoInd ON EMP(job,deptno);) the Oracle optimizer uses that index instead of the previous one. However, the overall query execution cost remains the same.
Query 6 Without indexes:
After creating an index on attribute Job of Table EMP (create index EmpJobInd ON EMP(job);):
The selectivity of the selection criterion Job = PHILOSOPHER is high, whereas the one of Job = ENGINEER is low. Hence, on the two instances of the same table EMP (E1 and E2) the Oracle optimizer uses different access methods, i.e., it performs a full table scan on E2 and an index scan on E1.
Query 7 Without indexes:
Height-balanced histograms on attribute Hisal of Table SALGRADE:
Creating an index on Attribute Hisal of Table SALGRADE (create index HisalInd ON SALGRADE(hisal);) the Oracle optimizer performs a fast full scan on SALGRADE. Note that in the inner query the optimizer just needs to access data in the index itself, without accessing the table. The expression LNNVL("HISAL"<>:B1) is used to also manage NULL values, because attribute HISAL is nullable.