Example
Create a tablespace under the Sys user. The creation statement is as follows:
Create tablespace devbase
Datafile '/home/ora/tbspace/devbase_20090320.dbf' size 512 m
Autoextend on
Next 100 m
Maxsize 1024 m;In the preceding statement, a file of MB is first submitted, and the initial space is MB.
View the table space information. You can use the following statement to view the table space devbase information:
Select * From dba_tablespaces A where a. tablespace_name = 'devbase ';
View the information of the tablespace file. The SQL statement is as follows. The field bytes = 536870912 Bytes/1024/1024 = m indicates the file size.
Select * From dba_data_files A where a. tablespace_name = 'devbase ';
However, the devbase information does not exist in dba_extents and dba_segments, because no objects have been created on the tablespace.
Modify the tablespace file. The statement is as follows:
Alter database datafile '/home/ora/tbspace/devbase_20090320.dbf' resize 1024 m;
After the successful operation, the records of the three views dba_tablespaces, dba_extents, and dba_segments remain unchanged.
Exercise caution when deleting data files. First back up the data in the tablespace to another tablespace, and then delete the tablespace together with the data file.
Create a table on the tablespace and create an index:
Create Table staff_info
(
Staff_id number (10 ),
Staff_name varchar2 (20 ),
ADDR varchar2 (200)
)
Tablespace devbase;
Create index idx_staff_info_pid on staff_info (staff_id) tablespace devbase;
Two segments are generated, with the table and index names respectively. The statement is as follows:
Select * From dba_segments A where a. tablespace_name = 'devbase ';
Two segments are generated and allocated space. The statement is as follows:
Select * From dba_extents A where a. tablespace_name = 'devbase ';
Note that the sum of bytes in the segment and segment information is equal.
Insert records into the table to affect the information of segments and segments. Execute the following script PLSQL:
Begin
For I in 200000
Loop
Insert into staff_info (staff_id, staff_name, ADDR)
Values (I, 'name', 'address ');
End loop;
Commit;
End;
0.2 million records are inserted into the table empty.
View the segment information of the devbase table space. Because there are two objects, there are two records:
Select * From dba_segments A where a. tablespace_name = 'devbase ';
We found that bytes, and blocks have increased a lot. bytes is the number of allocated bytes, blocks is the number of allocated blocks, and blocks is multiplied by the block size to be equal to the bytes size. The extents tables are 20 and 19 respectively, which means that creating an index is also very space-consuming.
View the segment information of the tablespace devbase:
Select * From dba_extents A where a. tablespace_name = 'devbase ';
We can see that a lot of segment information is added, with 39 records, which is exactly equal to the sum of extents in the segment information; there are two file_id, that is, the partition information is created on both data files; the sum of bytes is equal to the bytes in the segment information.
The available storage space of a tablespace is the total size of all data files.
When creating a tablespace above, we use the field extension option "autoextend On". When the tablespace is not enough, the project will be selected based on the expansion to automatically expand the size of the data file; however, the option of automatic file extension has some problems in management. In this way, some database files are automatically extended too much during database operation, which may affect Io performance; there are a lot of objects and data on it, which is not conducive to backup and daily maintenance. Therefore, when creating data files, do not open the automatic expansion attribute. You should create some data files of the proper size, when the tablespace is not enough, add data files to meet the requirements.
Next we will delete the data in the staff_info table.
Delete from staff_info;
Commit;
View the information in dba_segments and dba_extents. The records are not changed. After the data is deleted, the data dictionary is not modified.
Run the PLSQL script again:
Begin
For I in 200001 .. 400000
Loop
Insert into staff_info (staff_id, staff_name, ADDR)
Values (I, 'name', 'address ');
End loop;
Commit;
End;
Check the information in dba_segments and dba_extents, or 39 segments, indicating that the space just released has been used.
Next, we will delete the data in the staff_info table again:
Truncate table staff_info;
View the information in dba_segments and dba_extents. The same as when creating a table and an index, the tuncate space is recycled.
In this way, after the system runs for a long time, the insert, update, and delete operations may leave fragments. In general, moving is used to remove objects from one tablespace to another. For some business systems, regular maintenance is required to eliminate fragments and improve system performance. I will not perform tests here.