Overview of Practices for Lesson 6
Practice 6-1: Using Different Access Paths
1. Case 1: With and Without Index
a. Use the SQL Developer files tab and open
$HOME/solutions/Access_Paths/ap_setup.sql script.
b. Select sys_connection.
c. Execute the script (F5).
2. Open idx_setup.sql.
Oracle University and InfoTech (Pvt.) Ltd use only
3. Execute it with sh_connection.
− Output
drop table mysales purge
Error starting at line 3 in command:
drop table mysales purge Error report:
SQL Error: ORA-00942: table or view does not exist 00942. 00000 - "table or view does not exist"
*Cause:
*Action:
create table mysales as select * from sh.sales
create table succeeded.
insert into mysales select * from mysales 918843 rows inserted
commit commited
insert into mysales select * from mysales 1837686 rows inserted
commit commited
insert into mysales select * from mysales 3675372 rows inserted
commit
Oracle University and InfoTech (Pvt.) Ltd use only
commited
insert into mysales select * from mysales 7350744 rows inserted
commit commited
insert into mysales select * from mysales 14701488 rows inserted
commit commited
insert into mysales values (0,0,sysdate,0,0,0,0) 1 rows inserted
commit commited
exec dbms_stats.gather_schema_stats('SH') anonymous block completed
4. What do you observe when you execute the following query in the sh_connection worksheet.
a. Right-click sh_connection and select Open SQL Worksheet.
b. Autotrace the query.
Select * from mysales where prod_id=0;
c. Observe the output.
Oracle University and InfoTech (Pvt.) Ltd use only
− Basically, there are no indexes on the MYSALES table.
− The only possibility for the optimizer is to use the full table scan to retrieve only one row. You can see that the full table scan takes a long time.
5. To enhance the performance of the query in step 4, reexecute the query in step 4 after executing create_mysales_index.sql.
a. Open the create_mysales_index.sql file and execute it with sh_connection.
b. Autotrace the query in step 5 again.
c. Observe the output.
Oracle University and InfoTech (Pvt.) Ltd use only
− You can see a dramatic improvement in performance. Notice the difference in time, cost, and physical reads.
6. Clean up your environment for case 1 by executing the idx_cleanup.sql script.
7. Case 2: Compare Single Column Index Access: Open a terminal window. Connect to sh and drop all indexes currently created on the CUSTOMERS table except its primary key index by executing drop_customers_indexes.sql.
$ cd /home/oracle/solutions/Access_Paths/
$ sqlplus sh/sh
…
Oracle University and InfoTech (Pvt.) Ltd use only
Connected to:…
SQL> @drop_customers_indexes.sql
8. Autotrace the query in query00.sql. What do you observe?
9. Create three B*-tree indexes on the following CUSTOMERS table columns by using sh_connection:
cust_gender cust_postal_code cust_credit_limit
a. Open and execute the create_cust_gender_index.sql script.
Oracle University and InfoTech (Pvt.) Ltd use only
b. Open and execute the create_cust_postal_code_index.sql script.
c. Open and execute the create_cust_credit_limit_index.sql script.
Oracle University and InfoTech (Pvt.) Ltd use only
d. To verify that the indexes exist, execute list_customers_indexes.sql.
10. Start monitoring all the CUSTOMERS indexes. Notice that the value in the USED column is NO.
a. Open and execute the start_monitoring_indexes.sql script using sh_connection.
Oracle University and InfoTech (Pvt.) Ltd use only
b. Open and execute the statement (Ctrl + Enter) in the show_index_usage.sql script.
11. Autotrace the query in query01.sql. What do you observe?
Hint: Check the estimated cost in the execution plan and the sum of db block gets and consistent gets in the statistics.
Oracle University and InfoTech (Pvt.) Ltd use only
− The optimizer chooses to use only one index to do a full scan. The cost is lower than the full table scan.
12. Autotrace the query in query02.sql using sh_connection. What do you observe?
SELECT /*+ INDEX_COMBINE(c) */ c.*
FROM customers c
WHERE cust_gender = 'M' AND cust_postal_code = 40804 AND cust_credit_limit = 10000;
Oracle University and InfoTech (Pvt.) Ltd use only
− This time the optimizer uses multiple indexes and combines them to access the table. However, the cost is higher than that from the previous step, but is still lower than the full table scan.
13. Execute show_index_usage.sql to confirm the list of indexes that were accessed in this case.
14. Case 3: Concatenated Index: Open a terminal window. Connect to sh and drop all indexes currently created on the CUSTOMERS table except its primary key index by executing drop_customers_indexes.sql.
$ cd /home/oracle/solutions/Access_Paths/
$ sqlplus sh/sh
…
Connected to:…
Oracle University and InfoTech (Pvt.) Ltd use only
SQL> @drop_customers_indexes.sql
15. Open and execute the create_gender_limit_code_index.sql script using sh_connection to make sure that you create a concatenated index on the following CUSTOMERS columns, and in the order mentioned here:
cust_gender
cust_credit_limit cust_postal_code
16. Autotrace the query in query01.sql. What do you observe?
Oracle University and InfoTech (Pvt.) Ltd use only
− The optimizer uses your concatenated index, and the resulting cost is by far the best compared to the previous steps.
17. Autotrace the query in query03.sql. What do you observe?
Oracle University and InfoTech (Pvt.) Ltd use only
− The query is almost the same as in the previous step, but the predicate on cust_postal_code is removed. The optimizer can still use the concatenated index, but the resulting cost is much higher because neither cust_credit_limit nor cust_gender are very selective.
18. Autotrace the query in query04.sql. What do you observe?
Oracle University and InfoTech (Pvt.) Ltd use only
− You replaced cust_credit_limit with cust_postal_code, which has better selectivity. The index is used and the resulting cost is better.
19. Autotrace the query in query05.sql. What do you observe?
Oracle University and InfoTech (Pvt.) Ltd use only
− The leading part of the concatenated index is no longer part of the query. However, the optimizer is still able to use the index by doing a full index scan.
20. Case 4: Bitmap Index Access: Open a terminal window. Connect to sh and drop all indexes currently created on the CUSTOMERS table except its primary key index by executing drop_customers_indexes.sql.
$ cd /home/oracle/solutions/Access_Paths/
$ sqlplus sh/sh
…
Connected to:…
SQL> @drop_customers_indexes.sql
21. Open and execute three scripts using sh_connection to create three different bitmap indexes on the following columns of the CUSTOMERS table:
cust_gender (create_cust_gender_bindex.sql)
cust_postal_code (create_cust_postal_code_bindex.sql) cust_credit_limit create_cust_credit_limit_bindex.sql) a. Open and execute the create_cust_gender_bindex.sql script.
Oracle University and InfoTech (Pvt.) Ltd use only
b. Open and execute the create_cust_postal_code_bindex.sql script.
c. Open and execute the create_cust_credit_limit_bindex.sql script.
d. Confirm that the indexes have been created by executing the statement in the list_customers_indexes.sql script.
Oracle University and InfoTech (Pvt.) Ltd use only
22. Autotrace the query inquery02.sql. What do you observe?
− The optimizer uses only two bitmap indexes to solve this query. However, the cost is not good. The cost is a little lower than the cost of the full table scan.
Oracle University and InfoTech (Pvt.) Ltd use only
23. Case 5: Complex Predicate with Bitmap Indexes: Open a terminal window. Connect to sh and drop all indexes currently created on the CUSTOMERS table except its primary key index by executing drop_customers_indexes.sql.
$ cd /home/oracle/solutions/Access_Paths/
$ sqlplus sh/sh
…
Connected to:…
SQL> @drop_customers_indexes.sql
24. After this, create two bitmap indexes on the following columns of the CUSTOMERS table:
cust_year_of_birth (create_cust_year_of_birth_bindex.sql) cust_credit_limit (create_cust_credit_limit_bindex.sql)
a. Open and execute the create_cust_year_of_birth_bindex.sql script.
b. Open and execute the create_cust_credit_limit_bindex.sql script.
25. Autotrace the query inquery07.sql. What do you observe?
Oracle University and InfoTech (Pvt.) Ltd use only
− The query has a complex WHERE clause that is well suited for using bitmap indexes.
The optimizer uses two bitmap indexes and the resulting cost is better than the full table scan cost.
26. Make sure that the optimizer can no longer use the bitmap index you created on the cust_year_of_birth column.
The best solution is to render it invisible. Open and execute the alter_cust_yob_idx.sql script. Verify that the
optimizer_use_invisible_indexes parameter is set to FALSE.
select * from v$parameter
where name = 'optimizer_use_invisible_indexes';
alter index cust_cust_year_of_birth_bidx invisible;
select index_name, visibility from user_indexes where table_owner='SH' and table_name='CUSTOMERS';