Oracle SQL Optimizer - 9i vs 10g

Posted by Praveen राठी in , ,

We are currently in the process of migrating some of our applications from Oracle 9i to Oracle 10g. One of the problems that we faced while migrating the application code is the order in which the result is returned for a particular set of queries.
We have some dropdown boxes (e.g Country Name) in our application which are populated dynamically by a query like this:

SELECT DISTINCT country_name FROM countries WHERE current_rec_ind = 'Y';

The DISTINCT and "GROUP BY" are two clauses that inherently force the sorting of data set in case of Oracle 9i. So, when you have a DISTINCT or "GROUP BY" in your SELECT query, you are most likely to get the data set sorted by the columns in the SELECT list. This happens because the SQL Optimizer (Cost based) in Oracle 9i is implemented in such a way that it gets the lowest cost for a query when it first sorts the data and then removes the duplicates.
So, for the dropdown, we always used to get the country names sorted alphabetically.

But this is not the case with Oracle 10g. The SQL optimizer in 10g does not sort the data in order to remove duplicates. So, we were not getting the country names in proper order. Here are the explain plans of the same query in the two Oracle versions to make it clearer:



The second step in the plan makes it all clear.

This is one of the potential risks that should be considered for your application code while migrating from 9i to 10g. So, its always better to have an "ORDER BY" in your queries if you need sorted data to avoid backward compatibility problems.

This entry was posted on Wednesday, July 09, 2008 at Wednesday, July 09, 2008 and is filed under , , . You can follow any responses to this entry through the comments feed .

8 Comments

This comment has been removed by the author.
July 9, 2008 at 5:06 PM

does this mean the sample query is more costly in 10g then 9i, if so isnt it usually that next version of a product is better than previous one, or are there inherent benefits to not perform a default sort?

In the past week, i had seen a scenario when a select query just hung in the data, the DBA said that it may be due to bad execution plan, then the query was restarted and it ran fine.
Isn't execution plan dynamically generated by Oracle at runtime? What could have been the reasons for bad execution plan?

July 9, 2008 at 5:09 PM

Answers one by one:
1) Being costlier doesn't mean that it is more inefficient too.
The cost of a query is a function of multiple things like CPU usage, memory usage etc. Surprisingly, inspite of having more cost, the query runs faster on 10g than 9i.

2) As far as SORT is concerned, it is one of the costliest operations. It is a CPU and memory intensive opertion. Needs a lot of buffers to store the intermediate data and lot of CPU cycles to carry out the SORT operation, whatever be the sort algorithm.

3) The cost based SQL optimizer generates many execution plans of the query when it is executed the first time and the plan with the least cost is stored in the library cache. The subsequent runs of the query pick the execution plan from the cache (known as cache hit). That's the reason the query runs faster the second time.

July 10, 2008 at 9:03 AM

उपयोगी जानकारी है

July 10, 2008 at 11:38 AM

Dear Praveen,

Last week we did upgrade Oracle apps database 9i(9.2.0.5) RAC to 10g RAC(10.2.0.4). Migration was smooth. But after go-live we are facing severe performance problem with Oracle E-business suite 11i as well as all custom application. Execution plan of all custom query is changed. Indexes are not getting used properly and causing high CPU utilisation. This results into very poor perfornace of all the applications.
We already tried Oracle sql tuning advisor to fine tune the queries but no use . Could you please advice how best we need to tune the Oracle 9i Queries in 10g environment to get the good performance or atleast like 9i ?
Thanks in advance.

Regards,
Digambar Hawaldar

October 7, 2008 at 5:25 PM
Anonymous  

Goal:- To get the data with unique values ..and not to get that in sorted order.


So earlier in 9i we were getting that as additional benefit :)

What the optimizer is doing in 10g is just giving us the non-distinct values by using the more optimized Hash Unique inbuilt operation..


In 9i..it was first sorting and then removing duplicates, which is in fact more heavier operation. So oracle just optimized make things more faster ..

You can see the benefits at large data volume to appreciate the change..

I hope it helps.
Just as additional input... tell the oracle what you want to do.

November 21, 2008 at 5:02 PM
Anonymous  

Goal:- To get the data with unique values ..and not to get that in sorted order.


So earlier in 9i we were getting that as additional benefit :)

What the optimizer is doing in 10g is just giving us the non-distinct values by using the more optimized Hash Unique inbuilt operation..


In 9i..it was first sorting and then removing duplicates, which is in fact more heavier operation. So oracle just optimized make things more faster ..

You can see the benefits at large data volume to appreciate the change..

I hope it helps.
Just as additional input... tell the oracle what you want to do.

November 21, 2008 at 5:15 PM
Anonymous  

Amiable brief and this enter helped me alot in my college assignement. Thanks you as your information.

August 12, 2010 at 2:45 PM

Post a Comment