Cost of logic in a query

Posted by FrustratedWithFormsDesigner on Stack Overflow See other posts from Stack Overflow or by FrustratedWithFormsDesigner
Published on 2010-05-28T20:46:01Z Indexed on 2010/05/28 20:52 UTC
Read the original article Hit count: 213

Filed under:
|
|

I have a query that looks something like this:

select xmlelement("rootNode",
    (case
       when XH.ID is not null then
     xmlelement("xhID", XH.ID)
       else
     xmlelement("xhID", xmlattributes('true' AS "xsi:nil"), XH.ID)
    end),
    (case
       when XH.SER_NUM is not null then
     xmlelement("serialNumber", XH.SER_NUM)
       else
     xmlelement("serialNumber", xmlattributes('true' AS "xsi:nil"), XH.SER_NUM)
    end),
                /*repeat this pattern for many more columns from the same table...*/
                FROM XH
                WHERE XH.ID = 'SOMETHINGOROTHER'

It's ugly and I don't like it, and it is also the slowest executing query (there are others of similar form, but much smaller and they aren't causing any major problems - yet). Maintenance is relatively easy as this is mostly a generated query, but my concern now is for performance. I am wondering how much of an overhead there is for all of these case expressions.

To see if there was any difference, I wrote another version of this query as:

select xmlelement("rootNode",
                   xmlforest(XH.ID, XH.SER_NUM,...

(I know that this query does not produce exactly the same, thing, my plan was to move the logic to PL/SQL or XSL)

I tried to get execution plans for both versions, but they are the same. I'm guessing that the logic does not get factored into the execution plan. My gut tells me the second version should execute faster, but I'd like some way to prove that (other than writing a PL/SQL test function with timing statements before and after the query and running that code over and over again to get a test sample).

Is it possible to get a good idea of how much the case-when will cost?

Also, I could write the case-when using the decode function instead. Would that perform better (than case-statements)?

© Stack Overflow or respective owner

Related posts about sql

Related posts about Performance