Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: Cost of decode in the WHERE part

Re: Cost of decode in the WHERE part

From: Charles Hooper <hooperc2000_at_yahoo.com>
Date: 1 Feb 2007 12:04:58 -0800
Message-ID: <1170360298.089818.77720@k78g2000cwa.googlegroups.com>


On Feb 1, 9:08 am, "ian" <ipel..._at_yahoo.com> wrote:
> Hi All;
>
> Is there any experience on the cost of having DECODE in the where part
> of a select statement on this list Please?
>
> EG
> and DECODE(upper(chg_party),'A',a_sub_num,b_sub_num) = t.telephony_id
>
> I would have done somethong like :-
> and ( ( a_sub_num = t.telephony_id and ( chg_party = 'A' or
> chg_party = 'a' ) )
> or ( b_sub_num = t.telephony_id and ( chg_party != 'A' and
> chg_party != 'a' ) )
> )
>
> The select is on a wide long table that will have to be scanned from
> other parts of the SQL.
>
> Regards
> Ian

There is no straight answer to your question. If there is an index on the CHG_PARTY column, the DECODE statement will prevent Oracle from using the index. If the CHG_PARTY column only contains two values (A and B, or a and b), the index on the column probably will not be very helpful in terms of execution performance, so not being able to use the index may not be a big loss. The DECODE statement will force Oracle to use a particular execution plan. Oracle may chose to rewrite your SQL statement to look something like this: SELECT
  *
FROM
  ...
WHERE
  A_SUB_NUM = T.TELEPHONY_ID
  AND CHG_PARTY = 'A'
UNION
SELECT
  *
FROM
  ...
WHERE
  A_SUB_NUM = T.TELEPHONY_ID
  AND CHG_PARTY = 'A'
UNION
SELECT
  *
FROM
  ...
WHERE
  B_SUB_NUM = T.TELEPHONY_ID
  AND CHG_PARTY <> 'A'
UNION
SELECT
  *
FROM
  ...
WHERE
  B_SUB_NUM = T.TELEPHONY_ID
  AND CHG_PARTY <> 'a';

Now the question. Will your version require four full table scans of the table, while the DECODE version only required one?

Charles Hooper
PC Support Specialist
K&M Machine-Fabricating, Inc. Received on Thu Feb 01 2007 - 14:04:58 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US