Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

oracle: the execution of this query takes too long

Posted on 2011-03-02
12
Medium Priority
?
367 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 500 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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 1500 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 1500 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 1500 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…

705 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