Use 'UNION ALL' in oracle SQL or not?












1















I am trying to display employee properties using C# WPF view. I have data in different oracle tables in my DB.



Though there are a set of multiple tables, each set consists of the same structure. Those tables structure at high-level is...



Tables Structure



Employee table columns            (EMP table)  : ID, Name, Organisation
Employee properties table columns (EMPPR table): ID, PropertyName, PropertyValues


Each employee's ID and Name in EMP table and employee's properties in EMPPR table. Each employee has properties from 40-80 i.e. 40-80 rows per employee in EMPPR table.



Like this each department has one set of tables. i.e. (DEVEMP,DEVEMPPR),
(HREMP,HREMPPR),
(ADMINEMP, ADMINEMPPR),
(FINEMP, FINEMPPR),
(ADMINEMP, ADMINEMPPR) etc...



I need to fetch employee properties of multiple departments at once where I get lists of employee names department wise.



I am using the following query to fetch employee properties for inputted names as follows:



SELECT Pr.PropertyName, Pr.PropertyValue
FROM DEVEMP Emp
JOIN DEVEMPPR Pr ON Emp.ID = Pr.ID
AND Emp.Name IN (<List of DEV Names Inputted>)


Like this, I need to execute for each department.



Is query will be efficient if I use UNION ALL with all department tables query and IN condition with the combined list as below?



VIEW using UNION ALL



CREATE OR REPLACE FORCE VIEW EMPPRVIEW(NAME, PRNAME, PRVALUE) AS

SELECT Emp.Name, Pr.PropertyName, Pr.PropertyValue
FROM DEVEMP Emp
JOIN DEVEMPPR Pr ON Emp.ID = Pr.ID

UNION ALL

SELECT Emp.Name, Pr.PropertyName, Pr.PropertyValue
FROM HREMP Emp
JOIN HREMPPR Pr ON Emp.ID = Pr.ID

UNION ALL

SELECT Emp.Name, Pr.PropertyName, Pr.PropertyValue
FROM ADMINEMP Emp
JOIN ADMINEMPPR Pr ON Emp.ID = Pr.ID

UNION ALL

.
.
.


QUERY FROM VIEW



SELECT NAME, PRNAME, PRVALUE
FROM EMPPRVIEW
WHERE NAME IN (<Combined list of names from each dept>)


Approach #2 is easy in implementing as we execute query at once, but I am thinking it degrades query performance.



Which approach is better single query execution with UNION ALL or separate query execution for each department?



PS. The inputted emp names list may contain 1000 names for each department and each employee has 40-80 properties.










share|improve this question

























  • the UNION ALL is just one query instead separated query are as many as you do .. so normally the UNION ALL query is more performant

    – scaisEdge
    Nov 24 '18 at 17:12








  • 2





    The table structure is foobar. Unless you fix it this might be your best option.

    – MatBailie
    Nov 24 '18 at 17:13











  • What about using VIEW (question edited)? Even still using VIEW is a good option?

    – Upendhar Singirikonda
    Nov 24 '18 at 17:23











  • Well, my impression, both aproaches will be aproximately same inefficient. Your only option to get rid of this NoSQL table desing (completel unsuitable for relational databases) is IMO to take your big UNION ALL view, transform it in a materialized view add propper indices and make queries on it.

    – Marmite Bomber
    Nov 24 '18 at 21:30











  • I agree this should be redesigned and you should suggest that to whoever controls these things. There's no such thing an efficient query, just one that is performant enough for your purposed. Turning it into a view will probably have no effect on query performance.

    – eaolson
    Nov 24 '18 at 22:53
















1















I am trying to display employee properties using C# WPF view. I have data in different oracle tables in my DB.



Though there are a set of multiple tables, each set consists of the same structure. Those tables structure at high-level is...



Tables Structure



Employee table columns            (EMP table)  : ID, Name, Organisation
Employee properties table columns (EMPPR table): ID, PropertyName, PropertyValues


Each employee's ID and Name in EMP table and employee's properties in EMPPR table. Each employee has properties from 40-80 i.e. 40-80 rows per employee in EMPPR table.



Like this each department has one set of tables. i.e. (DEVEMP,DEVEMPPR),
(HREMP,HREMPPR),
(ADMINEMP, ADMINEMPPR),
(FINEMP, FINEMPPR),
(ADMINEMP, ADMINEMPPR) etc...



I need to fetch employee properties of multiple departments at once where I get lists of employee names department wise.



I am using the following query to fetch employee properties for inputted names as follows:



SELECT Pr.PropertyName, Pr.PropertyValue
FROM DEVEMP Emp
JOIN DEVEMPPR Pr ON Emp.ID = Pr.ID
AND Emp.Name IN (<List of DEV Names Inputted>)


Like this, I need to execute for each department.



Is query will be efficient if I use UNION ALL with all department tables query and IN condition with the combined list as below?



VIEW using UNION ALL



CREATE OR REPLACE FORCE VIEW EMPPRVIEW(NAME, PRNAME, PRVALUE) AS

SELECT Emp.Name, Pr.PropertyName, Pr.PropertyValue
FROM DEVEMP Emp
JOIN DEVEMPPR Pr ON Emp.ID = Pr.ID

UNION ALL

SELECT Emp.Name, Pr.PropertyName, Pr.PropertyValue
FROM HREMP Emp
JOIN HREMPPR Pr ON Emp.ID = Pr.ID

UNION ALL

SELECT Emp.Name, Pr.PropertyName, Pr.PropertyValue
FROM ADMINEMP Emp
JOIN ADMINEMPPR Pr ON Emp.ID = Pr.ID

UNION ALL

.
.
.


QUERY FROM VIEW



SELECT NAME, PRNAME, PRVALUE
FROM EMPPRVIEW
WHERE NAME IN (<Combined list of names from each dept>)


Approach #2 is easy in implementing as we execute query at once, but I am thinking it degrades query performance.



Which approach is better single query execution with UNION ALL or separate query execution for each department?



PS. The inputted emp names list may contain 1000 names for each department and each employee has 40-80 properties.










share|improve this question

























  • the UNION ALL is just one query instead separated query are as many as you do .. so normally the UNION ALL query is more performant

    – scaisEdge
    Nov 24 '18 at 17:12








  • 2





    The table structure is foobar. Unless you fix it this might be your best option.

    – MatBailie
    Nov 24 '18 at 17:13











  • What about using VIEW (question edited)? Even still using VIEW is a good option?

    – Upendhar Singirikonda
    Nov 24 '18 at 17:23











  • Well, my impression, both aproaches will be aproximately same inefficient. Your only option to get rid of this NoSQL table desing (completel unsuitable for relational databases) is IMO to take your big UNION ALL view, transform it in a materialized view add propper indices and make queries on it.

    – Marmite Bomber
    Nov 24 '18 at 21:30











  • I agree this should be redesigned and you should suggest that to whoever controls these things. There's no such thing an efficient query, just one that is performant enough for your purposed. Turning it into a view will probably have no effect on query performance.

    – eaolson
    Nov 24 '18 at 22:53














1












1








1








I am trying to display employee properties using C# WPF view. I have data in different oracle tables in my DB.



Though there are a set of multiple tables, each set consists of the same structure. Those tables structure at high-level is...



Tables Structure



Employee table columns            (EMP table)  : ID, Name, Organisation
Employee properties table columns (EMPPR table): ID, PropertyName, PropertyValues


Each employee's ID and Name in EMP table and employee's properties in EMPPR table. Each employee has properties from 40-80 i.e. 40-80 rows per employee in EMPPR table.



Like this each department has one set of tables. i.e. (DEVEMP,DEVEMPPR),
(HREMP,HREMPPR),
(ADMINEMP, ADMINEMPPR),
(FINEMP, FINEMPPR),
(ADMINEMP, ADMINEMPPR) etc...



I need to fetch employee properties of multiple departments at once where I get lists of employee names department wise.



I am using the following query to fetch employee properties for inputted names as follows:



SELECT Pr.PropertyName, Pr.PropertyValue
FROM DEVEMP Emp
JOIN DEVEMPPR Pr ON Emp.ID = Pr.ID
AND Emp.Name IN (<List of DEV Names Inputted>)


Like this, I need to execute for each department.



Is query will be efficient if I use UNION ALL with all department tables query and IN condition with the combined list as below?



VIEW using UNION ALL



CREATE OR REPLACE FORCE VIEW EMPPRVIEW(NAME, PRNAME, PRVALUE) AS

SELECT Emp.Name, Pr.PropertyName, Pr.PropertyValue
FROM DEVEMP Emp
JOIN DEVEMPPR Pr ON Emp.ID = Pr.ID

UNION ALL

SELECT Emp.Name, Pr.PropertyName, Pr.PropertyValue
FROM HREMP Emp
JOIN HREMPPR Pr ON Emp.ID = Pr.ID

UNION ALL

SELECT Emp.Name, Pr.PropertyName, Pr.PropertyValue
FROM ADMINEMP Emp
JOIN ADMINEMPPR Pr ON Emp.ID = Pr.ID

UNION ALL

.
.
.


QUERY FROM VIEW



SELECT NAME, PRNAME, PRVALUE
FROM EMPPRVIEW
WHERE NAME IN (<Combined list of names from each dept>)


Approach #2 is easy in implementing as we execute query at once, but I am thinking it degrades query performance.



Which approach is better single query execution with UNION ALL or separate query execution for each department?



PS. The inputted emp names list may contain 1000 names for each department and each employee has 40-80 properties.










share|improve this question
















I am trying to display employee properties using C# WPF view. I have data in different oracle tables in my DB.



Though there are a set of multiple tables, each set consists of the same structure. Those tables structure at high-level is...



Tables Structure



Employee table columns            (EMP table)  : ID, Name, Organisation
Employee properties table columns (EMPPR table): ID, PropertyName, PropertyValues


Each employee's ID and Name in EMP table and employee's properties in EMPPR table. Each employee has properties from 40-80 i.e. 40-80 rows per employee in EMPPR table.



Like this each department has one set of tables. i.e. (DEVEMP,DEVEMPPR),
(HREMP,HREMPPR),
(ADMINEMP, ADMINEMPPR),
(FINEMP, FINEMPPR),
(ADMINEMP, ADMINEMPPR) etc...



I need to fetch employee properties of multiple departments at once where I get lists of employee names department wise.



I am using the following query to fetch employee properties for inputted names as follows:



SELECT Pr.PropertyName, Pr.PropertyValue
FROM DEVEMP Emp
JOIN DEVEMPPR Pr ON Emp.ID = Pr.ID
AND Emp.Name IN (<List of DEV Names Inputted>)


Like this, I need to execute for each department.



Is query will be efficient if I use UNION ALL with all department tables query and IN condition with the combined list as below?



VIEW using UNION ALL



CREATE OR REPLACE FORCE VIEW EMPPRVIEW(NAME, PRNAME, PRVALUE) AS

SELECT Emp.Name, Pr.PropertyName, Pr.PropertyValue
FROM DEVEMP Emp
JOIN DEVEMPPR Pr ON Emp.ID = Pr.ID

UNION ALL

SELECT Emp.Name, Pr.PropertyName, Pr.PropertyValue
FROM HREMP Emp
JOIN HREMPPR Pr ON Emp.ID = Pr.ID

UNION ALL

SELECT Emp.Name, Pr.PropertyName, Pr.PropertyValue
FROM ADMINEMP Emp
JOIN ADMINEMPPR Pr ON Emp.ID = Pr.ID

UNION ALL

.
.
.


QUERY FROM VIEW



SELECT NAME, PRNAME, PRVALUE
FROM EMPPRVIEW
WHERE NAME IN (<Combined list of names from each dept>)


Approach #2 is easy in implementing as we execute query at once, but I am thinking it degrades query performance.



Which approach is better single query execution with UNION ALL or separate query execution for each department?



PS. The inputted emp names list may contain 1000 names for each department and each employee has 40-80 properties.







sql oracle union-all sqlperformance






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 19:05







Upendhar Singirikonda

















asked Nov 24 '18 at 17:10









Upendhar SingirikondaUpendhar Singirikonda

495




495













  • the UNION ALL is just one query instead separated query are as many as you do .. so normally the UNION ALL query is more performant

    – scaisEdge
    Nov 24 '18 at 17:12








  • 2





    The table structure is foobar. Unless you fix it this might be your best option.

    – MatBailie
    Nov 24 '18 at 17:13











  • What about using VIEW (question edited)? Even still using VIEW is a good option?

    – Upendhar Singirikonda
    Nov 24 '18 at 17:23











  • Well, my impression, both aproaches will be aproximately same inefficient. Your only option to get rid of this NoSQL table desing (completel unsuitable for relational databases) is IMO to take your big UNION ALL view, transform it in a materialized view add propper indices and make queries on it.

    – Marmite Bomber
    Nov 24 '18 at 21:30











  • I agree this should be redesigned and you should suggest that to whoever controls these things. There's no such thing an efficient query, just one that is performant enough for your purposed. Turning it into a view will probably have no effect on query performance.

    – eaolson
    Nov 24 '18 at 22:53



















  • the UNION ALL is just one query instead separated query are as many as you do .. so normally the UNION ALL query is more performant

    – scaisEdge
    Nov 24 '18 at 17:12








  • 2





    The table structure is foobar. Unless you fix it this might be your best option.

    – MatBailie
    Nov 24 '18 at 17:13











  • What about using VIEW (question edited)? Even still using VIEW is a good option?

    – Upendhar Singirikonda
    Nov 24 '18 at 17:23











  • Well, my impression, both aproaches will be aproximately same inefficient. Your only option to get rid of this NoSQL table desing (completel unsuitable for relational databases) is IMO to take your big UNION ALL view, transform it in a materialized view add propper indices and make queries on it.

    – Marmite Bomber
    Nov 24 '18 at 21:30











  • I agree this should be redesigned and you should suggest that to whoever controls these things. There's no such thing an efficient query, just one that is performant enough for your purposed. Turning it into a view will probably have no effect on query performance.

    – eaolson
    Nov 24 '18 at 22:53

















the UNION ALL is just one query instead separated query are as many as you do .. so normally the UNION ALL query is more performant

– scaisEdge
Nov 24 '18 at 17:12







the UNION ALL is just one query instead separated query are as many as you do .. so normally the UNION ALL query is more performant

– scaisEdge
Nov 24 '18 at 17:12






2




2





The table structure is foobar. Unless you fix it this might be your best option.

– MatBailie
Nov 24 '18 at 17:13





The table structure is foobar. Unless you fix it this might be your best option.

– MatBailie
Nov 24 '18 at 17:13













What about using VIEW (question edited)? Even still using VIEW is a good option?

– Upendhar Singirikonda
Nov 24 '18 at 17:23





What about using VIEW (question edited)? Even still using VIEW is a good option?

– Upendhar Singirikonda
Nov 24 '18 at 17:23













Well, my impression, both aproaches will be aproximately same inefficient. Your only option to get rid of this NoSQL table desing (completel unsuitable for relational databases) is IMO to take your big UNION ALL view, transform it in a materialized view add propper indices and make queries on it.

– Marmite Bomber
Nov 24 '18 at 21:30





Well, my impression, both aproaches will be aproximately same inefficient. Your only option to get rid of this NoSQL table desing (completel unsuitable for relational databases) is IMO to take your big UNION ALL view, transform it in a materialized view add propper indices and make queries on it.

– Marmite Bomber
Nov 24 '18 at 21:30













I agree this should be redesigned and you should suggest that to whoever controls these things. There's no such thing an efficient query, just one that is performant enough for your purposed. Turning it into a view will probably have no effect on query performance.

– eaolson
Nov 24 '18 at 22:53





I agree this should be redesigned and you should suggest that to whoever controls these things. There's no such thing an efficient query, just one that is performant enough for your purposed. Turning it into a view will probably have no effect on query performance.

– eaolson
Nov 24 '18 at 22:53












1 Answer
1






active

oldest

votes


















1














I concur with MatBailie's opinion; this should have been done as a column in a single Employee table. You could change it to that, and create updatable views mirroring your existing table structure so that the application carries on working while you switch it over to using a single unified table for Employees



In terms of your asking about query efficiency and presumably performance, you're going to have to test it. I only foresee performance problems if oracle won't use an index on each table during the union op..



So to answer your question: it'll work, but you should consider a change the structure rather than persist with finding ways of fudging around what is a problematic initial design decision. The only way to know if it will perform well enough for your requirement is to (stress) test it






share|improve this answer


























  • Sorry. I can't change DB design, that is not in my hand. The application context and usage is completely different. Just for explanation purpose, I used the above example.

    – Upendhar Singirikonda
    Nov 24 '18 at 17:52











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460524%2fuse-union-all-in-oracle-sql-or-not%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I concur with MatBailie's opinion; this should have been done as a column in a single Employee table. You could change it to that, and create updatable views mirroring your existing table structure so that the application carries on working while you switch it over to using a single unified table for Employees



In terms of your asking about query efficiency and presumably performance, you're going to have to test it. I only foresee performance problems if oracle won't use an index on each table during the union op..



So to answer your question: it'll work, but you should consider a change the structure rather than persist with finding ways of fudging around what is a problematic initial design decision. The only way to know if it will perform well enough for your requirement is to (stress) test it






share|improve this answer


























  • Sorry. I can't change DB design, that is not in my hand. The application context and usage is completely different. Just for explanation purpose, I used the above example.

    – Upendhar Singirikonda
    Nov 24 '18 at 17:52
















1














I concur with MatBailie's opinion; this should have been done as a column in a single Employee table. You could change it to that, and create updatable views mirroring your existing table structure so that the application carries on working while you switch it over to using a single unified table for Employees



In terms of your asking about query efficiency and presumably performance, you're going to have to test it. I only foresee performance problems if oracle won't use an index on each table during the union op..



So to answer your question: it'll work, but you should consider a change the structure rather than persist with finding ways of fudging around what is a problematic initial design decision. The only way to know if it will perform well enough for your requirement is to (stress) test it






share|improve this answer


























  • Sorry. I can't change DB design, that is not in my hand. The application context and usage is completely different. Just for explanation purpose, I used the above example.

    – Upendhar Singirikonda
    Nov 24 '18 at 17:52














1












1








1







I concur with MatBailie's opinion; this should have been done as a column in a single Employee table. You could change it to that, and create updatable views mirroring your existing table structure so that the application carries on working while you switch it over to using a single unified table for Employees



In terms of your asking about query efficiency and presumably performance, you're going to have to test it. I only foresee performance problems if oracle won't use an index on each table during the union op..



So to answer your question: it'll work, but you should consider a change the structure rather than persist with finding ways of fudging around what is a problematic initial design decision. The only way to know if it will perform well enough for your requirement is to (stress) test it






share|improve this answer















I concur with MatBailie's opinion; this should have been done as a column in a single Employee table. You could change it to that, and create updatable views mirroring your existing table structure so that the application carries on working while you switch it over to using a single unified table for Employees



In terms of your asking about query efficiency and presumably performance, you're going to have to test it. I only foresee performance problems if oracle won't use an index on each table during the union op..



So to answer your question: it'll work, but you should consider a change the structure rather than persist with finding ways of fudging around what is a problematic initial design decision. The only way to know if it will perform well enough for your requirement is to (stress) test it







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 20:01

























answered Nov 24 '18 at 17:30









Caius JardCaius Jard

12k21240




12k21240













  • Sorry. I can't change DB design, that is not in my hand. The application context and usage is completely different. Just for explanation purpose, I used the above example.

    – Upendhar Singirikonda
    Nov 24 '18 at 17:52



















  • Sorry. I can't change DB design, that is not in my hand. The application context and usage is completely different. Just for explanation purpose, I used the above example.

    – Upendhar Singirikonda
    Nov 24 '18 at 17:52

















Sorry. I can't change DB design, that is not in my hand. The application context and usage is completely different. Just for explanation purpose, I used the above example.

– Upendhar Singirikonda
Nov 24 '18 at 17:52





Sorry. I can't change DB design, that is not in my hand. The application context and usage is completely different. Just for explanation purpose, I used the above example.

– Upendhar Singirikonda
Nov 24 '18 at 17:52




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460524%2fuse-union-all-in-oracle-sql-or-not%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen