Muhammad Nadeem Chaudhry's Blog


SQL Tuning Advisor and SQL Access Advisor in Oracle 10g

Posted in Uncategorized by Muhammad Nadeem Chaudhry on December 28, 2010

Oracle 10g with its AWR (Automatic Workload Repository) for automatic collection of performance statistics and with ADDM (Automatic Database Diagnostic Monitor) for identifying root causes of problems is a self-healing and self-tuning getting wiser every day and very soon will start to think on its own database.
There are features in Oracle 10g which help you to quickly find out what is wrong with the database. Features like ASH (Active Session History) to peak into what your active sessions are waiting for and Server Generated Alerts with Metrics and Thresholds are all tools which tell you what is really going on in your database. The OEM (Oracle Enterprise Manager) Database control is a brilliant interface you can use to learn and find out more about all these smart enhancements. All in the name of pro-active database management.
What is database well-being all about anyway? Isn’t it high throughput (OLTP) and response time (DSS)? Aren’t all databases working to achieve these?
Oracle 10g has also an Advisory Framework which based on workload, will also try to advise you on configuration issues like Buffer Cache, Library Cache, PGA, Undo and Segments used. There are also Tuning-Related Advisors which deal with SQL Tuning and these are:

I – SQL Tuning Advisor
II – SQL Access Advisor

In this post I will try to explain the usage of these two advisors in the job of tuning bad and nasty SQL statements.

All automation is good, self healing databases are very good. But what happens in the development arena when a developer turns up to your desk pointing to an email he/she just sent to you and says ‘…what’s wrong with this SQL statement? it takes ages? Why is Oracle so slow?…’ I am particularly fond of the last comment.
How can we quickly look at what is wrong with the SQL statement using the SQL Tuning Advisor? Here is how.

I – SQL Tuning Advisor
Using the DBMS_SQLTUNE package to tune SQL
Assuming the suspect query is something like “SELECT * FROM SESSIONS, SOURCE_EVENTS”, two very large tables, and your schema has the ADVISOR privilege granted, you would put this query into the Oracle SQL Tuning Advisor engine using the PL/SQL package DBMS_SQLTUNE like this:

1. Create the tuning task
In SQL*Plus and in the schema where objects live, use the procedure dbms_sqltune.create_tuning_task to create a tuning task.

Then you can check to make sure that your task is created and get the TASK_NAME value as you will need this in the next step. I could name the task, just being lazy…

2. Execute the tuning tasks
Execute the tuning task you created in step 1 with the procedure dbms_sqltune.execute_tuning_task

3. Get the tuning report
Get the tuning report/recommendation with the following SQL.

 Well done SQL Tuning Advisor, a good answer. It immediately spotted the Cartesian product and it recommends with a rationale that it would be expensive!

II – SQL Access Advisor
SQL Access advisor is another Advisory Framework tool which provides primarily advice on the creation of indexes, materialized views, materialized view logs to improve query performance and response time.

Both SQL Tuning Advisor and SQL Access Advisor tools are quite powerful as they can source the SQL they will tune automatcially from multiple different sources, including SQL cache, AWR, SQL tuning Sets and user defined workloads. Look at the Oracle documentation to find out how all this is possible under OEM Database Control.

What I focus on here is how to quickly pass through these tools ad-hoc SQL that the developer will bring to you and will demand to be tuned there and then.

So using the QUICK_TUNE procedure from the DBMS_ADVISOR package here is how you would tune a nasty single SQL statement for better data access paths with SQL Access Advisor

1. Create tuning task
In the schema where the table is run the following

2. Check to see the task is there
Again to see the task myquicktunetask which you just created run in the schema

3. See tuning advice
Now, to see the advice you get you will have to look in the DBA dictionary view
DBA_ADVISOR_ACTIONS. So login as DBA and filter for the task_name=’myquicktunetask’ in the DBA_ADVISOR_ACTIONS dictionary view. Well, you can view the advice in the USER_ADVISOR_ACTIONS dictionary view as well.

 TASK_NAME||’|’||COMMAND||’|’||ATTR2||’|’||ATTR3||’|’||ATTR4||’|’||ATTR5
(CLOB) myquicktunetask|CREATE MATERIALIZED VIEW LOG||ROWID, SEQUENCE| INCLUDING NEW VALUES|(“SHORT_CODE”)
(CLOB) myquicktunetask|CREATE MATERIALIZED VIEW||REFRESH FAST WITH ROWID|ENABLE QUERY REWRITE|SELECT ACHILLES.SESSIONS.SHORT_CODE C1, COUNT(*) M1 FROM ACHILLES.SESSIONS WHERE (ACHILLES.SESSIONS.SHORT_CODE = ‘tl’) GROUP BY ACHILLES.SESSIONS.SHORT_CODE
(CLOB) myquicktunetask|GATHER TABLE STATISTICS||-1||

Conclusion
Well I find both tools extremely useful. Both are good starting points in resolving SQL tuning issues and discussions. SQL Tuning Advisor seems to be doing logical optimization mainly by checking your SQL structure and statistics and the SQL Access Advisor does suggest good data access paths, that is mainly work which can be done better on disk.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


%d bloggers like this: