0% found this document useful (0 votes)
23 views18 pages

Lab10 n01530481

The document provides instructions for a lab exercise on using explain plans to analyze query performance before and after adding indexes to a database table. Students are asked to: 1) Add a primary key to the CUSTOMERS table and observe the effect on a query. 2) Create indexes on various columns and columns combinations of the CUSTOMERS table. 3) Run queries and observe the explain plans before and after each index is added to see how the plans and costs are affected. Taking screenshots of the plans is required. Key concepts like gathering statistics, using SQL*Plus, and interpreting explain plans are also explained.

Uploaded by

smeet patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views18 pages

Lab10 n01530481

The document provides instructions for a lab exercise on using explain plans to analyze query performance before and after adding indexes to a database table. Students are asked to: 1) Add a primary key to the CUSTOMERS table and observe the effect on a query. 2) Create indexes on various columns and columns combinations of the CUSTOMERS table. 3) Run queries and observe the explain plans before and after each index is added to see how the plans and costs are affected. Taking screenshots of the plans is required. Key concepts like gathering statistics, using SQL*Plus, and interpreting explain plans are also explained.

Uploaded by

smeet patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

ITE 5323 Data Warehouse Fundamentals

Lab Exercise 11

Due: See Blackboard

Introduction to Explain Plans with Indexes

For this exercise, you will use your Data Warehouse image that is in the cloud. There are a few administrative things that need to be
done before we start the lab work.

For this exercise you can use the EXERCISE10 user you created for Lab 10.

You should see the CUSTOMERS table when you expand the Tables node for the EXERCISE10 user.

Notice the number of rows in the CUSTOMERS table.

Run the following to ensure the statistics are up to date on the CUSTOMERS table.
There are a few ways to view the EXPLAIN PLAN. It can be done in SQL Developer or in SQL*Plus.I would like to use this method, it
works in both SQL Developer and SQL*Plus.

How to view an explain plan:


We are working with a copy of the CUSTOMERS table that exists in the SH schema. They do not include the SH schema in the current
releases of Oracle. So you are creating the CUSTOMERS table from a download.
How many rows exist where the cust_last_name is Maine? Let’s ask the database, notice there are 78 rows of 55,000 rows.
How to view an explain plan:

We are working with a copy of the CUSTOMERS table that exists in the SH schema. They do not include the SH schema in the current
releases of Oracle. So you are creating the CUSTOMERS table from a download.

How many rows exist where the cust_last_name is Maine? Let’s ask the database, notice there are 78 rows of 55,000 rows.

This query retrieved 78 rows from the table. Let us look at the explain plan for this query.

The query had to do a Full Table Scan to retrieve the data.

Perform the EXPLAIN PLAN.


Display results of the PLAN TABLE

This second method will also work in SQL*Plus.

With this method a PLAN TABLE Is used to hold the results of the EXPAIN PLAN. We must display the contents of the PLAN TABLE.
We are now going to add an object to the database.
How does this plan compare to the previous plan we executed? What do you think caused the increase in speed of the query? Scroll
over to see the cost now, is it less?

Let us look at the method to do this.

How does this plan compare to the previous plan we executed? What do you think caused the increase in speed of the query? Scroll
over to see the cost now, is it less? This is shown in the two different methods used.
Effect of using wild cards with LIKE. The following is the same query but instead of specifying the actual value the letter M was used
with the % wildcard.

Notice the number of Rows increased as did the Cost. Still used the index. Not nearly as efficient as entering the actual value.
Let us now investigate a few other occurrences. Look at the following query.

Will this query use the index that was created?

Answer: yes, it will

Reverse the order of the columns.


Yes, it still works.
We will now try to search for a different value in the same table.

The EXPLAIN PLAN for the above query using the PLAN TABLE method

Again, a full table scan is done. CUST_ID is the PRIMARY KEY. Has it been defined? Now that you have an idea of how this is
done you are to apply some objects to the CUSTOMER table and run the explain plans before and after to see the difference.
Scroll over to check the cost. Does the value look familiar?
These are things to do on your own during the lab class. This is due by the end of class today. Take a screen capture of the steps
you perform.

1. Add a primary key to the CUSTOMERS table.

2. Re-execute the query you did to look for CUST_ID 8750. Notice the differences between the two queries. Take a screen
capture of this query.
3. Create a query that will show customers in the CUST_CITY of Wellington. Show the EXPLAIN PLAN using the EXPLAIN
PLAN FOR method.

4. Create an index on CUST_CITY.

5. Execute the query in question 3 to show the result after the index has been created.
6. Create a query to show all the customers in Buffalo, NY.

7. Show the EXPLAIN PLAN for the query.


8. Create an index on the following two columns, CUST_CITY and CUST_STATE_PROVINCE.

9. Create a query to show the customers in Buffalo, NY. Same query as Question 6.
10. Show the EXPLAIN PLAN for the query.

11. Create a new index for the CUST_LAST_NAME and CUST_FIRST_NAME columns.
12. Create a query to show all customers with the First Name of Gale and the last name of Wright.

13. Show the explain plan for the query. Notice what index if any was being used.
Terms Explained

Gathering Statistics

All the results shown depend on whether you are working with up-to-date statistics. You may ask what that means. The database
will periodically run statistics on the tables in the various schemas to determine what are in the tables in the way of number of rows.
Only then can the optimizer provide accurate results for the explain plan. If it does not know the exact statistics it will use what it
thinks to be accurate. These could be way off. You can analyze a table and indexes manually if you need to.

Using SQL*Plus

In SQL*Plus you need to create the PLAN TABLE to store the execution plans. There is a script to run to do this.

Open a terminal session. Connect as your new user, EXERCISE10.

The PLAN TABLE needs to be created for this user.

Using the following to EXPLAIN the same SQL statement we first did.
Display the EXPLAIN PLAN for the SQL statement just entered.
Your result is likely different from the one I show here. You created an index previously; the index would be used. To see
the same result, I see you need to drop the index that you created.

DROP INDEX cust_last_name_idx;

Then run the explain plan again.

Use SQL*Plus to create the rest of the lab that you did using SQL Developer. Compare the results between the two
different tools.

You might also like