Solved

oracle: better the perfomrnace. inner join vs partition by

Posted on 2011-03-02
5
606 Views
Last Modified: 2013-12-07
I have 2 querys, the former takes more than 15 minutes.
query 1
select      
          ts1.CodUnico,
            ts1.RazonSocial,
            ...
            ts1.LineaCredTienda
from        tmp_Solicitud ts1 inner join tmp_Solicitud ts2
            on ts1.CODSOLICITUD = ts2.CODSOLICITUD
            and ts2.codsolicitud = (select max(t.codsolicitud)
                                    from tmp_Solicitud t  
                                    where ts2.CodUnico = t.CodUnico);  
                                   

query 2
SELECT
       codunico,
       razonsocial,
       numerodocumento,
       codtipobanca,
       codgrupo,
       codcalificacionsbs,
       codfeve,
       rating,
       codsegmentacion,
       codejecutivonegocios,
       desejecutivonegocios,
       lineacredtienda
  FROM (SELECT codunico,
               razonsocial,
               numerodocumento,
               CASE codtipobanca
                   WHEN 1 THEN 'Corporativa BC'
                   WHEN 2 THEN 'Empresa BE'
                   WHEN 3 THEN 'Institucional BI'
                   WHEN 4 THEN 'Leasing LS'
               END
                   codtipobanca,
               codgrupo,
               codcalificacionsbs,
               codfeve,
               rating,
               CASE codsegmentacion WHEN '1' THEN '1' ELSE '0' END codsegmentacion,
               codejecutivonegocios,
               desejecutivonegocios,
               lineacredtienda,
               RANK() OVER (PARTITION BY codunico ORDER BY codsolicitud DESC) r
          FROM tmp_solicitud)
 WHERE r = 1

Thanks to you, the query was modified and now takes less than 2 seconds, my question is why?
 Deputy respective execution plans.
 Greetings

partitionBY.jpg
INNERjoin.jpg
0
Comment
Question by:enrique_aeo
  • 3
  • 2
5 Comments
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 500 total points
ID: 35021499
The first queries the same table 3 times including extra work for aggregation and filtering on the aggregate

The second queries the table only once and then sorts the data.

The first does a lot more work, hence more time
0
 
LVL 73

Accepted Solution

by:
sdstuber earned 500 total points
ID: 35021540
The

select .... from (select....)

might make it appear there are two queries needing double io, but there isn't.  The inner query is called an inline view and is merely a logical construction for syntax.  Looking at the plan you can see there is only one IO  (TABLE ACCESS FULL) which makes sense since the table is only referenced once in the query (the FROM clause)

the other query clearly  references the table 3 times, so regardless of any syntax constructions the query will hit the table 3 times once for each reference and the plan shows that
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35021570
Since your question has to do with performance:  What version of Oracle are you using?

I noticed you are using Rule-Based Optimization.  If possible and you aren't using it 'by choice', I would look into using Cost-Based Optimization.

Check the online documentation for your version.

Here's the 11Gr2 link (It's what I have bookmarked):
http://download.oracle.com/docs/cd/E11882_01/server.112/e17120/ds_appdev004.htm#ADMIN12198
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 500 total points
ID: 35021606
I agree switching to CBO from RBO is a good idea,
 but it's not actually related to the specific reason why query 2 is better than query 1.

Since you're able to use the RANK analytic that means you're using at least 8.1  so CBO is definitely an option to investigate.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35021656
>>but it's not actually related to the specific reason why query 2 is better than query 1.

As they are currently written, I agree and should have commented on that.

enrique_aeo,
please do not award any points to my post on RBO versus CBO.

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Migrating an SQL 2008 database to Oracle 12c 3 90
Dataware house query tuning 9 65
Fastest way to replace data in Oracle 5 51
ORA-01008: not all variables bound. 6 38
Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now