Peformance tuning of merge statement
HelloI have a merge statement that spans hours(10-12). V$ session_longops shows that he's working on the hash join, but the wait event is db file sequential read.
MERGE INTO art_base ab
USING x_SOURCE x
ON (ab.art_no = x.art_no AND ab.sto_no = x.sto_no AND ab.yw = x.yw)
WHEN MATCHED THEN
UPDATE
SET ab.art_qty_sale_tot = x.art_qty_sale_tot,
ab.art_qty_sale_cust = x.art_qty_sale_cust, ab.src_id = 'DW',
ab.bu_type_sto = x.bu_type_sto
WHERE ( NVL (ab.art_qty_sale_tot, 0) <>
NVL (x.art_qty_sale_tot, 0)
OR NVL (ab.art_qty_sale_cust, 0) <>
NVL (x.art_qty_sale_cust, 0)
OR NVL (ab.bu_type_sto, 0) <> NVL (x.bu_type_sto, 0)
)
WHEN NOT MATCHED THEN
INSERT (ab.art_no, ab.sto_no, ab.yw, ab.art_qty_sale_tot,
ab.art_qty_sale_cust, ab.src_id, ab.bu_type_sto)
VALUES (x.art_no, x.sto_no, x.yw, x.art_qty_sale_tot,
x.art_qty_sale_cust, 'DW', x.bu_type_sto)
The art_base table has approximately 53131553 records and the x_source of the table around 3800000 records.There is a unique index on the table art_base art_no, sto_no, yw.
the plan
------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------------
| 0 | MERGE STATEMENT | | 3896K| 620M| | 182K (1)| 00:36:25 |
| 1 | MERGE | ART_BASE | | | | | |
| 2 | VIEW | | | | | | |
|* 3 | HASH JOIN OUTER | | 3896K| 222M| 144M| 182K (1)| 00:36:25 |
| 4 | TABLE ACCESS FULL| X_SOURCE | 3896K| 100M| | 3829 (2)| 00:00:46 |
| 5 | TABLE ACCESS FULL| ART_BASE | 53M| 1672M| | 57381 (2)| 00:11:29 |
------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("ASS"."YW"(+)="X"."YW" AND "ASS"."STO_NO"(+)="X"."STO_NO" AND
"ASS"."ART_NO"(+)="X"."ART_NO")
Any suggestions on how to improve performance?Version: 10.2.0.4
Concerning
Ankit
Published by: Ankit Goel on January 11, 2011 15:47
The execution plan sounds perfect for me.
You need all THE lines of the two huge tables. There is no join faster full scan and HASH join.
Test only with
select count(*)
from art_base ab
INNER JOIN on x_SOURCE x
ON (ab.art_no = x.art_no AND ab.sto_no = x.sto_no AND ab.yw = x.yw);
This should be 'fast enough' (minutes) and have the same (similar) execution plan. Right?
If no index help for this join.
But, perhaps that it hurts for UPDATE and INSERT?
Index of how many do you have? All BITMAP index... they can kill performance for updates on these areas.
If you have need of these indexes (for other queries) to disable THEM and rebuild them after MERGER. This merger does not run faster with any clue.
V$ session_longops shows that he's working on the hash join, but the wait event is db file sequential read.
You see just LONG OPS in v$ session_longops ;-)
That will mean a COMPLETE analysis, kinds of wholesale, hash joins are LONG OPS by definition. The unique updates you do INSERT are not... you can check if the table expands revealing itself INSERTS are already works, by checking the size of the array in DBA_SEGMENTS.
-andy
Tags: Database
Similar Questions
-
Different result by using the MERGE statement
I have 2 SQL statements:
MERGE INTO PT_CQS_AGGR APC
With the HELP of PT_CQS_AGGR_TEMP Lady
WE (concat (concat(pca.from_city_id,pca.to_city_id), pca.query_timestamp_hh) = concat (concat(pcah.from_city_id,pcah.to_city_id), pcah.query_timestamp_hh))
WHEN MATCHED THEN
Setting a DAY SET pca.search_count = pca.search_count + pcah.search_count
WHEN NOT MATCHED THEN
VALUES of INSERTION (to_city_id, query_timestamp_hh, from_city_id, search_count) (pcah.from_city_id, pcah.to_city_id, pcah.query_timestamp_hh, pcah.search_count);
AND
MERGE INTO PT_CQS_AGGR APC
With the HELP of PT_CQS_AGGR_TEMP Lady
WE (pca.from_city_id = pcah.from_city_id AND pca.to_city_id = pcah.to_city_id AND pca.query_timestamp_hh = pcah.query_timestamp_hh)
WHEN MATCHED THEN
Setting a DAY SET pca.search_count = pca.search_count + pcah.search_count
WHEN NOT MATCHED THEN
VALUES of INSERTION (to_city_id, query_timestamp_hh, from_city_id, search_count) (pcah.from_city_id, pcah.to_city_id, pcah.query_timestamp_hh, pcah.search_count);
First statement correctly the merged data, but use FULL TABLE SCANS, so it's rather slow. Second statement using a UNIQUE INDEX on the columns from_city_id, to_city_id, and query_timestamp_hh on the MAP of EXPLAIN, but during the execution of I've got ORA-00001: unique constraint (PODOWNER. IDX_CQS_AGGR_3COL) violated. What is the problem with the second statement - especially with this line:
WE (pca.from_city_id = pcah.from_city_id AND pca.to_city_id = pcah.to_city_id AND pca.query_timestamp_hh = pcah.query_timestamp_hh) Can I use a more complex condition in the MERGE statement? Because it seems that not all the conditions have been taken...
???
Select pcah.from_city_id, pcah.to_city_id, PT_CQS_AGGR_TEMP Lady pcah.query_timestamp_hh
less
Select pca.from_city_id, pca.to_city_id, pca.query_timestamp_hh PT_CQS_AGGR pca
somewhere the conditions limiting the time.
(or whatever the columns make up the stress of failing)
In addition, select for each column in the constraint of failure where the column is null. If one of these columns is null, equality fails, even if both are null, because null <> null. So you will get a "no match" if all values are the same and it is set to null.
Edit: Oh, slap forehead, that is why one works and the complete analysis. As SomeoneElse said, it normalizes the type of data and concatenates, comparison null to no effect and refusing all usual index.
-
Debugging this merge statement
EMPL of MERGE IN one
USING (SELECT *)
T) b
WE (a.empno = b.empno)
WHEN MATCHED THEN
UPDATE
SET a.ename = b.ename, a.job = b.job,
a.Mgr = b.mgr, a.hiredate = b.hiredate, a.sal = b.sal,
a.comm b.com, a.deptno = b.deptno =
WHEN NOT MATCHED THEN
INSERT (a.empno, a.ename, a.job, a.mgr, a.hiredate, a.sal, a.comm,
a.DEPTNO)
VALUES (b.empno, b.ename, b.job, b.mgr, b.hiredate, b.sal, b.b.a., specialization
b.DEPTNO);
Note t is import table that has data of new employees and empl is the table that store data for all employees and it is the main table, above 2 tables are replica of scott.emp with data
Problem when I'm executing the merge statement preceding the session is the gel
Solution ?
Perhaps another session has locked some rows in the emp table?
Try
select * from empl where empno in (select empno from t_imp) for update nowait;
When you get an error such as ORA-00054, another session is locking of lines that you want to update.
Hope that helps,
dhalek
-
Get error in the merge statement
Version of Oracle, Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production
Hi gurusI am trying to perform the merge statement using the clause, but I get the error message, see below my sample data:
Examples of data
DROP TABLE OF BILLING;
DROP TABLE GROUP_MST;
DROP TABLE age_max;
CREATE ARRAY OF BILLING
(
BILLING_ID NUMBER (5),
RATE NUMBER (5),
MAXIMUM_AGE NUMBER (5),
package_id NUMBER (5)
);
INSERT
IN BILLING
(SELECT 11,1,10,100 FROM DUAL
UNION ALL
SELECT 12,2,15,100 FROM DUAL
UNION ALL
SELECT 13,3,20,100 FROM DUAL
);
CREATE TABLE GROUP_MST
(group_number NUMBER (5), package_id NUMBER (5))
);
INSERT INTO GROUP_MST
(SELECT DOUBLE 5 000 100
);
-------
CREATE TABLE AGE_MAX
(group_number NUMBER (5), max_age NUMBER (5))
);
INSERT
IN AGE_MAX
(SELECT 5000,60 FROM DUAL
);
Merge statement
MERGE INTO DRC USING billing
(WITH datum1 AS
(SELECT A.group_number,
b.billing_id,
b.rate,
b.maximum_age,
b.package_id
OF group_mst A,.
b billing
WHERE A.package_id = b.package_id
),
datum2 AS
(SELECT max_age, group_number FROM age_max
)
SELECT * FROM A, b WHERE A.group_number = b.group_number datum2 datum1
)
CBC ON (drc.billing_id = src.billing_id)
WHEN MATCHED THEN
UPDATE SET drc.maximum_age = src.max_age - 1;
Error
Error from the 33 in the command line:
MERGE INTO DRC USING billing
(WITH datum1 AS
(SELECT A.group_number,
b.billing_id,
b.rate,
b.maximum_age,
b.package_id
OF group_mst A,.
b billing
WHERE A.package_id = b.package_id
),
datum2 AS
(SELECT max_age, group_number FROM age_max
)
SELECT * FROM A, b WHERE A.group_number = b.group_number datum2 datum1
)
CBC ON (drc.billing_id = src.billing_id)
WHEN MATCHED THEN
UPDATE SET drc.maximum_age = src.max_age - 1
Error in the command line: 50 column: 1
Error report:
SQL error: ORA-00918: column ambiguously defined
00918 00000 - "ambiguously defined column.
* Cause:
* Action:
Thanks in advance
Concerning
Shu
WITH datum1 AS (SELECT A.group_number,
b.billing_id,
b.rate,
b.maximum_age,
b.package_id
OF group_mst A, b billing
WHERE A.package_id = b.package_id),
datum2 AS (SELECT max_age, group_number FROM age_max)
SELECT A.*, b.max_age, b.group_number as group_number_b - ERROR HERE
OF datum1 A, b datum2
WHERE A.group_number = b.group_number
-
MERGE statement is increment the sequence. It problem?
Hi allI use a merge statement to update thousands of data in a table. If the criteria does not match (WHEN NOT MATCHED) I wrote an insert statement that contains a reference to the sequence. And it works very well.
My question is, will this increment also sequence even though it won't insert the statement in the script of fusion. I think it will be. Right?
Say, I update 20K record will be the sequence also increment by 20 times k. If his past all work around.
Thanks in advance.
Here is a less effective solution to your problem that creates a function to get the value of the following sequence, but I agree with Justin cave that because of the cache you must lose the values.
SQL > CREATE SEQUENCE s_emp;
Order of creation.
SQL > CREATE or REPLACE FUNCTION s_emp_nextval
2 RETURN NUMBER
3 AS
4 v_nextval NUMBER;
5 BEGIN
6. SELECT s_emp.nextval
7 INTO v_nextval
8 DOUBLE;
9 v_nextval of RETURN;
10 END;
11.
The function is created.
SQL > MERGE IN emp t (USING)
2. SELECT empno, ename
3 FROM emp) s
4. WE (t.empno = s.empno)
5. WHEN MATCHED THEN
UPDATE 6
7 SET t.ename = s.ename
8 WHEN NOT MATCHED THEN
9 INSERT (empno, ename)
10 VALUES (s_emp_nextval, 'SMITH');
14 lines merged.
SQL > SELECT s_emp. NEXTVAL
2 FROM dual;
NEXTVAL
----------
1
-
Error in the Merge statement using dblink
Hello
I am facing the following error when you use the merge statement using the dblink.
ORA-02069: global_names must be set to true for this operation parameter.
I can use the same dblink in my select insert and update statements but when I try to use merge then he invites the error said, is also not any syntax error as same statement can be used on the same tables of database instead of dblink.
Please suggest any help will be much appreciated.
Thanks in advance
Hi Aqeel
If insert and update statements work well through links db, then it should not be a problem with the merge statement. But if you are faced with the question, so please check the entire sql statement with tnsnames on both sides. Please share the tnsnames for the two dbs with dblink ddl.
Concerning
Jihane Narain Sylca
-
Return in the MERGE statement clause
Hello
I use Oracle 10 g Version
I tried the code using UPDATE with return Clause & MERGER with return below Clause.
I have not found ANY error while working with the UPDATE statement. Here is the code with the UPDATE statement
DECLARE TYPE empno_list IS TABLE OF emp.empno%TYPE; vempno_list empno_list; BEGIN UPDATE emp SET comm = 11 WHERE deptno IN (SELECT deptno FROM dept) RETURNING empno BULK COLLECT INTO vempno_list; FOR i IN vempno_list.FIRST .. vempno_list.LAST LOOP DBMS_OUTPUT.put_line ('Values of EMP ' || vempno_list (i)); END LOOP; END;
But PL/SQL error: ORA-00933: SQL not correctly completed command when you work with the MERGE statement
declare type empno_list is table of emp.empno%type; vempno_list empno_list; begin merge into emp tgt using dept src on (src.deptno =tgt.deptno) when matched then update set tgt.comm=12 returning tgt.empno bulk collect into vempno_list ; for i in vempno_list.first .. vempno_list.last loop dbms_output.put_line('Values of EMP '||vempno_list(i) ) ; end loop; end;
Please suggest me
Probably because BACK IN clause does not belong to the MERGE statement. It is available only for INSERT, UPDATE, and DELETE. Here's the quote from Oracle Documentation:
The static method
RETURNING
INTO
article belongs to aDELETE
,INSERT
, orUPDATE
statement. The dynamicRETURNING
INTO
article belongs to aEXECUTE
IMMEDIATE
statement.And here is the link.
It will be useful.
Ishan
-
Hi all
I am facing a problem of failure to merge because of duplicate statement in the Source table. In a merge statement, what I understand is if the Update statement in which corresponding clause contains WHERE predicates, they are pushed upward to filter the lines of the clause?
In order to understand, I tried to frame a test case to model how my real scenario is:
create table tab_src (col1, col2 number number, number of col3, col4 varchar2 (5));
create table tab_des (col1, col2 number number, number of col3, col4 varchar2 (5));insert into tab_src values (1, 1, 1, 'AAA');
insert into tab_src values (1, 1, 0, "AAB");insert into tab_des values (1, 1, -1, null);
explain plan for
merge into tab_des d
a_l'_aide_de)
Select *.
of tab_src
where col3 is not null
) s
on (d.col1 = s.col1
and d.col2 = s.col2
)
When matched then
update the value of col4 = s.col4
where d.col3 is not null;Select *.
table (dbms_xplan.display (null, null, 'ALL'));Hash value of plan: 3751341164
--------------------------------------------------------------------------------
| ID | Operation | Name | Lines | Bytes | Cost (% CPU). Time |
--------------------------------------------------------------------------------
| 0 | MERGE STATEMENT | | 1. 60. 7 (15) | 00:00:01 |
| 1. MERGE | TAB_DES | | | | |
| 2. VIEW | | | | | |
|* 3 | HASH JOIN | | 1. 98. 7 (15) | 00:00:01 |
| 4. TABLE ACCESS FULL | TAB_DES | 1. 55. 3 (0) | 00:00:01 |
|* 5 | TABLE ACCESS FULL | TAB_SRC | 2. 86. 3 (0) | 00:00:01 |
--------------------------------------------------------------------------------
Name of the query block / Alias object (identified by the operation identity card):
-------------------------------------------------------------
1 MRG$ 1
3 SALT$ F5BB74E1
4 SALT$ F5BB74E1 / D@SEL$1
5. SALT$ F5BB74E1 / TAB_SRC@SEL$2
Information of predicates (identified by the operation identity card):
---------------------------------------------------
3 - access("D".") COL1 "=" TAB_SRC. " "" COL1 "AND
"D"." COL2 "=" TAB_SRC. " ("" COL2 ")
5 - filter ("COL3" IS NOT NULL)
Projection of the column information (identified by the operation identity card):
-----------------------------------------------------------
1 SYSDEF [4], SYSDEF [32720], SYSDEF [1], SYSDEF [76], SYSDEF [32720]
2 - « D ». "COL3" [NO.22], "S" "." " COL4 ' [VARCHAR2, 5].
3. (#keys = 2) "D". "COL1" [NO.22], "TAB_SRC" "." " COL1 '[NUMBER, 22],
"D"." COL2"[NO.22],"TAB_SRC ". ' COL2 '[NUMBER, 22],
« D ». ROWID [ROWID, 10], "D". "COL4" [VARCHAR2, 5], "D" "." " COL3 '[NUMBER, 22],
'TAB_SRC '. "COL4" [VARCHAR2, 5], 'COL3 '[NUMBER, 22] "
4 - « D ». ROWID [ROWID, 10], "D". ' COL1 '[NUMBER, 22],
"D"." COL2"[NO.22],"D ". "COL3" [NO.22], "D" "." " COL4 ' [VARCHAR2, 5].
5. "TAB_SRC". "COL1" [NO.22], "TAB_SRC" "." " COL2 '[NUMBER, 22],
'COL3' [NO.22], "TAB_SRC". "COL4" [VARCHAR2, 5].
Note
-----
-dynamic sampling used for this statement
The predicate information section made no reference to the column COL3 to the Destination table. Then it would be correct to conclude that the where predicates in the UPDATE clause will play no role in the identification of lines of data from source table/query?
I use oracle 10.2.0.5.
That's right, the where clauses are applied as the update is done, they will not be part of the condition WE which identifies the lines merge. This is why you must make sure that your condition IT only joined the lines to merge and there are no duplicates.
-
Hi all
I have a main table and a table of detail (the two are linked by a key (Param_id)).
I just need to know is possible so that we can insert a record into the child table, whenever we are inserting record to master through a Merge statement.
When matched
Update...
When not matched
Insert...
-An entry will be in the Master table, I want to know, is there a command by which I can make the entry in the child table, also
Is no excuse for a double wire display.
You have not 'right' to an answer. Respondents are volunteers who are free to choose what threads they meet.
If someone won't respond to your thread creates a new thread with the same question will not help.
And when someone mistreats forums by not following the guidelines of the forum makes it just AS likely that people will want to help you.
-
Hi Experts,
I write under the MERGE statement. In this cardinality of the table_a table_b is 1:2. That is, each record of table_b corresponds to 2 records in table_a based on columns in CLAUSE.
Well, this query throws below error.
-Error-
ORA-12801: error reported in the P011 parallel query server
ORA-30926: failed to get a stable set of rows in the source tables
However, the same statement runs successfully what tip PARALLEL is removed altogether. (There are no duplicates in table_b based on unity, group, loc columns.)
Please sugget if anything look his place here or causes possible.
-Request-
MERGE / * + PARALLEL (8) * /.
IN table_a a
With the HELP of table_b b
ON (a.unit = b.unit
AND a.group = b.group
AND a.loc = b.loc)
WHEN MATCHED
THEN UPDATE
SET a.u_pushonly = b.u_pushonly
a.u_seasonind = b.u_seasonind,
a.eff = BOX
WHEN b.u_season_start_dt <>TO_DATE('01/01/1970','DD/MM/YYYY')
OR b.u_season_end_dt <>TO_DATE('01/01/1970','DD/MM/YYYY')
OR b.u_pushonly = 1
THEN more GRAND (NVL (b.u_assortment_start_dt, TO_DATE('01/01/1970','DD/MM/YYYY'))
b.u_season_start_dt)
Of OTHER NVL (b.u_assortment_start_dt, TO_DATE('01/01/1970','DD/MM/YYYY'))
END;
kendenny wrote:
Fusion requires that columns be unique in both tables. Given that you only update was not insert is not put in correspondence, then you can use a single update statement.
I don't think so, it requires only NEW data is unique. Informal argument: a single line in the new table can update several lines in the table of old, but you can not use several lines in the new table to update a single row in the old table.
However, I wonder if something went wrong with the parallelism so that the lines with the same VALUES for the data (old) table_a has been distributed through many slaves, leading to several slaves, trying to update each on the other lines. (I don't see how this could happen, but it's one of the problems with bugs - if she should not deceive you, it can be difficult to imagine how it's bad).
To the OP - this sounds like something, you need to trigger an SR for (after a search of MOS).
Concerning
Jonathan Lewis
-
Question about the transition from string values to the Partition clause in a merge statement
Hi all
I use the code to update the data of specific secondary partition using oracle merge statements below.
I'm getting the name of the secondary partition and pass this string to the secondary partition clause.
The Merge statement is a failure, indicating that the specified secondary partition does not exist. But the partition under do exists for the table.
We use a server Oracle 11 GR 2.
Here is the code I use to fill in the data.
declare
ln_min_batchkey PLS_INTEGER;
ln_max_batchkey PLS_INTEGER;
lv_partition_name VARCHAR2 (32767).
lv_subpartition_name VARCHAR2 (32767).
Start
FOR m1 IN (SELECT (year_val + 1) AS year_val, year_val AS orig_year_val)
FROM (SELECT DISTINCT
To_char (batch_create_dt, 'YYYY') year_val
OF stores_comm_mob_sub_temp
ORDER BY 1)
ORDER BY year_val)
LOOP
lv_partition_name: =.
() scmsa_handset_mobility_data_build.fn_get_partition_name
nom_table_p = > 'STORES_COMM_MOB_SUB_INFO ',.
p_search_string = > m1.year_val);
FOR m2
IN (SELECT DISTINCT
'M' || To_char (batch_create_dt, 'MM') AS month_val
OF stores_comm_mob_sub_temp
WHERE TO_CHAR (batch_create_dt, 'YYYY') = m1.orig_year_val)
LOOP
lv_subpartition_name: =.
() scmsa_handset_mobility_data_build.fn_get_subpartition_name
nom_table_p = > 'STORES_COMM_MOB_SUB_INFO ',.
p_partition_name = > lv_partition_name,
p_search_string = > m2.month_val);
DBMS_OUTPUT. Put_line (' lv_subpartition_name = > ' | lv_subpartition_name |' and lv_partition_name = > ' | lv_partition_name);
IF lv_subpartition_name IS NULL
THEN
DBMS_OUTPUT. Put_line ("to the INTERIOR of FI = > ' |") M2.month_val);
INSERT INTO STORES_COMM_MOB_SUB_INFO (T1)
T1.ntlogin,
T1.first_name,
T1.last_name,
T1.job_title,
T1.store_id,
T1.batch_create_dt)
SELECT t2.ntlogin,
T2.first_name,
T2.last_name,
T2.job_title,
T2.store_id,
T2.batch_create_dt
OF stores_comm_mob_sub_temp t2
WHERE TO_CHAR (batch_create_dt, 'YYYY') = m1.orig_year_val
AND'M '. To_char (batch_create_dt, 'MM') =
M2.month_val;
ELSIF lv_subpartition_name IS NOT NULL
THEN
DBMS_OUTPUT. Put_line (' INSIDE ELSIF = > ' | m2.month_val);
MERGE (SELECT *)
OF stores_comm_mob_sub_info
SUBPARTITION (lv_subpartition_name)) T1
USING (SELECT *)
OF stores_comm_mob_sub_temp
WHERE TO_CHAR (batch_create_dt, 'YYYY') =
M1.orig_year_val
AND'M '. To_char (batch_create_dt, 'MM') =
M2.month_val) T2
WE (T1.store_id = T2.store_id
AND T1.ntlogin = T2.ntlogin)
WHEN MATCHED
THEN
GAME UPDATE
T1.postpaid_totalqty =
(NVL (t1.postpaid_totalqty, 0))
(+ NVL (t2.postpaid_totalqty, 0));
T1.sales_transaction_dt =
LARGEST)
NVL (t1.sales_transaction_dt,
T2.sales_transaction_dt),
NVL (t2.sales_transaction_dt,
T1.sales_transaction_dt)),
T1.batch_create_dt =
LARGEST)
NVL (t1.batch_create_dt, t2.batch_create_dt),
NVL (t2.batch_create_dt, t1.batch_create_dt))
WHEN NOT MATCHED
THEN
INSERT (t1.ntlogin,
T1.first_name,
T1.last_name,
T1.job_title,
T1.store_id,
T1.batch_create_dt)
VALUES (t2.ntlogin,
T2.first_name,
T2.last_name,
T2.job_title,
T2.store_id,
T2.batch_create_dt);
END IF;
END LOOP;
END LOOP;
COMMIT;
end;
/
Really appreciate your input here.
Thank you
MK.Hello
You can use "immediate execution" what works.
Thank you
-
Question about the MERGE statement
Hi all
I have a small question... I can post code, table creation, etc... but I think this question can be able to be simply answered, if I explain it. If you want me to zip codes, etc... then just ask and I'll do it.
Here it goes:
I have a set up merge statement, and I have two columns. One is named ADD_DT, and the other is called LST_UPD_DT.
Update in the merge statement, the LST_UPD_DT column is updated with the SYSDATE.
When it is inserted in the merge statement, the ADD_DT column is updated with the SYSDATE.
So, here is my question:
Regardless of whether or not the information is updated, the LST_UPD_DT updates the SYSDATE every time. Any advice on how to update the LST_UPD_DT column only if information is changed/updated, so that the information is not changed would be not updated by the SYSDATE? I hope that this question was not too complicated.
Thank you all and if you want to see the code or examples of table... Please ask and I'll post one. I just thought that this may be able to meet without it.Hello
991769 wrote:
OK, here's what the table looks like:Thank you.
CREATE TABLE TBL_SUPPLIERS ( SUPP_CD VARCHAR2(40 CHAR), SUPP_NM VARCHAR2(255 CHAR), ADD_DT VARCHAR2(50 CHAR), LST_UPD_DT VARCHAR2(50 CHAR))
Do not use a VARCHAR2 column for date information. Use a DATE column.
CREATE TABLE D_SUPPLIERS ( SUPP_CD VARCHAR2(40 CHAR), SUPP_NM VARCHAR2(255 CHAR))
INSERT INTO TBL_SUPPLIERS VALUES (1234, MINING, '03-26-2013 3:33:49 PM', '04-02-2013 9:04:52 AM'); INSERT INTO TBL_SUPPLIERS VALUES (2345, ELECTRIC, '03-26-2013 3:33:49 PM', '04-02-2013 9:04:52 AM'); INSERT INTO TBL_SUPPLIERS VALUES (3456, COAL, '03-26-2013 3:33:49 PM', '04-02-2013 9:04:52 AM');
You want people to view the solutions that actually work? Please make sure that your own instructions will actually work. Test and fix your code before posting.
All INSERT statements you posted fail; You seem to be missing single quotes around string literals. I think you meant:INSERT INTO TBL_SUPPLIERS VALUES (1234, 'MINING', '03-26-2013 3:33:49 PM', '04-02-2013 9:04:52 AM'); ...
Here's one way:
MERGE INTO tbl_suppliers sm USING ( SELECT d.* FROM d_suppliers d LEFT OUTER JOIN tbl_suppliers t ON t.supp_cd = d.supp_cd WHERE t.supp_cd IS NULL OR DECODE (t.supp_nm, d.supp_nm, 0, 1) = 1 ) ds ON (sm.supp_cd = ds.supp_cd) WHEN MATCHED THEN UPDATE SET sm.supp_nm = ds.supp_nm, sm.lst_upd_dt = SYSDATE WHEN NOT MATCHED THEN INSERT (sm.supp_cd, sm.supp_nm, sm.add_dt) VALUES (ds.supp_cd, ds.supp_nm, SYSDATE); SELECT * FROM tbl_suppliers ORDER BY SUPP_CD;
This can be more efficient, because it does not generate redo, or triggers, when two tables have exactly the same line.
-
Merge statement: update only when a difference
Hello
I have two tables have almost the same columns, how can I use the merge statement to update the table target only when there is difference between the source and target table. Is there an easier way to do not compare every column one by one? I use Oracle 11.2.
Here's the MERGE statement:
Merge into trgt tb_trgt using tb_src src
on (src.id = trgt.id)
When not matched then values of insertion (trgt.id, trgt.nm, trgt.addr) (src.id, src.nm, src.nm)
when matched, then update set trgt.nm = src.nm, trgt.addr = src.addr
where trgt.nm <>src.nm or trgt.addr <>src.addr
;
Is there an easier way to clarify the clause in the NO MATCHED? I don't want to compare every column in the tables. Because I might have a lot of columns in tables.
Thank you939569 wrote:
I have two tables have almost the same columns, how can I use the merge statement to update the table target only when there is difference between the source and target table. Is there an easier way to do not compare every column one by one? I use Oracle 11.2.
Is there an easier way to clarify the clause in the NO MATCHED? I don't want to compare every column in the tables. Because I might have a lot of columns in tables.I use the method of Tom Kyte to compare tables; It gives me the differences between the source and the target using GROUP BY, who manages the value NULL comparisons. Can I use this result in the MERGER. Here is a step by step illustration. First, set up test data:
define num_rows = 10 / define pct = 20 / define value_length = 50 / define num_mods = round((&num_rows/3)*(&pct/100),0) / DROP TABLE T_TARGET; / DROP TABLE T_SOURCE; / create table t_target(key1 number, value1 varchar2(&value_length), constraint pk_target primary key(key1)); insert /*+ append */ into t_target select level+&num_mods*3, rpad('DO NOTHING - same in source and target ',&value_length, '*') from dual where level <= &num_rows-&num_mods*2 connect by level <= &num_rows-&num_mods*2; / create table t_source as select * from t_target; / insert into t_source select level, rpad('INSERT - in source, not in target ',&value_length, '*') from dual where level <= &num_mods connect by level <= &num_mods; / insert into t_target select level+&num_mods, rpad('DELETE (after update) - not in source, in target ',&value_length, '*') from dual where level <= &num_mods connect by level <= &num_mods; / insert into t_source select level+&num_mods*2, rpad('UPDATE - put this in target ',&value_length, '*') from dual where level <= &num_mods connect by level <= &num_mods; / insert into t_target select level+&num_mods*2, rpad('UPDATE - update this from source ',&value_length, '*') from dual where level <= &num_mods connect by level <= &num_mods; / commit; / select 't_target', count(*), value1 from t_target group by 't_target', value1 union all select 't_source', count(*), value1 from t_source group by 't_source', value1; / 'T_TARGET' COUNT(*) VALUE1 ---------- ---------- -------------------------------------------------- t_target 1 UPDATE - update this from source ***************** t_target 8 DO NOTHING - same in source and target *********** t_target 1 DELETE (after update) - not in source, in target * t_source 1 UPDATE - put this in target ********************** t_source 8 DO NOTHING - same in source and target *********** t_source 1 INSERT - in source, not in target ****************
So, I need to do an insert, update, and a delete.
Now I'll build code comparison step by step. I don't show the results every time, but if you run each query yourself can see what is happening.
-- Step by step build of "refresh by merge" USING ROWIDs -- Result: with cardinality hint, can use "BY USER ROWID" and avoid 3d full scan -- 1) Full scan of both tables to get data, -- identify old / new records and get old ROWID select KEY1,VALUE1, 1 old_cnt, 0 new_cnt, rowid rid from T_TARGET o UNION ALL SELECT KEY1,VALUE1, 0 old_cnt, 1 new_cnt, NULL FROM T_SOURCE n / -- 2) GROUP BY compares records, identical records have old_cnt = new_cnt select KEY1,VALUE1, sum(old_cnt) old_cnt, sum(new_cnt) new_cnt, max(rid) rid FROM ( select KEY1,VALUE1, 1 old_cnt, 0 new_cnt, rowid rid from T_TARGET o UNION ALL SELECT KEY1,VALUE1, 0 old_cnt, 1 new_cnt, NULL FROM T_SOURCE n ) group by KEY1,VALUE1 / -- 3) Filter out identical records select KEY1,VALUE1, sum(old_cnt) old_cnt, sum(new_cnt) new_cnt, max(rid) rid FROM ( select KEY1,VALUE1, 1 old_cnt, 0 new_cnt, rowid rid from T_TARGET o UNION ALL SELECT KEY1,VALUE1, 0 old_cnt, 1 new_cnt, NULL FROM T_SOURCE n ) group by KEY1,VALUE1 having sum(old_cnt) <> sum(new_cnt) / -- 4) for INSERT, keep NEW; for DELETE, keep OLD; -- for UPDATE, keep NEW values and OLD rid SELECT /*+ cardinality(1) */ KEY1,VALUE1, old_cnt, new_cnt, row_number() OVER(PARTITION BY KEY1 ORDER BY old_cnt) rn , max(rid) over(partition by key1) rid from ( select KEY1,VALUE1, sum(old_cnt) old_cnt, sum(new_cnt) new_cnt, max(rid) rid FROM ( select KEY1,VALUE1, 1 old_cnt, 0 new_cnt, rowid rid from T_TARGET o UNION ALL SELECT KEY1,VALUE1, 0 old_cnt, 1 new_cnt, NULL FROM T_SOURCE n ) group by KEY1,VALUE1 having sum(old_cnt) <> sum(new_cnt) ) / -- 5) Filter out OLD UPDATE record, not needed SELECT KEY1,VALUE1, new_cnt, rid FROM ( SELECT /*+ cardinality(1) */ KEY1,VALUE1, old_cnt, new_cnt, row_number() OVER(PARTITION BY KEY1 ORDER BY old_cnt) rn , max(rid) over(partition by key1) rid from ( select KEY1,VALUE1, sum(old_cnt) old_cnt, sum(new_cnt) new_cnt, max(rid) rid FROM ( select KEY1,VALUE1, 1 old_cnt, 0 new_cnt, rowid rid from T_TARGET o UNION ALL SELECT KEY1,VALUE1, 0 old_cnt, 1 new_cnt, NULL FROM T_SOURCE n ) group by KEY1,VALUE1 having sum(old_cnt) <> sum(new_cnt) ) ) where rn = 1 / KEY1 VALUE1 NEW_CNT RID 1 INSERT - in source, not in target **************** 1 2 DELETE (after update) - not in source, in target * 0 AAAcDHAAMAAAACtAAA 3 UPDATE - put this in target ********************** 1 AAAcDHAAMAAAACtAAB
Now, here's the real MERGER:
merge into T_TARGET o USING ( SELECT KEY1,VALUE1, new_cnt, rid FROM ( SELECT /*+ cardinality(1) */ KEY1,VALUE1, old_cnt, new_cnt, row_number() OVER(PARTITION BY KEY1 ORDER BY old_cnt) rn , max(rid) over(partition by key1) rid from ( select KEY1,VALUE1, sum(old_cnt) old_cnt, sum(new_cnt) new_cnt, max(rid) rid FROM ( select KEY1,VALUE1, 1 old_cnt, 0 new_cnt, rowid rid from T_TARGET o UNION ALL SELECT KEY1,VALUE1, 0 old_cnt, 1 new_cnt, NULL FROM T_SOURCE n ) group by KEY1,VALUE1 having sum(old_cnt) <> sum(new_cnt) ) ) where rn = 1 ) n on (o.rowid = n.rid) when matched then update set VALUE1=n.VALUE1 DELETE WHERE (n.new_cnt = 0) WHEN NOT MATCHED THEN INSERT ( KEY1,VALUE1 ) VALUES ( n.KEY1,n.VALUE1 ) /
Published by: stew Ashton on February 7, 2013 20:42
-
problem compiling with MERGE statement
What is the problem with this merge statement?
When I compile I get the message
met symbol IN when one of the following expected
*:= . (@ %; < a SQL > *)
MERGE INTO PF_REP_TEMP HAS
WITH THE HELP OF PF_REP_SKS B
WE (a. pf_userid = "FINP' AND B.pfbatch = 'F99' and B.pfstfno = A.pf_staffno
and B.pfcc = "7" and B.pftransmy = A.mmyy and B.slno = A.s_no)
When matched then
game update
a.pfwdrlm = b.pfwdrlm,
a.PFRC = b.PFRC;
Help, please
(version of the form is 10g, 11g database version)How to perform a merge into forms button pressed trigger command
-
ORA 22813 in the merge statement
Hi the gems... Good afternoon...
My version of the database is 11.2.0.1.0 Solaris 64-bit OS.
I am facing a ' ORA-22813: value of the operand exceeds the limits of the system "during the execution of a procedure.
I used loggers and found that it is getting failed in a MERGE statement.
This merge statement is used to merge a table with a collection. the code is as below:
Function GET_BALANCE_HIST (V_MERGE_REC) parameter is an array type.MERGE /*+ INDEX(P BALANCE_HISTORIC_INDEX) */ INTO BALANCE_HOLD_HISTORIC P USING TABLE(GET_BALANCE_HIST(V_MERGE_REC)) M ON (P.CUSTOMER_ID = M.CUSTOMER_ID AND P.BOOK_ID = M.BOOK_ID AND P.PRODUCT_ID = M.PRODUCT_ID AND P.SUB_BOOK_ID = M.SUB_BOOK_ID AND) WHEN MATCHED THEN UPDATE <set .....> WHEN NOT MATCHED THEN INSERT<.....>
Now GET_BALANCE_HIST (V_MERGE_REC) is a function in the pipeline and we used that because the V_MERGE_REC collection can become huge with data.
This process worked very well since the beginning, but since yesterday, it was constantly throwing 22813 ORA error in this line.
Help, please... Thanks in advance...Gogol wrote:
The code flow is as below:There is a sql query, the output is a set of some several lakes of records. Now we take the result of the sql query in a collection with a limit of 1000 (FETCH cur_sql COLLECT LOOSE v_balance_rec LIMIT 1000). Then we do a lot of processing, the calculation of this collection.
After the treatment and calculation, we are filling another collection this collection transformed (V_BALANCE_REC) (V_MERGE_REC). Thus, if the loop iterates for 1000 times, then the V_MERGE_REC is filled with records of 1000 * 500 = 500000 processed.
And then we go to this huge V_MERGE_REC collection as a parameter to this function.
Don't know what are the Lakes. Please use International English.
Why can't do you math in a SQL query?
If it's really too hard to do in SQL (SQL queries can perform a treatment fairly complex), then I would look to Scripture
results of a table and use it.
Maybe you are looking for
-
HP pavilion n248sa-15 drivers Win7
(SorryI have a European accent) Hello, I bought this laptop with a Windows 8.1, but now I have windows 7, but can not find, if somene is going to help me where I can download driver I will be very greatfull. I tried HP but no results, just usb driver
-
Nokia BH-214 and stereo headset BH-214
Unable to connect to the Nokia BH-214 stereo headset.
-
My laptop auto fills all the boxes that requires any entry with the entry several times of number 1. When I try to log on to my laptop, the password box automatically fills the password with 1 and keeps repeating. This also happens with Web pages, e-
-
The value of static output on PFI on USB 6366
Hi all What I want to achieve is to save dedicated 8 DIO lines on dev USB6366 on port 0 for some specific application and use remaining 16 PFI lines control some external equipment. All I need is the ability to set some PFI line high/low within a rea
-
can't OPEN MY AOL E-MAIL; WHAT APPEARS IS A TITLE OF THE DECLARATION; AUTHENTICATION FAILURE PROBLEM.