SQL & PL/SQL :: Improve Query So That UNION Can Be Removed
Mar 6, 2012
select
a.empno,a.ename,a.job,
B.DNAME
from scott.emp a,scott.dept b
where ( a.ename like 'S%' and a.deptno=b.deptno)
union
select
a.empno,a.ename,a.job,
'aaa' AS DNAME
from scott.emp a,scott.dept b
where ( a.ename like 'S%' and a.job not like 'SALES%');
I have a big table in which we load about 37M recrods. We have informatica ETL which Loads the data in bulk Mode and creats index after completion. The data load takes about 1Hr and Index Creation takes about 1/2 hr. In total it takes about 90 to 95 Mnts.
Now I thought if Partition and Load paralley, It will improve perfromance. We did 4 partition and and each Partition about 9M records. The data load in Bulk mode is completing in 25 Mnts. Again When I am creating index over it, It is taking about 40 Mnts. and in Total Load time is 65 Mnts.
Is there way I can better performance to complete the load in 1/2 hr ?
I have inherited a query that union alls 2 select statements, I added a further field to one of the select statements ( a date field). However I need to add another dummy field to the 2nd select statement so the union query marries up I have tried to do this by simply adding a
select 'date_on' to add a field called date on populated by 'date_on' (the name of the column in the first query)
however when I run the union query i get the error Ora-01790 expression must have same datatype as corresponding expression.
I'm just trying to see if there is another way of doing this query without using a UNION. The only way I can see is using a UNION but maybe I'm missing something or a way to do it without a UNION.
Result:
Select any customer within the user_states table who lives in "MO" Select any customer within the user_cars table who lives in "MO" and has a "White" car Select any customer within the user_plates table who lives in "MO" and has a plate of "A" or "B" Join to the user_names table to display the customers name
So, the result would be any customer who lives in "MO" who owns a "White" car and any customer who lives in "MO" who has a plate of "A" or "B".
I am new to SQL Oracle programming and have a question on a union query. I am trying to get results (example) for the following:
Org # Full_date Run_date 5 2/20/2012 2/20/2012 5 2/21/2012 2/21/2012 5 2/22/2012 null (there is not a record for this) 6 2/20/2012 2/20/2012 6 2/21/2012 null 6 2/22/2012 2/22/2012 7 2/20/2012 2/20/2012 7 2/21/2012 2/21/2012 7 2/22/2012 2/22/2012
The dw_time table would have the listing of all dates, (Full_date) and the dw_capacity_daily table would have the run_date. Here is my
select * from ( select distinct a.Organization_Nbr, d.full_date from CMBHS_DW.DW_ORGANIZATION a, cmbhs_DW.DW_Organization_Identifier b, cmbhs_DW.DW_Contract c, cmbhs_dw.dw_time d where a.ORGANIZATION_NBR = b.ORGANIZATION_NBR [code]....
I have one scenario in which i want to write the sql,but not able to write correct qry,
tables 1-emp 2-emp_hist 3-dept 4-dept_hist.
i want to retrieve data from the emp and hist able based on some conditions,but if data is not present in emp and dept tables.then fetch data from emp_hist and dept_hist.I have written below qry which is working fine,but my prob is i want to provide my emp_id only one place. else i have to change my java code.
select * from emp,dept where emp.emp_id=dept.dept_id and emp_id=5 union select * from emp_hist,dept_hist where emp_hist.emp_id=dept_hist.dept_id and emp_id=5
We have very large table having data more than 1000 millions rows. We divide this table into four physical tables say A, B, C and D. The physical horizontal partition of data of this original table is done based upon their business policy.
Each partitioned table has contained data of particular business entity. Further each table has partition and sub partitions based upon business rule.
We have to retrieve data from all these tables as follows:
select a1, a2, a3, a4, a5, a6 from A where < logical filter condition> union all select b1, b2, b3, b4, b5, b6 [code].....
We observed that above each query block execute in serial one after another and individual each query block capable to process data in parallel from respective table.
How does this above query able to execute each query block in parallel?
The DBAs have built 2 Materialized Views, which aggregates data in the fact tables at column C1 level
MAT_VIEW1 :SELECT C1_ID, SUM(SALE) SALES from FACTTABLE1 join LOOKUPTABLE on C1_ID MAT_VIEW2: SELECT C1_ID, SUM(SALE) SALE from FACTTABLE2 join LOOKUPTABLE on C1_ID
We are using an old BI tool that can ONLY generate Inline Views in these formats.
CASE1: select INL_VIEW.C1_ID ,LOOKUPTABLE.C1_NAME ,sum(SALE) SALE from (select C1_ID, C2_ID, null C3_ID, SALE from FACTTABLE1)INL_VIEW join LOOKUPTABLE on INL_VIEW.C1_ID = LOOKUPTABLE.C1_ID group by INL_VIEW.C1_ID, LOOKUPTABLE.C1_NAME
CASE2: select INL_VIEW.C1_ID ,LOOKUPTABLE.C1_NAME ,sum(SALE) SALE from (select C1_ID, null C2_ID, C3_ID, SALE from FACTTABLE2)INL_VIEW join LOOKUPTABLE on INL_VIEW.C1_ID = LOOKUPTABLE.C1_ID group by INL_VIEW.C1_ID, LOOKUPTABLE.C1_NAME
CASE3: select INL_VIEW.C1_ID ,LOOKUPTABLE.C1_NAME ,sum(SALE) SALE from ( select C1_ID, C2_ID, null C3_ID, SALE from FACTTABLE1 union all select C1_ID, C2_ID, null C3_ID, SALE from FACTTABLE2 )INL_VIEW join LOOKUPTABLE on INL_VIEW.C1_ID = LOOKUPTABLE.C1_ID group by INL_VIEW.C1_ID, LOOKUPTABLE.C1_NAME
Oracle 11g rewrites Case 1 and Case 2 to use the correct materialized views. But for case 3, it goes to the base fact tables 1 and 2. Is there a way to make oracle use the MVs even if there is a UNION ALL in the inline view? There is a 1:M Foreign Key relationship between LOOKUPTABLE.C1_ID and the 2 fact tables.
I have a SQL query where I am making UNION of two select statements. The table that I am joining in each select statement have indexes defined for those tables.
Now the UNION of the two select statements again in enclosed in an inline view , from which I fetching my final field values.
The select statements inside the inline view returns huge number of row (like 50 million rows).
The whole query fails with time out.
How can I optimize this query further?
Is there a way to pass Oracle Hints so that Oracle uses indexes?
I am making a report in hrd regarding gross, deduction and there difference. I hv an attribute in the table as indicator whose value is addition and deduction. i want the sum of both in two diff column in a single rep. i want the report dept wise.
but i m getting fatal error at run time. which i don't understand.
I have a SQL query where I am making UNION of two select statements. The table that I am joining in each select statement have indexes defined for those tables.
Now the UNION of the two select statements again in enclosed in an inline view , from which I fetching my final field values.
The select statements inside the inline view returns huge number of row (like 50 million rows).
The whole query fails with time out.
Is there a way to pass Oracle Hints so that Oracle uses indexes?
I have the following Union All query. It throws the following error in SQL plus
ERROR at line 27: ORA-01789: query block has incorrect number of result columns
After doing some google for the above error it suggests there are incorrect number of columns in the Union All query.I could not figure out the exact location well SQl Plus says error is on line 27 at the first opening bracket like
Someone(a non DBA) removed the only sysaux datafile from the server using the rm command Obviously, the DB is still up and none of the schemas in the database have their objects right now.
This is not a critical database as you can see when I say that I have NO backup whatsoever. Even though I have the option of deleting and recreating the database, i want to know if I can avoid doing that.
Creating a file on the server with the same name OR creating a new datafile for the tablespace OR recreate the sysaux tablespace ?But how to do any of these? What else should be done ?
So, we had multiple set ups of primary database in our local data center, standby in remote location.
We're in the process of shutting down our remote location and have shut down our standbys gracefully, i.e., shutdown managed standby mode, removed archive_log_dest, changed archive_log_dest_state, etc. However, in some primary databases, I'm seeing a weird entry in the alert log:
Changing destination 2 from remote to local during archival of log#: 2 sequence#: 48708 thread#: 1
If I check my local destination, the archive log is created fine. In the remote location, no archives are being created.
Although i have removed current online redo log file in linux os (Oracle Linux),when i type "commit" it says that "commit complete".
Is this fair for this princip?*:" if Only when all redo records associated with a given transaction are safely on disk in the online logs is the user process notified that the transaction has been committed."*
I think that it can lead to loss of data in some cases..I'm using Oracle 11g R2 on OEL (x64)..
P.S : I haven't multiplexed current ORL group files...
It seems certain queries search by the number of days to ship(number of days between the order and shipping dates). What kind of index would improve the performance of these queries?
Why do stored procedures and functions improve performance?
A. They reduce network round trips. B. They reduce the number of calls to the database and decrease network traffic by bundling commands. C. They reduce the number of calls to the database and decrease network traffic by using the local PL/SQL engine. D. They allow the application to perform high-speed processing locally. E. They postpone PL/SQL parsing until run time.
I think the answer should be A and B but i came across answers as B and E Can u explain me what is the difference between option A and B and does it postpone parsing till run time?
In search queries generally we select 10-25 columns (more can't be displayed on the screen) from 5-10 tables
Say in case of insurance related application, the search might be on policy number, policy holder's first name, policy holder's last name, region, policy type etc.
And not to many columns we are displaying on the screen, say, 4 tables have collectively 4 * 20 = 80 columns, then we are displaying say 12-15 columns with 2-3 columns have aggregates on it.
since the search criteria (e.g. first name, last name, policy number etc.) is not known till last moment it will be a generic dynamic query
Is it possible that instead we create a Materialized view with query with only joining conditions but no filter conditions and selecting only columns to be displayed on the screen and then we will refresh the materialized view (to take care of recent business transactions) and fire refined query with filter criteria on this materialized view
Select col1,col2,col3,col4,col5 From tab1,tab2,tab3,tab4 Where tab1.col1=tab2.col1 And tab2.col2=tab3.col2 And tab2.col2=tab4.col2;
Will it improve performance of the search functionality
When i exporting an user using expdp utility, the load the on the server is going up-to 5. The size of the database is 180GB. Below is the command that i use for export.
- returning from the procedure 2 ref cursors containing result set of 2 queries - returning from the procedure 1 ref cursor containing result set of that 2 queries as one (with UNION ALL)?
I have a query which has 5 unions, each clause of the union takes 1 hr to run and query results come back in 5 hrs, Is there any way I can make these clause to run in parallel?
Below is my query Issue faced is that based on union I want to limit the result and does not want second query to give duplicate result as both queries deal with same table
SELECT TAB1.ID, TAB1.CRNCY_CODE, TAB2.SCHEME_CODE, DECODE( 'INFENG',CAST( '' AS VARCHAR(20)), NVL(TAB2.ALT1_SCHEME_DESC,TAB2.SCHEME_DESC),TAB2.SCHEME_DESC), TAB1.SUB_HEAD_CODE, DECODE( 'INFENG' ,CAST( '' AS VARCHAR(20)),NVL(TAB1.ALT1_SUB_HEAD_DESC , TAB1.SUB_HEAD_DESC),TAB1.SUB_HEAD_DESC)
[code]...
select null ID,null crncy_code,TAB2.SCHEME_CODE , NVL(TAB2.ALT1_SCHEME_DESC,TAB2.SCHEME_DESC),TAB2.SCHEME_DESC,null SUB_HEAD_CODE from TAB2 where TAB2.SCHEME_TYPE ='SCHEME1';
Cant find any artice on what is better to use unpivot or union all. I can achive same results using either but cant decide what would be better. For example a table has columns:
Name 1, Phone1, Name 2, Phone 2. The result I want would be: