Lab10 n01530481
Lab10 n01530481
Lab Exercise 11
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.
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.
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.
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?
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.
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.
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.
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.
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.
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.
Use SQL*Plus to create the rest of the lab that you did using SQL Developer. Compare the results between the two
different tools.