Oracle 10g大表批量更新优化
racle 10g大表批量更新优化,其实,700万的表不算大表,作为测试够了 一,t表信息 SQL alter table t add is_del number(1); SQL alter table t modify is_del default 0; SQL desc t Name Type Nullable Default Comments ------ --------- -------- -------
racle 10g大表批量更新优化,其实,700万的表不算大表,作为测试够了一,t表信息
SQL> alter table t add is_del number(1);
SQL> alter table t modify is_del default 0;
SQL> desc t
Name Type Nullable Default Comments
------ --------- -------- ------- --------
ID NUMBER Y
CODE NUMBER Y
IS_DEL NUMBER(1) Y 0
SQL> select count(*) from t;
COUNT(*)
----------
7136976
二,为了比较基准的一致性,先缓存t数据
update t set t.is_del = 0;
三,这里共总结了4种方法
SQL> set timing on
--0
SQL> update t set t.is_del = 0;
7136976 rows updated.
Elapsed: 00:08:28.64
--1
SQL> declare
2 rnt pls_integer := 0;
3 begin
4 for idx in (select rowid rid from t) loop
5 update t set t.is_del = 0 where rowid = idx.rid;
6 rnt := rnt + 1;
7 if mod(rnt,2000) = 0 then
8 commit;
9 end if;
10 end loop;
11 commit;
12 end;
13 /
PL/SQL procedure successfully completed.
Elapsed: 00:09:41.32
SQL>
--2
SQL> declare
2 rnt pls_integer := 0;
3 begin
4 for idx in (select rowid rid from t) loop
5 update t set t.is_del = 0 where rowid = idx.rid;
6 rnt := rnt + 1;
7 if rnt = 2000 then
8 rnt := 0;
9 commit;
10 end if;
11 end loop;
12 commit;
13 end;
14 /
PL/SQL procedure successfully completed.
Elapsed: 00:09:35.67
--3
SQL> declare
2 cursor cur_t is select rowid rid from t;
3 type tab_t is table of urowid index by binary_integer;
4 l_rid tab_t;
5 begin
6 open cur_t;
7 loop
8 fetch cur_t bulk collect into l_rid limit 2000;
9 forall idx in 1 .. l_rid.count
10 update t set t.is_del = 0 where rowid = l_rid(idx);
11 commit;
12 exit when cur_t%notfound;
13 end loop;
14 close cur_t;
15 end;
16 /
PL/SQL procedure successfully completed.
Elapsed: 00:06:48.84
通过上面的测试结果可以看到,方法3最好,方法0不建议使用,这会使undo快速增长,出现ora-01555错误。方法1和方法2在一些书籍上看到过测试,说方法2优于方法1,但我这次测试效果不明显,以后再进行一些测试。
注:
测试的数据库配置了闪回特性,db_recovery_file_dest_size=2g,归档日志放在db_recovery_file_dest目录中。开始时的更新操作,redo增长很快,常常hang住了,alert log报空间不足,所有增加了db_recovery_file_dest_size=4g。还有要注意undo表空间的监控。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

Oracle Invalid numeric errors may be caused by data type mismatch, numeric overflow, data conversion errors, or data corruption. Troubleshooting steps include checking data types, detecting digital overflows, checking data conversions, checking data corruption, and exploring other possible solutions such as configuring the NLS_NUMERIC_CHARACTERS parameter and enabling data verification logging.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

To create a user in Oracle, follow these steps: Create a new user using the CREATE USER statement. Grant the necessary permissions using the GRANT statement. Optional: Use the RESOURCE statement to set the quota. Configure other options such as default roles and temporary tablespaces.
