Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: UNION of two SELECT extremely slow
gmilosavljevic wrote:
> Hi All,
>
> I'm fighting with the weird problem. I have a public ASP application
> and suddenly one user reported the problem - very slow rendering of a
> page. I've localized the problem and it is about UNION operation. There
> are two SELECT statements, each of them work very very normaly
> (1-2secs) when being ran separately, but when I try to do UNION of
> them, the whole query executes more than 3minutes (!!!). It's really
> weird.
> Anyone has any thoughts about it?
>
> Thanks a lot in advance!!!
> <SNIP SQL Code>
> Regards,
> Goran M.
Without seeing the wait events, data size, plan, Oracle version, and init.ora parameters everything is likely to be a guess. One of the things that I see is that you use UNION rather than UNION ALL. My guess is that the query is hitting the temporary tablespace for sorting, distinct, and even join operations.
Another way to help the SQL statement is to remove the subquery, if at all possible. This can typically be done by converting it into an inline view. The inline view only needs to be resolved once, rather than once per row. For example, the second half of your SQL statement will look like this (note the NOT IN syntax was replaced by an outer join and then the right side of the join is specified as being null):
UNION ALL
SELECT
VW.ID AS BWEID,
B.PE_SUBMITTED,
VW.BUDGET_ID, VW.CATEGORY_ID, VW.CATEGORY_NAME AS CAPTION, VW.USER_DEFINED_CATEGORY AS ADDLINE,
VW.SUPERCATEGORY_ID AS PARENTID, VW.SUPER_CATEGORY_NAME AS PARENTCAPTION, VW.PARENT_UDC, VW.CAPTION_UNIT_COST, VW.CAPTION_MULTIPLIER_1, VW.CAPTION_MULTIPLIER_2, VW.EXPENSE_ACCOUNT_CODE AS EXPCODE, VW.UNIT_COST, VW.MULTIPLIER_1, VW.MULTIPLIER_2, VW.COMMENTS, VW.APPLICABLE, VW.DISPLAY_SEQUENCE
Charles Hooper
PC Support Specialist
K&M Machine-Fabricating, Inc.
Received on Mon Jul 17 2006 - 06:36:15 CDT