Solved

oracle: the execution of this query takes too long

Posted on 2011-03-02
12
365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 77

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 74

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 74

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
Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

 

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 77

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
 
LVL 74

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 74

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 74

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 74

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Suggested Courses

627 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