Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Oracle hint "FIRST_ROWS" to improve Oracle database performances

I have a statement that runs on Oracle database server. The statement has about 5 joins and there is nothing unusual there. It looks pretty much like below:

SELECT field1, field2, field3, ...  
FROM table1, table2, table3, table4, table5  
WHERE table1.id = table2.id AND table2.id = table3.id AND ...  
      table5.userid = 1

The problem (and what is interesting) is that statement for userid = 1 takes 1 second to return 590 records. Statement for userid = 2 takes around 30 seconds to return 70 records.

I don't understand why is difference so big.

It seems that different execution plan is chosen for statement with userid = 1 and different for userid = 2.

After I implemented Oracle Hint FIRST_ROW, performance become significantly better. Both statements (for both ids 1 and 2) produce return in under 1 second.

SELECT /*+ FIRST_ROWS */
       field1, field2, field3, ...  
FROM table1, table2, table3, table4, table5  
WHERE table1.id = table2.id AND table2.id = table3.id AND ...  
      table5.userid = 1

Questions:

  1. What are possible reasons for bad performance when userid = 2 (when hint is not used)?
  2. Why would execution plan be different for one vs another statement (when hint is not used)?
  3. Is there anything that I should be careful about when deciding to add this hint to my queries?
like image 273
bobetko Avatar asked Jun 11 '12 13:06

bobetko


People also ask

When to use the first_rows hint in Oracle?

As the FIRST_ROWS (n) hint is cost-based, Oracle recommends that you collect statistics on your tables before you use this hint. You use the FIRST_ROWS (n) hint in cases where you want the first number ( n) of rows in the shortest possible time.

How do I use first_rows in Oracle?

About. FIRST_ROWS is an optimizer goal in order to ask it the best response time. You can instruct this instruction through: a hint. or by setting the value of the optimizer goal. In the two case, it instructs Oracle to optimize an individual SQL statement with a goal of best response time to return the first rows.

What is the purpose of hinting in Oracle Database?

Without the hint, Oracle Database sorts the rowids after the Text index has returned all the rows in unsorted order that satisfy the CONTAINS predicate. Retrieving the entire result set this way takes time. Because only the first 10 hits are needed in this query, using the hint results in better performance.

Does using first_rows(1) hint really improve performance?

Im running a select query by joining 2 tables. While trying to tune the query i found using first_rows (1) hint makes less reads from the joining tables than without it and hence improves performance. Below are the explain plan. Question is so using the first_rows hint really improves the performance and both returns same number of result sets..?


1 Answers

1) What are possible reasons for bad performance when userid = 2 (when hint is not used)?

Because Oracle thinks that one of the interim result sets using the plan from (userid=1) will be very large - probably incorrectly.

2) Why would execution plan be different for one vs another statement (when hint is not used)?

Histogram based indexes

3) Is there anything that I should be careful about when deciding to add this hint to my queries?

As long as the number of records being returned is small, this hint should be failry safe - unlike pushing the optimizer to use a specific index, this approach allows Oracle to pick a different plan if the indexes are changed.

like image 133
symcbean Avatar answered Nov 16 '22 02:11

symcbean