update t_join_situation set join_state = '1' where year='2011'If related tables are involved, and the field values of one table are updated to fields in another table, the above update method is used, and the statement is usually written as follows: update table a set a. field 1 = (select b. field 1 from table b where a.field2=b.field2) where exists(select 1 from table b where a.field2=b.field2), as shown in the following SQL:
update t_join_situation a set a.join_state=(select b.join_state from t_people_info b where a.people_number=b.people_number and a.year='2011' and a.city_number='M00000' and a.town_number='M51000') where exists (select 1 from t_people_info b where a.people_number=b.people_number and a.year='2011' and a.city_number='M00000' and a.town_number='M51000')However, the efficiency of such an update is very low.
Here, several ways to optimize multi-table association update are recommended:
(1) Inline view update method
The inline view update method is to update a temporarily created view. Such as:update (select a.join_state as join_state_a,b.join_state as join_state_b from t_join_situation a, t_people_info b where a.people_number = b.people_number and a.year = '2011' and a.city_number='M00000'and a.town_number='M51000') set join_state_a = join_state_bIn parentheses, a view is established by associating two tables, and the updated fields are set in the set. This solution is more intuitive and faster to execute than writing. However, the primary key of table B must be in the where condition, and the table to be updated must be associated with "=", otherwise an error will be reported:
(2) merge update method merge is an oracle-specific statement, the syntax is as follows:
MERGE INTO table_name alias1 USING (table|view|sub_query) alias2 ON (join condition) WHEN MATCHED THEN UPDATE table_name SET col1 = col_val1, col2 = col2_val WHEN NOT MATCHED THEN INSERT (column_list) VALUES (column_values);E.g:
MERGE INTO T_SUPERVISE_REPORT T USING (SELECT A.UUID, A.SUPERVISETYPE, A.IMGID, A.LASTVALUE FROM T_SUPERVISE_REPORT_TMP A) S ON (T.UUID = S.UUID AND T.SUPERVISETYPE = S.SUPERVISETYPE AND T.IMGID = S.IMGID) WHEN MATCHED THEN UPDATE SET T..LAST_VALUE= S.LASTVALUE;Its principle is to compare the data selected in alias2 with the ON (join condition) of alias1. If it matches, perform an update operation (Update), and if it does not match, perform an insert operation (Insert). Doing a merge does not return the number of rows affected. The Merge statement is cumbersome and can only be associated with two tables at most. For complex statements, the merge update method will be ineffective and inefficient.
(3) Fast cursor update method
The syntax is like:begin for cr in (query statement) loop -- loop --Update statement (based on the result set from the query) end loop; --end the loop end;
Oracle supports fast cursors, and the cursor is directly written to the for loop without definition, which makes it convenient for us to update data in batches. Coupled with oracle's rowid physical field (oracle defaults to each table has a rowid field, and is a unique index), you can quickly locate the record to be updated.
Examples are as follows:begin for cr in (select a.rowid,b.join_state from t_join_situation a,t_people_info b where a.people_number=b.people_number and a.year='2011' and a.city_number='M00000' and a.town_number='M51000') loop update t_join_situation set join_state=cr.join_state where rowid = cr.rowid; end loop; end;
The advantages of using fast cursors are many. It can support complex query statements and update accurately. No matter how large the data is, the update efficiency is still high, but the number of affected rows will not be returned after execution.
in conclusion:
plan |
suggestion |
Standard update syntax |
It is better to use this scheme for single table updates or simpler statements. |
inline view update method |
If the two tables are related and the updated table is related by the primary key of the related table, this scheme is better. |
merge update method |
If the two tables are related and the updated table is not related by the primary key of the related table, it is better to adopt this scheme. |
Fast cursor update method |
If multiple tables are associated and the logic is complex, this solution is better. |
Reference: Baidu [Five Ways of Oracle's Update]