Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Tse Chong.

Asked: February 23, 2009 - 5:16 am UTC

Last updated: February 23, 2009 - 5:35 pm UTC

Version: 9.2.0

Viewed 1000+ times

You Asked

Hi Tom,

In some of your posts, you mentioned "what if we drive with table1", or "what if we lead with table2".

How do you make the optimiser 'drive with table1' or 'lead with table2'?

I pretty much understand how to interpret the Explain_Plan output. But I am still struggling with how to make SQL perform better.

1) Do I re-order the tables in the FROM Clause? Does this help?

2) Do I re-order the 'Where' statements? Does this help?

I am holding on to your 'Effective Oracle by Design'. I have read some chapters. If there are any pages which explains my questions, please point me to them.

Thank you!

Rgds,
TC

and Tom said...

1 & 2 - no, the CBO doesn't really care about such things, part of its JOB is to reorder these things as it sees best.

To "force" things like that - you would use hints, but I would strongly discourage you from going down that path.


Since you have Effective Oracle by Design, read chapter 8, it starts with:

Effective SQL

This was probably the hardest chapter of the book to write. That is not because the material is all that complex. It¿s because I know what people want, and I know what can be delivered. What people want is the ten-step process for tuning any query. What can be delivered is knowledge about how queries are processed, which you can use and apply as you develop queries.

If there were a ten-step, or even a million-step, process by which any query (or a large percentage of queries) could be tuned, we would write a program to do it. Sure, there are actually many programs that try to do this, such as Oracle Enterprise Manager with its tuning pack, SQL Navigator, and others. What they do is primarily recommend indexing schemes to tune a query, suggest materialized views, and offer to add hints to the query to try other access plans. They show you different query plans for the same statement and allow you to pick one. These tuning tools use a very limited set of rules that sometimes can suggest that index or set of indexes you really should have thought of during your design. They offer ¿rule of thumb¿ (what I generally call ROT, since the acronym and the word it maps to are so appropriate for each other) SQL optimizations. If these were universally applicable, the optimizer would do them routinely.

In fact, the CBO does tuning already. It rewrites our queries all of the time. If there were an N-step process to tuning a query¿to writing efficient SQL¿ the optimizer would incorporate it all, and we would not be having a discussion on this topic. It is like the search for the Holy Grail. Maybe someday the software will be sophisticated enough to take our SQL, understand the question being asked, and process the question rather than syntax.

This chapter will provide the foundation knowledge you need in order to begin thinking about how queries could be processed. Once you understand query processing, you can make the query do what you think might be best. Writing efficient SQL is no different from writing efficient C, Java, or even English. It takes an understanding of how things work and what is available. If you don¿t know some technique is available, you¿ll never be able to optimize to use it.


Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Was it always thus?

David Weigel, February 23, 2009 - 12:14 pm UTC

Once upon a time, in 1988 or so, our company had to pick a database to develop our software package around, and it came down to Oracle and Ingres. One big deciding factor was that Ingres seemed to do better in query optimization (Ingres's cost-based optimizer was neat). As I remember Oracle's optimizer did act differently depending on the order of the tables in the FROM and the order of the joins in the WHERE clause. We've long ago switched to Oracle, and I know that the modern cost-based optimizer doesn't care about the order, but is my memory faulty about the order of clauses helping Oracle's (rule-based?) optimizer back in the day?
Tom Kyte
February 23, 2009 - 5:35 pm UTC

in 1988 - we only had the rule based optimizer, the CBO was introduced in version 7.0 in 1992.

In the RBO - the order of tables in the from clause and the ordering of the predicate could affect the plan - definitely.

the RBO was right to left on the from clause, bottom up on the where clause.

More to Explore

Performance

Get all the information about database performance in the Database Performance guide.