Solved

oracle: the execution of this query takes too long

Posted on 2011-03-02
12
363 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
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!

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
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 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 explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.

749 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