Solved

How do I speed up a union query in Oracle database?

Posted on 2009-05-04
5
761 Views
Last Modified: 2012-05-06
This is the query:

SELECT EXCEPTION_TYPE, 
NVL(LAST_CHG_DATE, TRUNC(SYSDATE)) LAST_CHG_DATE, HOUSE, CUST,
(CASE WHEN OPER = 'D80' THEN '000' ELSE OPER END) OPER,
(CASE WHEN OPER_NAME = 'USER-ITCDEMO' THEN 'UNKNOWN' ELSE OPER_NAME END) OPER_NAME
FROM
 
 
 
(SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME from EXCEPT_HSI_PHONE_ONLY_CHG
UNION
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_MULT_VID_PKG
UNION
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_MULT_VID_SERV
UNION
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_MULT_VID_BUS
UNION 
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_NO_DVR_CHG
UNION
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_DIG_ST_CHG
UNION
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_SEASONAL
UNION
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_TENANT_CHG
UNION 
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_XF_MISSING
UNION
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_XG_MISSING
UNION 
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_NO_SUBWISE
UNION
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_NO_SALES_REP
UNION
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_CANCEL_NP
UNION 
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_PREF_NEW_VIDEO_ONLY
UNION
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_PREF_NO_3BRONZE
UNION
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_BUS_LITE
 
)
 
WHERE HOUSE <> '118247'

Open in new window

0
Comment
Question by:libby49
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 125 total points
ID: 24456719
is HOUSE a numeric column?  If so,  

change

WHERE HOUSE <> '118247'
to

WHERE HOUSE <> 118247

what is the distribution of data in each table for various HOUSE values?  Is NOT 118247 a small portion or are you selecting nearly every row in every table.
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 125 total points
ID: 24636945
to really optimize this query, put the condition inside the subquery, and also use UNION ALL instead of UNION

the tip of sdstuber still applies.
SELECT EXCEPTION_TYPE, 
NVL(LAST_CHG_DATE, TRUNC(SYSDATE)) LAST_CHG_DATE, HOUSE, CUST,
(CASE WHEN OPER = 'D80' THEN '000' ELSE OPER END) OPER,
(CASE WHEN OPER_NAME = 'USER-ITCDEMO' THEN 'UNKNOWN' ELSE OPER_NAME END) OPER_NAME
FROM
(SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME from EXCEPT_HSI_PHONE_ONLY_CHG
WHERE HOUSE <> '118247'
UNION ALL
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_MULT_VID_PKG
WHERE HOUSE <> '118247'
UNION ALL
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_MULT_VID_SERV
WHERE HOUSE <> '118247'
UNION ALL
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_MULT_VID_BUS
UNION ALL
WHERE HOUSE <> '118247'
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_NO_DVR_CHG
WHERE HOUSE <> '118247'
UNION ALL
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_DIG_ST_CHG
WHERE HOUSE <> '118247'
UNION ALL
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_SEASONAL
WHERE HOUSE <> '118247'
UNION ALL
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_TENANT_CHG
WHERE HOUSE <> '118247'
UNION ALL
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_XF_MISSING
WHERE HOUSE <> '118247'
UNION ALL
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_XG_MISSING
WHERE HOUSE <> '118247'
UNION ALL
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_NO_SUBWISE
WHERE HOUSE <> '118247'
UNION ALL
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_NO_SALES_REP
WHERE HOUSE <> '118247'
UNION ALL
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_CANCEL_NP
WHERE HOUSE <> '118247'
UNION ALL
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_PREF_NEW_VIDEO_ONLY
WHERE HOUSE <> '118247'
UNION ALL
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_PREF_NO_3BRONZE
WHERE HOUSE <> '118247'
UNION ALL
SELECT EXCEPTION_TYPE, LAST_CHG_DATE, HOUSE, CUST, OPER, OPER_NAME FROM EXCEPT_BUS_LITE
WHERE HOUSE <> '118247'
)

Open in new window

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 24637123
using "UNION ALL" will include duplicates  "UNION" will remove them.

It's not just a performance issue but functionally different.  If your data is already unique between tables then definitely use UNION ALL

if it is not and you want the duplicates then use UNION ALL.

Depending on your database you shouldn't have to put the where clause inside the sub-query.  Oracle, for instance, will push that clause for you
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24637182
>using "UNION ALL" will include duplicates  "UNION" will remove them.
agreed. however, most people starting with sql only know UNION, and never heard about "UNION ALL" in their lessons (blaming the teachers, it was the same for me!)

>Depending on your database you shouldn't have to put the where clause inside the sub-query.  Oracle, for instance, will push that clause for you
from what I know, better safe then sorry... especially as I do migration of sql from one platform to another all the time ...

0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question