Solved

oracle: the execution of this query takes too long

Posted on 2011-03-02
12
357 Views
Last Modified: 2012-06-21
select      ts1.CodUnico,
            ts1.RazonSocial,
            ts1.NumeroDocumento,
            case ts1.codtipobanca
            when       1      then 'Corporativa BC'  
            when       2      then 'Empresa BE'  
            when       3      then 'Institucional BI'  
            when       4      then 'Leasing LS'  
            end case,
            ts1.CodGrupo,
            ts1.CodCalificacionSBS,
            ts1.CodFeve,
            ts1.Rating,
            case ts1.CodSegmentacion
            when '1' then '1'
            else '0'
            end case,
            ts1.CodEjecutivoNegocios,
            ts1.DesEjecutivoNegocios,
            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);  

Hi, the execution of this query takes too long, how I can improve?
0
Comment
Question by:enrique_aeo
  • 6
  • 4
  • 2
12 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 125 total points
ID: 35019492
Please post an execution plan.

If you are unfamiliar with how to do this:  using sqlplus

explain plan for
select ... <the rest of your select>;

then:
select * from table(dbms_xplan.display);

0
 
LVL 73

Expert Comment

by:sdstuber
ID: 35019516
you're querying the same table 3 times and joining by the same column

 on ts1.CODSOLICITUD = ts2.CODSOLICITUD

does this query make sense?
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 35019535
I'm just guessing the triple io can be removed...

try this...

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

Author Comment

by:enrique_aeo
ID: 35019623
HI sdstuber, I EXECUTED YOUR QUERY, but i have this error
errORACLE.jpg
0
 

Author Comment

by:enrique_aeo
ID: 35019659
THIS IS THE execution plan
executionPLAN.jpg
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 35019689
I think sdstuber's query in http:#a35019535 will fix the issue.

You just need to decide which columns form which table to use in "PARTITION BY codunico ORDER BY codsolicitud DESC" and add the alias.
0
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.

 
LVL 73

Expert Comment

by:sdstuber
ID: 35019729
Did you run the query exactly as I posted it?


I'm not sure what could be ambiguous,  there is only one table left in the query.

You could try removing the ts1 alias and prefix since it's not needed, but there should be no need.
There's only one table
0
 

Author Comment

by:enrique_aeo
ID: 35020000
soory experts, this is working
SELECT CodUnico,
            RazonSocial,
            NumeroDocumento,
            --CodTipoBanca,
            CodGrupo,
            CodCalificacionSBS,
            CodFeve,
            Rating,
            --CodSegmentacion,
            CodEjecutivoNegocios,
            DesEjecutivoNegocios,
            LineaCredTienda
  FROM (SELECT ts1.codunico,
               ts1.razonsocial,
               ts1.numerodocumento,
               CASE ts1.codtipobanca
                   WHEN 1 THEN 'Corporativa BC'
                   WHEN 2 THEN 'Empresa BE'
                   WHEN 3 THEN 'Institucional BI'
                   WHEN 4 THEN 'Leasing LS'
               END  CASE --CodTipoBanca
               ,
               ts1.codgrupo,
               ts1.codcalificacionsbs,
               ts1.codfeve,
               ts1.rating,
               CASE ts1.codsegmentacion WHEN '1' THEN '1' ELSE '0' END CASE,
               ts1.codejecutivonegocios,
               ts1.desejecutivonegocios,
               ts1.lineacredtienda,
               RANK() OVER (PARTITION BY codunico ORDER BY codsolicitud DESC) r
          FROM tmp_solicitud ts1)
 WHERE r = 1

BUT NEED CodTipoBanca COLUMN WHEN THE ERROR ENABLE ME OUT, view file. Case is the column
0
 

Author Comment

by:enrique_aeo
ID: 35020025
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 375 total points
ID: 35020026
>>> BUT NEED CodTipoBanca COLUMN WHEN THE ERROR ENABLE ME OUT, view file. Case is the column


I'm sorry, I don't know what that is trying to say, can you rephrase?

I see you have the codtipobanca column commented out, simply remove the "--"  and you'll get it
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 375 total points
ID: 35020043
is this what you're looking for?
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

Open in new window

0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 375 total points
ID: 35020083
I see where the ambiguous column was coming from earlier too


END CASE --- this is pl/sql syntax,  not sql.

So you were actually aliasing both CASE statments to have the name "CASE",  which was ambiguous in the select *

by using sql syntax as in my last post, each case is given a distinct name
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.

Join & Write a Comment

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

747 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

13 Experts available now in Live!

Get 1:1 Help Now