Sie sind auf Seite 1von 7

Database

Exception handling SQLERR ?


Handling sequal error port in SP transformation,need ti
connect to a file to store SP log from DB otherwise it will be
discarded. Else we have to handle DB log in the exception
session of Stored Procedure.
In a procedure individually we can write many operations,
First load source table to some temp table(or to a cursor).
Then write merge statement in when matched compare the
temp table with target table and take appropriate action.
Need work on sequences?
%ROWTYPE ,BULK CURORS.

Find the 2nd max salary in each dept?

1) SELECT max(e1.sal), e1.deptno FROM s_emp e1

WHERE sal < (SELECT max(sal) FROM s_emp e2

WHERE e2.deptno = e1.deptno)

GROUP BY e1.deptno; - Bad performance

2) SELECT * FROM (SELECT S.*,DENSE_RANK() OVER (PARTITION BY DNO ORDER BY SALARY DESC) DR
FROM SOURCE ) S

WHERE S.DR=2; Best performance

Running sum calculation:

SELECT DISTINCT DIM_TIME_ID AS TIME_ID,ORDER BY DIM_TIME_ID


SELECT DISTINCT DIM_TIME_ID AS TIME_ID, SUM(sales_units) OVER (ORDER BY DIM_TIME_ID) AS
SUM_SALES_UNIT FROM f_sales ORDER BY DIM_TIME_ID

Query to find duplicate rows:

Select * from Employee a where rowid <>( select max(rowid) from Employee b where
a.Employee_num=b.Employee_num); ( co related is bad query)

http://www.oratable.com/oracle-merge-command-for-upsert/

Oracle:

BEGIN

For i in (select id, name, desc from table1)

LOOP

Update table2 set name = i.name, desc = i.desc where id = i.id;

END LOOP;

END;

MERGE into <target table>

USING

<souce table/view/result of subquery>

ON

<match condition>

WHEN MATCHED THEN

<update clause>

<delete clause>

WHEN NOT MATCHED THEN

<insert clause>
merge into student a

2 using

3 (select id, name, score

4 from student_n) b

5 on (a.id = b.id)

6 when matched then

7 update set a.name = b.name

8 , a.score = b.score

9 when not matched then

10 insert (a.id, a.name, a.score)

11 values (b.id, b.name, b.score);

Create Or Replace Procedure Proc_SCD1

(CNbr IN Cust.CustNo%Type , PID IN Prod.ProdID%Type)

Is

BEGIN

Create Global Temporary Table SrcTmp ON COMMIT PRESERVE ROWS

As select * from Scr

END;

Merge Tgt t

Using Src s

on (t.custno=s.custno)

when Matched then

Update

Set t.enddt = currentdate-1, t.flg = ‘N’,t.add=s.add

Insert (t.id,t.stdt,t.enddt,t.add,t.flg) values


(s.id,stdt,enddt,s.add,flg)

End;

Oracle support two types of temporary tables. Global Temporary Tables : Available since Oracle 8i and subject of
this article. Private Temporary Tables : Available since Oracle 18c. Discussed here.

Temporary Tables:

Temporary tables have all features alike normal tables but they cannot have foreign keys. And data will be
deleted after session disconnect.

https://oracle-base.com/articles/misc/temporary-tables

www.vertabelo.com/blog/technical-articles/oracle-global-temporary-table

http://www.dba-oracle.com/t_temporary_tables_sql.htm

Global Temporary Table transaction-specific

CREATE GLOBAL TEMPORARY TABLE table name

( column_name column_data_type

...

...

) ON COMMIT DELETE ROWS;

Global Temporary Table session-specific

CREATE GLOBAL TEMPORARY TABLE table_name

( column_name column_data_type

...

...

) ON COMMIT PRESERVE ROWS;

Create Temp Table on Fly:


CREATE GLOBAL TEMPORARY TABLE TEMP_TABLE
ON COMMIT PRESERVE ROWS
AS
select * from existing_table

Sequence:

CREATE SEQUENCE transaction_sequence

MINVALUE 0

START WITH (SELECT MAX(trans_seq_no)

FROM TRANSACTION_LOG)

INCREMENT BY 1

CACHE 20; // (cycle ,no cycle,maxvalue )

Insert into tgt(id) values (transaction_sequence.NEXTVAL);

Set Operators:

UNION – eliminates duplicates

SELECT location_id, department_name "Department",

TO_CHAR(NULL) "Country Office" FROM departments

UNION

SELECT location_id, TO_CHAR(NULL) "Department",

state_province FROM locations;

UNION ALL – with duplicates

SELECT region_id FROM regions

UNION ALL

SELECT region_id FROM countries

ORDER BY region_id;
Intersect - Both the queries select all the distinct rows.

Select regionid from country

Intersect

Select regionid from region

MINUS Operator:

All distinct rows selected by the first query but not the second

Select countryid from country

Minus

Select countryid from locations

What are the Drawbacks of the Indexes

Indexes increase performance of select query, they can also decrease performance of data
manipulation.

Many indexes on a table can slow down INSERTS and DELETES drastically

The more the indexes on the table, the more time inserts and delete will take.

Similarly every change to an indexed column will need a change to index.

So we need to choose the index very carefully and drop which are not in use.

Though the extra space occupied by indexes is also a consideration, it may not matter much since the
cost of data storage has declined substantially.

Oracle Database performance Tuning Techniques:

http://www.dataintegration.ninja/32-tips-for-oracle-sql-query-writing-and-performance-tuning/
Database partitioning :

https://docs.oracle.com/cd/B28359_01/server.111/b32024/partition.htm

Complex SQL Queries:

http://www.complexsql.com/complex-sql-queries-examples-with-answers/

Das könnte Ihnen auch gefallen