Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle LEADING hint -- why is this required?

Suddenly (but unfortunately I don't know when "suddenly" was; I know it ran fine at some point in the past) one of my queries started taking 7+ seconds instead of milliseconds to execute. I have 1 local table and 3 tables being accessed via a DB link. The 3 remote tables are joined together, and one of them is joined with my local table.

The local table's where clause only takes a few millis to execute on its own, and only returns a few (10's or 100's at the most) records. The 3 remote tables have many hundreds of thousands, possibly millions, of records between them, and if I join them appropriately I get tens or hundreds of thousands of records.

I am only joining with the remote tables so that I can pull out a few pieces of data related to each record in my local table.

What appears to be happening, however, is that Oracle joins the remote tables together first and then my local table to that mess at the end. This is always going to be a bad idea, especially given the data set that exists right now, so I added a /*+ LEADING(local_tab remote_tab_1) */ hint to my query and it now returns in milliseconds.

I compared the explain plans and they are almost identical, save for a single BUFFER SORT on one of the remote tables.

I'm wondering what might cause Oracle to approach this the wrong way? Is it an index issue? What should I be looking for?

like image 480
aw crud Avatar asked Feb 23 '10 21:02

aw crud


People also ask

What is the use of leading hint in Oracle?

"The LEADING hint specifies the set of tables to be used as the prefix in the execution plan. "

Why do we use parallel hint in Oracle?

Enhance customers' Oracle database performance by using parallel SQL with an Oracle parallel hint, enabling a SQL statement to be simultaneously processed by multiple threads or processes.

What are hints used for in SQL queries?

Hints are options or strategies specified for enforcement by the SQL Server query processor on SELECT, INSERT, UPDATE, or DELETE statements. The hints override any execution plan the query optimizer might select for a query.

Can we avoid index by using hint in Oracle?

Purpose: NO_INDEX hint explicitly notifies the optimizer to not to use the specified index(s). This can be used for query testing purpose without dropping the actual index. In some cases queries will give better performance without indexes.


1 Answers

When choosing an execution plan, oracle estimates costs for the different plans. One crucial information for that estimate is the amount of rows will get returned from a step of the execution plan. Oracle tries to estimate those using 'statistics', i.e. information about how many rows a table contains, how many different values a column contains; How evenly these values are distributed.

These statistics are just that statistics, and they might be wrong, which is one of the most important reasons for misjudgments of the oracle optimizer.

So gathering new statistics as described in a comment might help. Have a look at the documentation on that dbms_stats package. There are many different ways to call that package.

like image 147
Jens Schauder Avatar answered Sep 25 '22 13:09

Jens Schauder