Home » SQL & PL/SQL » SQL & PL/SQL » rewrite sql query
rewrite sql query [message #609596] Mon, 10 March 2014 03:47 Go to next message
mohan1760
Messages: 59
Registered: June 2008
Member
Hi,

How can i re-write the below query in order to reduce its cost.

SELECT *
FROM A
WHERE cod_acct_no = var_l_cod_acct_no
AND cod_dep_no in (var_l_cod_dep_no,0)
AND ctr_inst_no = (SELECT max(ctr_inst_no)
FROM A
WHERE cod_acct_no = var_l_cod_acct_no
AND cod_dep_no in (var_l_cod_dep_no, 0)
AND cod_payout_type = P
AND flg_mnt_status = A
)
AND cod_payout_type = P
AND flg_mnt_status = A;
Re: rewrite sql query [message #609598 is a reply to message #609596] Mon, 10 March 2014 03:50 Go to previous messageGo to next message
ThomasG
Messages: 3211
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
Since you didn't give enough information for a more qualified answer:

Add a "and 1=2" at the end. That will definitely bring the cost down.

Re: rewrite sql query [message #609600 is a reply to message #609596] Mon, 10 March 2014 03:55 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Please read our OraFAQ Forum Guide and please read How to use [code] tags and make your code easier to read You have been asked to do this before.
You need to provide lot more information, at the very list how many rows are in the tables; how many rows the query returns; and the execution plan, formatted with dbms_xplan.

But in any case, I do not see how the query can run because you have not enclosed the literals in quotes.


Re: rewrite sql query [message #609601 is a reply to message #609596] Mon, 10 March 2014 03:56 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

In addition, the query will surely not execute as it so the execution time is near 0.

I think that being here since almost 6 years you should be able to post what is needed in the way it is usually requested (format, session...) and I don't see any reason why you don't do it. Can you explain?

Re: rewrite sql query [message #609892 is a reply to message #609596] Thu, 13 March 2014 13:00 Go to previous message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Your query is quite inefficient. You should use analytic DENSE_RANK, not subquery to get MAX:

WITH T AS (
           SELECT  A.*,
                   dense_rank() over(order by ctr_inst_no desc) rnk
             FROM  A
             WHERE cod_acct_no = var_l_cod_acct_no
               AND cod_dep_no in (var_l_cod_dep_no, 0)
               AND cod_payout_type = P
               AND flg_mnt_status = A
          )
SELECT  *
  FROM  T
  WHERE rnk = 1
/


SY.
Previous Topic: Regular expression
Next Topic: Calc elapsed time within a group of data
Goto Forum:
  


Current Time: Fri Apr 26 07:57:56 CDT 2024