SQL Cost of using DECODE built-in

I'm trying to tune some SQL and wondering about the cost of using DECODE. I believe that by using this function, it prevents any index on the related column from being used.
Do any "Tuning Gurus" have opinions on the use of DECODE?
TIA,

As a general rule, the Oracle built-in functions are pretty fast to execute. You will not really see any difference in performance between a sql statement using a built-in function in the SELECT clause, and one not using one.
However, a lot depends on where and how you use the DECODE.
SELECT col1,DECODE(col2,1,'YES',2,'NO',3,'MAYBE')
FROM table
will be no slower than
SELECT col1,col2
FROM table
If there is an index on col2, then
SELECT col1,DECODE(col2,1,'YES',2,'NO',3,'MAYBE')
FROM table
WHERE col2 BETWEEN 1 AND 3
will use it.  However
SELECT col1,col2
FROM table
WHERE DECODE(col2,1,'YES',2,'NO',3,'MAYBE') IN ('YES','NO','MAYBE')
will not (unless you have a function based index).  But,
SELECT col1,col3
FROM table
WHERE col2 BETWEEN 1 and 3 and
      col3 = DECODE(col2,1,'YES',2,'NO',3,'MAYBE') IN ('YES','NO','MAYBE')
will.Note that "will" in the above really means can. it is up to the optimizer whether it actually is used or not.
HTH
John

Similar Messages

  • Using DECODE Function in SQL

    I need to know if there is any way to use a range of values from
    database and decode to certain text. I am able to do with one
    value.
    for example:
    DECODE(column_name,'216767111','Unlimited',column_name)
    above argument works with one value only. How about a range,
    ex: 216767000 to 216767111. I need to use only SQL. No PL/SQL.
    Kinldly need some body's help
    Thanks
    Munis

    Which version of the database? If it's 8i+ then you can use
    the CASE function
    for example:
    (CASE WHEN column_name between 216767000 and 216767111
    THEN 'Unlimited' ELSE column_name END)
    This won't work in PL/SQL because they're introducing a CASE
    statement does soemthing different.
    rgds, APCHello Andrew
    Thank you for response. I am using 8i. 8.1.6. However using
    CASE, I get inconsistent data type, ORA-00932: inconsistent
    datatypes. I able to work it out with other response using
    DECODE(sign(. Do you have any idea why i am getting this error.
    If time permits, let me know

  • Pl/sql using DECODE

    Hello,
    i have question regarding PL/SQL using DECODE when i use following qurey in SQL plus it is working fine but when i try to run in tode it only display value of g.
    everything else is blank........
    so can u plz assist me on that .....
    thanks in advance!!!!!!!!!!
    SELECT
    b.g,b.s,
    COUNT(DECODE(a.u,'R',a.rep)) AS R,
    COUNT(DECODE(a.u,'u',a.rep)) AS U,
    COUNT(DECODE(a.u,'C',a.rep))AS C,
    COUNT(DECODE(a.u,'I',a.rep)) AS I,
    COUNT(DECODE(a.u,NULL,a.rep)) AS B
    FROM AREA a, data b
    WHERE a.rep = '454637'
    AND a.rep = b.emp
    GROUP BY b.g,b.s

    Very strange.
    Find out if there are alterations to your SQL*PLUS envirionment. Warren mentioned user/database wierdness; the ALTER SCHEMA and other commands can affect privilges and what objects you can see. Sometimes these commands are buried in LOGIN.SQL and GLOGIN.SQL files (for SQL*PLUS), and more hard to find they can call files buried on a network somewhere.
    Try the query without the DECODE() and see what's in the table without trying to modify the values in both envirionments.

  • Using Decode in where clause in free hand sql

    Hi,
        I want to use decode in free hand sql.
    for eg : this is a where condition
    Outlet_Lookup.City  =  @variable('Enter City')
    Requirement :
    if we put the user prompt as "Enter City and % for all cities"
    and the user enters % then the data display should be all cities or else it shld be the specific city entered in the prmpt..
    Can we do that using Decode statement,
    I have tried
    Outlet_Lookup.City  =  Decode(@variable('Enter City'),'%',Outlet_Lookup.City,(@variable('Enter City')))
    This is not working....
    Pls guide on the same..
    Thanks.

    Hi Mathieu,
            Thanks for the solution,
    I tried the following
    ((Outlet_Lookup.City  =  @variable('Enter City or % for All') or @variable('Enter City or % for All')='%'))
    This is working fine....
    Thanks
    Regards,
    Aparna.

  • Is it possible to save / extract data from a MS SQL database 2008 using lookout 6.2?

    Is it possible to save / extract data from a MS SQL database 2008 using lookout 6.2?
      Now a days we are saving / extracting data in a excel spreadsheet, but we would like to do that using a database because it is better.

    You can use ODBC connection to work with SQL Server database.
    Use SQLExec object to execute the SQL statement. Here is a KB about the SQL statement.
    http://digital.ni.com/public.nsf/allkb/4ADEEA04CD24AE0B862565E20002A16F?OpenDocument
    To write data to spreadsheet is more straightforward because Lookout has built-in spreadsheet object. But you need to write SQL statement by yourself to interact with other database.
    To read the data back is the same way. Just use "select" SQL statement to query. You can use Datatable object to query.
    The "Data Source" can be "DSN = data source name;". The data source name is configured in Control Panel->Administrative Tools->Data Sources(ODBC).
    Ryan Shi
    National Instruments

  • I need to pass data from an Access database to Teststand by using the built in Data step types(open data

    base /open SQL Statement etc) the first time i defined the system everything was fine but when i changed the Database (using M.S.Access) the " open SQL Statement" it would show the tables but not thier columns ,I"m using win98 sec edition / Teststand 1.0.1i need to pass data from an Access database to Teststand by using the built in Data step types(open database /open SQL Statement etc) the first time i defined the system everything was fine but when i changed the Database (using M.S.Access) the " open SQL Statement" it would show the tables but not thier columns ,I"m using win98 sec edition / Teststand 1.0.1
    When I tried the same thing on another cmputer the same thing
    happend
    appreiciate u"r help

    base /open SQL Statement etc) the first time i defined the system everything was fine but when i changed the Database (using M.S.Access) the " open SQL Statement" it would show the tables but not thier columns ,I"m using win98 sec edition / Teststand 1.0.1Hello Kitty -
    Certainly it is unusual that you can still see the tables available in your MS Access database but cannot see the columns? I am assuming you are configuring an Open Statement step and are trying to use the ring-control to select columns from your table?
    Can you tell me more about the changes you made to your file when you 'changed' it with MS Access? What version of Access are you using? What happens if you try and manually type in an 'Open Statement Dialog's SQL string such as...
    "SELECT UUT_RESULT.TEST_SOCKET_INDEX, UUT_RESULT.UUT_STATUS, UUT_RESULT.START_DATE_TIME FROM UUT_RESULT"
    Is it able to find the columns even if it can't display them? I am worried that maybe you are using a version of MS Access that is too new for the version of TestSt
    and you are running. Has anything else changed aside from the file you are editing?
    Regards,
    -Elaine R.
    National Instruments
    http://www.ni.com/ask

  • How to use decode function in oracle apex 3.2.1

    Hello Friends,
    how to use decode
    var_decode := 'decode'||'('|| var1 ||','|| -800000000000000||','||'.A'||','||
    appreciate your help.
    regards/kumar
    Edited by: kumar73 on Apr 23, 2010 12:25 PM
    Edited by: kumar73 on Apr 23, 2010 12:27 PM

    Hey just on this particular point, there's a handy PL/SQL method for escaping quotation characters which may reduce the complexity of the string.
    For example, if I had a string:
    I can't understand why quoting my 'quote' characters never seems to "work"!and I wanted to pass that into a variable, I could do this:
    vc_foo:= 'I can''t understand why my ''quote'' characters never seems to "work"!';or I could use the q function, whereby you enclose your string within a quote delimiter of your choice, like so:
    vc_foo:= q'^I can't understand why quoting my 'quote' characters never seems to "work"!^';
    note: I used the caret (^) character by convention but any valid character will do
    ...all of which leaves your original string more or less unadulterated.
    I find this particularly useful for generating dynamic SQL and dynamic PL/SQL, as it leave the query looking a tad more readable, although if you are chaining together multiple strings with double-pipes, it can make it a little muddier. I find it particularly useful if I'm going to deal with string data from, for example, a field with Irish-derived surnames such as "O'Reilly"...
    Give it a go!

  • How to use DECODE function in Exspression?

    Hi,
    Can we use DECODE in Expression?
    I'm trying to use DECODE function but there is an error during the validation. But when i validate the mapping, it is successfully compiled but it is failed during deployment.
    But if I use CASE instead of DECODE, it works fine.
    Can we use DECODE in OWB???
    Thanks
    Raj

    Hi,
    In OWB 10gR2, if your are using only one DECODE in an expression, it's working. The package will compile when deploying the mapping. OWB will replace the DECODE by a CASE.
    But when you are using nested decode in an expression ( for example : decode(col1, 1, 'M', decode(col2, 'Madame', 'Mme', null)) ) only the first one is replaced by a case at deployment.
    In ROW_BASED mode, text of the expression is used outside of an sql statement and deployment will fails with "PLS-00204: function or pseudo-column 'DECODE' may be used inside a SQL statement only."
    If operating mode for the mapping is set to SET_BASED, it's working because the expression is used in an sql statement.
    I have logged a SR in metalink for this issue and a bug is opened (bug 5414112).
    But I agree with you, it's better to use case statement.
    Bernard

  • How to use decode to calculate sum for different date range

    I'm stuck with decode() function:
    I have a table like this:
    (project_id, approve_date, value, builder_code)
    I want to write a SQL query to get sum of values for different month of the approve_date, and group by builder_code)
    The result is like this:
    builder_code Sum(value)_Sep-03 Sum(value)_Oct-03 Sum(value)_Nov03
    1001 1,299 1,322 990
    1002 3,332 1,222 333
    I tried to use decode for this question but could not get the answer.
    Thanks a lot

    I don't think you need a DECODE() here. I'd do something like this-
    create table builder (
        project_id number,
        approve_date date,
        value        number,
        builder_code number
    insert into builder values( 1, to_date('09-01-2003', 'MM-DD-YYYY'), 100, 990 )
    insert into builder values( 2, to_date('09-03-2003', 'MM-DD-YYYY'), 150, 990 )
    insert into builder values( 3, to_date('09-05-2003', 'MM-DD-YYYY'), 250, 990 )
    insert into builder values( 3, to_date('09-05-2003', 'MM-DD-YYYY'), 250, 333)
    SELECT sept.builder_code, sept.sept_sum, oct.oct_sum
    FROM (SELECT builder_code, sum(value) sept_sum
            FROM builder
           WHERE approve_date >= to_date('09-01-2003','MM-DD-YYYY')
             AND approve_date < to_date('10-01-2003','MM-DD-YYYY')
           GROUP BY builder_code) sept,
         (SELECT builder_code, sum(value) oct_sum
            FROM builder
           WHERE approve_date >= to_date('10-01-2003','MM-DD-YYYY')
             AND approve_date < to_date('11-01-2003','MM-DD-YYYY')
           GROUP BY builder_code) oct
    WHERE oct.builder_code(+) = sept.builder_code
    BUILDER_CODE   SEPT_SUM    OCT_SUM
             333        250
             990        500Justin
    Distributed Database Consulting, Inc.
    www.ddbcinc.com/askDDBC

  • Sql server is using memory more then there max memory setting?

    Hi All,
    today i have faced the different issue..........
    we have one SQl server 2008 prod server... it have memory issue actually  total server memory is 8GB and we have allocated the 6 GB to SQL server,we checked the server no open trans and dont have running jobs also,one database is using the memory high(no
    open trans)... but Sql server has used 6.5 GB (6 GB) and total server used memory is reached to 7.89 GB.
    Please tell me how to find the most costly query in particular database wise and sql server internally behavior ?
    how to troubleshot the  above issue?
    please post the your valuable answers ASAP...
    Thanks,
    RAM

    Please refer to what Olaf said briefly.Buffer pool in SQL server only caters to memory requests which requires pages <8 KB . Almost all requests are satisfies by buffer pool and generally request for memory is small and so buffer pool satisfies the requests.
    However there are some direct memory allocation by windows API which is done by windows for requests which  requires memory greater than 8 KB. This memory wont come from buffer pool and this would be counted in memory utilized by SQL server. SQL
    CLR,XML, linked Server, all takes memory outside buffer pool in SQL Server 2008. This behavior changed in 2012 so my statement only applies to SQL server 2005/2008 and 2008 R2.
    I would run below to check SQL server memory utilization.
    select
    (physical_memory_in_use_kb/1024)Memory_usedby_Sqlserver_MB,
    (locked_page_allocations_kb/1024 )Locked_pages_used_Sqlserver_MB,
    (total_virtual_address_space_kb/1024 )Total_VAS_in_MB,
    process_physical_memory_low,
    process_virtual_memory_low
    from sys. dm_os_process_memory
    I am sure you must be using some features mentioned above.You were worried that even though there is no query running on SQL server it has still holded up the memory. This is default behavior and this is how memory management in SQL Server works. It will
    not release memory untill SQLOS asks it to do so which is told by windows OS thorugh some flag that it is facing memory pressure.
    Please read below for more details on memory. There is section to test whether system is facing memory crunch please read that as well.
    http://social.technet.microsoft.com/wiki/contents/articles/22316.sql-server-memory-and-troubleshooting.aspx
    Please mark this reply as answer if it solved your issue or vote as helpful if it helped so that other forum members can benefit from it.
    My TechNet Wiki Articles

  • OWB Error for using Decode in Expression

    Debug code deployment messages:
    LINE 4558 ,COLUMN 71:
    PLS-00204: function or pseudo-column 'DECODE' may be used inside a SQL statement only
    LINE 4558 ,COLUMN 17:
    PL/SQL: Statement ignored
    End debug code deployment messages
    DBG1012: Debug deployment errors, can't run debug code.

    What i have experience is that if you use Decode inside the expression the OWB cannot validate it .
    But it will execute perfect.
    Use Case statement so that you can validate it and debug then
    Cheers
    Nawneet

  • Using Decode in Collections

    when i using decode in queries written for collection i.e
    wwv_flow_item.select_list_from_query_xl(13,'','select decode(id,123,''XYZ'',name) from mytable ','','NO') Field1,
    I get the error
    report error:
    ORA-06550: line 1, column 120:
    PL/SQL: ORA-00913: too many values
    ORA-06550: line 1, column 7:
    PL/SQL: SQL Statement ignored
    in the front end ?
    Please let me know whether we can use decode in queries wrriten in collections if so how
    Thanks in Advance

    when i using decode in queries written for collection i.e
    wwv_flow_item.select_list_from_query_xl(13,'','select decode(id,123,''XYZ'',name) from mytable ','','NO') Field1,
    I get the error
    report error:
    ORA-06550: line 1, column 120:
    PL/SQL: ORA-00913: too many values
    ORA-06550: line 1, column 7:
    PL/SQL: SQL Statement ignored
    in the front end ?
    Please let me know whether we can use decode in queries wrriten in collections if so how
    Thanks in Advance

  • How to use decode concept in htp.p?

    Hi All,
    How to use decode function in htp.p syntax?
    Thanks,
    Anoo..

    Hi,
    Could you please give more details on what you like to achieve? Decode function works only in sql query.
    You can first do sql query, and then do htp.p.
    Or you can use PL/SQL IF or CASE statements.
    You can also create custom decode function if you need.
    Regards,
    Oleg

  • How to use Decode with a variable

    Maybe you've seen this done.
    I want to take statement strMenu4 and subtract the previous value
    that is stored in an array as shown in strMenu5.
    My problem is this change as shown in strMenu5 produces
    an error code:
    ORA-00904 invalid column name
    So either I'm coding something wrong or it can't be done within the
    SUM(decode) construct. If I replace the variable with a literal
    like 1000 the code works:
    SUM(decode(name,'DBWR lru scans', c1.value) - 1000) DBWR_lru_scans
    but with an array variable or even a constant the code fails:
    SUM(decode(name,'DBWR lru scans', c1.value) - Array(3)) DBWR_lru_scans, "
    Any ideas how I can use Decode with a variable and not have it interrupt it
    as a invalid column name?
    Thanks,
    Joseph Karpinski
    [email protected]
    [email protected]
    strMenu4 = "SELECT d1.instance, " _
    & " to_char(sysdate, 'dd-mon-yy hh24:mi:ss') start_time, " _
    & " SUM(decode(name,'DBWR buffers scanned', c1.value)) DBWR_buffers_scanned, " _
    & " SUM(decode(name,'DBWR lru scans', c1.value)) DBWR_lru_scans, " _
    & " SUM(decode(name,'db block gets', c1.value)) db_block_gets , " _
    & " SUM(decode(name,'consistent gets', c1.value)) consistent_gets , " _
    & " SUM(decode(name,'physical reads', c1.value)) physical_reads " _
    & " from dual, v$sysstat c1, v$thread d1 " _
    & " group by d1.instance "
    strMenu5 = "SELECT d1.instance, " _
    & " to_char(sysdate, 'dd-mon-yy hh24:mi:ss') start_time, " _
    & " SUM(decode(name,'DBWR buffers scanned', c1.value) - Array(2)) DBWR_buffers_scanned, " _
    & " SUM(decode(name,'DBWR lru scans', c1.value) - Array(3)) DBWR_lru_scans, " _
    & " SUM(decode(name,'db block gets', c1.value) - Array(4)) db_block_gets , " _
    & " SUM(decode(name,'consistent gets', c1.value) - Array(5)) consistent_gets , " _
    & " SUM(decode(name,'physical reads', c1.value) - Array(6)) physical_reads " _
    & " from dual, v$sysstat c1, v$thread d1 " _
    & " group by d1.instance "

    Re-posting in the SQL and PL/SQL forum.
    It's more appropriate.
    Thanks,
    Closing

  • Can I use decode( decode...)...)

    Hi,
    Can I use decode function inside decode function.
    It seem doesn't work. Please help
    decode(lv.RANK||' / '||
    decode('('||lv.ONE_YR_PCT||'%)', '(%)','N/A','('||lv.ONE_YR_PCT||'%)'),
    '0 / (0%)','N/A', lv.RANK )
    Thanks

    I'm not quite sure it's the result you intended but your nested decode statement does run ...
    SQL> CREATE TABLE lv (rank number, one_yr_pct number)
      2  /
    Table created.
    SQL> insert into lv values (1, 1)
      2  /
    1 row created.
    SQL> insert into lv values (2,2)
      2  /
    1 row created.
    SQL> insert into lv values (0,0)
      2  /
    1 row created.
    SQL> select decode(lv.RANK||' / '||
      2  decode('('||lv.ONE_YR_PCT||'%)', '(%)','N/A','('||lv.ONE_YR_PCT||'%)'),
      3  '0 / (0%)','N/A', lv.RANK )
      4  from lv
      5  /
    DECODE(LV.RANK||'/'||DECODE('('||LV.ONE_
    1
    2
    N/A
    SQL> If you're having problems maybe you should cut'n'paste the output from your SQL*Plus session so we can see what's happening.
    Alternatively, as CD suggests, you may find it easier to express your logic using CASE.
    Cheers, APC

Maybe you are looking for

  • I want to migrate email history to profile 2. It's available on the c-drive or usb drive it won't let me browse to the location when i click import.

    We set up two profiles in Thuinderbird. Two addresses, Two address books, etc. I want to migrate email history to profile 2 in thunderbird. when I get to IMPORT it does not give me the option of browsing to the location of the outlook express file fo

  • How do you check CPU temperature w/o using Bios?

    I saw many many posts about this and just want to make sure I get the correct software to do this (If I can)  I'd like to be able to check my temperature without going to the BIOS. Motherboard    K9N2 SLI Platinum Windows Vista 64 bit AMD Phenom 9950

  • How do I dsable attack site warnings. I never got these before.

    Where do I find the Security Tab to uncheck "Block reported attack sites". I never got this message before and I have always visited Virginia Tech's website for sports (Hokiesports.com) and I know it is not an attack site. Is this a new feature? I wa

  • Movement type 101

    Hi experts, Suppose i received material from supplier against PO , at the time of creating GRV it indicate 101 Movement Type  & also while creating GRV against stock transfer order ( inter unit transfer) it indicate  101 Movement Type . So at the the

  • Novell vs. Tally Collector

    While browsing through the various posts in the forum I came across this comment from one of the users that caught my attention: "You can launch the collection editor from the client workstation to perform the inventory using the command line below,