Trong bài viết này chúng ta sẽ thử nghiệm việc di chuyển bảng và kiểm tra xem dung lượng yêu cầu để thực hiện việc di chuyển bảng này là bao nhiêu.
Để tính toán dung lượng, chúng ta sẽ sử dụng cách tạo ra một tablespace rất nhỏ và đặt thuộc tính autoextend. Khi đó chúng ta thực hiện câu lệnh, tablespace sẽ được tự động mở rộng tới kích thước yêu cầu để thực hiện câu lệnh ALTER TABLE MOVE.
Ví dụ 1: ALTER TABLE MOVE
Chúng ta sẽ load 1 table khoảng 1300MB vào và di chuyển nó theo phương thức offline.
SQL> create tablespace DEMO
2 datafile 'X:\ORACLE\ORADATA\DB19\PDB1\DEMO.DBF' size 10m autoextend on next 1m;
Tablespace created.
SQL>
SQL> create table t tablespace demo as
2 select d.* from dba_objects d,
3 ( select 1 from dual connect by level <= 100 );
Table created.
SQL>
SQL> select bytes/1024/1024
2 from user_segments
3 where segment_name = 'T';
BYTES/1024/1024
---------------
1344
SQL>
SQL> select bytes/1024/1024 from dba_data_files
2 where tablespace_name = 'DEMO';
BYTES/1024/1024
---------------
1346
SQL>
SQL> alter table t move;
Table altered.
SQL>
SQL> select bytes/1024/1024 from dba_data_files
2 where tablespace_name = 'DEMO';
BYTES/1024/1024
---------------
2747
SQL>
SQL> select bytes/1024/1024
2 from user_segments
3 where segment_name = 'T';
BYTES/1024/1024
---------------
1344
SQL>
Như chúng ta có thể thấy, table chiếm khoảng khoảng 1344MB. Khi lệnh ALTER TABLE MOVE hoàn thành, tablespace chiếm dung lượng khoảng 2747MB. Vì vậy lệnh ALTER TABLE MOVE sẽ yêu cầu dung lượng khoảng 1353MB ~ với dung lượng ban đầu của bảng để hoàn thành.
Ví dụ 2: ALTER TABLE MOVE COMPRESS
Chúng ta vẫn sử dụng bảng như ví dụ 1, nhưng hãy thử xem tới cuối tablespace sẽ tăng thêm bao nhiêu:
SQL> create tablespace DEMO
2 datafile 'X:\ORACLE\ORADATA\DB19\PDB1\DEMO.DBF' size 10m autoextend on next 1m;
Tablespace created.
SQL>
SQL> create table t tablespace demo as
2 select d.* from dba_objects d,
3 ( select 1 from dual connect by level <= 100 );
Table created.
SQL>
SQL> select bytes/1024/1024
2 from user_segments
3 where segment_name = 'T';
BYTES/1024/1024
---------------
1344
SQL>
SQL> select bytes/1024/1024 from dba_data_files
2 where tablespace_name = 'DEMO';
BYTES/1024/1024
---------------
1346
SQL>
SQL> alter table t move compress;
Table altered.
SQL>
SQL> select bytes/1024/1024 from dba_data_files
2 where tablespace_name = 'DEMO';
BYTES/1024/1024
---------------
1883
SQL>
SQL> select bytes/1024/1024
2 from user_segments
3 where segment_name = 'T';
BYTES/1024/1024
---------------
408
Tổng dung lượng của tablespace sau khi hoàn thành lệnh là 1883MB, vậy việc thực hiện lệnh chỉ tốn hơn 400MB, ít hơn nhiều phải không?
Ví dụ 3: ALTER TABLE MOVE ONLINE
Chúng ta cũng thực hiện tương tự như vậy và hãy xem kết quả
SQL> create tablespace DEMO
2 datafile 'X:\ORACLE\ORADATA\DB19\PDB1\DEMO.DBF' size 10m autoextend on next 1m;
Tablespace created.
SQL>
SQL> create table t tablespace demo as
2 select d.* from dba_objects d,
3 ( select 1 from dual connect by level <= 100 );
Table created.
SQL>
SQL> select bytes/1024/1024
2 from user_segments
3 where segment_name = 'T';
BYTES/1024/1024
---------------
1344
SQL>
SQL> select bytes/1024/1024 from dba_data_files
2 where tablespace_name = 'DEMO';
BYTES/1024/1024
---------------
1346
SQL>
SQL> alter table t move online;
Table altered.
SQL>
SQL> select bytes/1024/1024 from dba_data_files
2 where tablespace_name = 'DEMO';
BYTES/1024/1024
---------------
3347
SQL>
SQL> select bytes/1024/1024
2 from user_segments
3 where segment_name = 'T';
BYTES/1024/1024
---------------
1344
Tổng dung lượng tablespace yêu cầu là 3347MB, như vậy câu lệnh ALTER TABLE MOVE ONLINE sẽ yêu cầu khoảng 2000MB dung lượng, cao nhất trong tất cả các ví dụ trên.
Ví dụ 4: ALTER TABLE MOVE COMPRESS ONLINE
Bạn có thể đoán được, câu lệnh này sẽ yêu cầu dung lượng nhỏ hơn câu lệnh ALTER TABLE MOVE ONLINE
SQL> create tablespace DEMO
2 datafile 'X:\ORACLE\ORADATA\DB19\PDB1\DEMO.DBF' size 10m autoextend on next 1m;
Tablespace created.
SQL>
SQL> create table t tablespace demo as
2 select d.* from dba_objects d,
3 ( select 1 from dual connect by level <= 100 );
Table created.
SQL>
SQL> select bytes/1024/1024
2 from user_segments
3 where segment_name = 'T';
BYTES/1024/1024
---------------
1344
SQL>
SQL> select bytes/1024/1024 from dba_data_files
2 where tablespace_name = 'DEMO';
BYTES/1024/1024
---------------
1346
SQL>
SQL> alter table t move compress online;
Table altered.
SQL>
SQL> select bytes/1024/1024 from dba_data_files
2 where tablespace_name = 'DEMO';
BYTES/1024/1024
---------------
2420
SQL>
SQL> select bytes/1024/1024
2 from user_segments
3 where segment_name = 'T';
BYTES/1024/1024
---------------
408
Dung lượng yêu cầu khoảng 1000MB để hoàn thành câu lệnh.
Trên đây là 4 ví dụ sử dụng câu lệnh ALTER TABLE MOVE, dựa vào đây bạn hãy tính toán ra kích thước yêu cầu trước khi thực hiện câu lệnh này nhé.
Leave a Review