oracle: the execution of this query takes too long

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?
enrique_aeoAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
slightwv (䄆 Netminder)Connect With a Mentor Commented:
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
 
sdstuberCommented:
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
 
sdstuberCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
enrique_aeoAuthor Commented:
HI sdstuber, I EXECUTED YOUR QUERY, but i have this error
errORACLE.jpg
0
 
enrique_aeoAuthor Commented:
THIS IS THE execution plan
executionPLAN.jpg
0
 
slightwv (䄆 Netminder) Commented:
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
 
sdstuberCommented:
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
 
enrique_aeoAuthor Commented:
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
 
enrique_aeoAuthor Commented:
0
 
sdstuberConnect With a Mentor Commented:
>>> 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
 
sdstuberConnect With a Mentor Commented:
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
 
sdstuberConnect With a Mentor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.