Oracle principle: high water mark, PCTFREE, PCTUSED, index-organized table, cluster table, temporary table

table of Contents

Types of tables in 1.11g:

2. High water mark HWM, (High Water Mark)

3. PCTFREE and PCTUSED;

4. Move, shrink, truncate to lower the high water mark

5.IOT table, cluster table, temporary table


Types of tables in 1.11g:

Ordinary tables, partition tables, index-organized tables IOT, cluster tables, temporary tables, nested tables, object tables, etc.

Partition table: In order to increase the amount of data and query speed, a table is divided into multiple areas. Cluster table: to make the joint query faster. It is suitable for tables that are frequently associated with queries. Nested table: There are tables in the table. For example, a certain row and certain column of table A is the data of table B. The containment relationship like a "box" is called a nested table.

2. High water mark HWM, (High Water Mark)

The high water mark refers to the historical maximum number of used data blocks in the table. The high water mark is generally not lowered. Unless rebuild is used to rebuild, truncated and shrunk shrink will refresh the high water mark. 

  When scanning a full table, it starts from the 0th data block in the table, and scans one data block to the high water mark. In other words, if there is no data in the table, the data block location of the high water mark will also be scanned.

At the beginning of the table creation, the high-water mark is located at the position of data block 0, and then data is inserted continuously, and the high-water mark rises accordingly, and then the data is deleted. The high-water mark remains unchanged, as shown in the figure above.

3. PCTFREE and PCTUSED;

The default size of a data block is 8KB, and PCTFREE (default 10%) specifies the minimum percentage of free memory allowed for inserting data in a data block. PCTUSED (default 40%) specifies the percentage of memory that allows re-insertion of data in a data block, in %. When inserting data into the table, the data block will not be full, because this free memory is used to prepare for modifying the data. Add the insert data block to fill the data block, and then update the data content. Fortunately, the content decreases. If the content increases, the current data block does not have available memory to expand the data. The data block consists of 3 parts: Header, Free Space, and DATA; PCTFREE controls the minimum value of Free Space. If it exceeds the minimum value, it is not allowed to be inserted and deleted. PCTUSED controls the minimum value of DATA, if it is exceeded, it will resume allowing insertion. It is not that PCTFREE has set a 20% space area and it will always be greater than 20%, but that at least 20% of the free area is reserved for Oracle data expansion in the future. A data block can put multiple rows of data in the table.

 As shown in the figure above. When a data block is enabled, the usage rate of the data block starts to increase from 0%, and the unused rate starts to decrease from 100%. If PCTFREE is set to 20%, PCTUSED is set to 40%. Since the usage rate of the data block at the beginning is less than PCTUSED, it is allowed to insert data into the data block until the unused rate reaches 20% in FreeSpace and it is not allowed to insert data into this data block. I want to go to this data again. To insert data in a block, it is necessary to reduce the usage rate of the data block to PCTUSED 40% before it is allowed.

If the data still exceeds the total size of the data block, a row migration method will be used : Oracle will retain the Row-Header on the original data block, followed by a C++ pointer to migrate the data to the new one. On the data block, let the pointer point to the data.

The data block size is 8KB by default. If the size of a data row exceeds the maximum available data block size in the memory, row linking will occur : put a piece of data into different data blocks, and associate the same piece of data with a pointer. .

 

If ASSM is specified on the table space, only PCTFREE can be specified when creating the table. ASSM, automatic segment space management. MSSM, manual segment space management.

Check whether segment space management: select s.TABLESPACE_NAME,s.SEGMENT_SPACE_MANAGEMENT from dba_tablespaces s;

Table space expansion: alter table voapd.t1 allocate extent(datafile'D:\ORACLE\ORADATA\ORCL\TESTTBS.DBF' size 1m);

4. Move, shrink, truncate to lower the high water mark

  The move statement moves the data in the table to another table space, and at the same time clears table space fragments and lowers the high water mark:

  alter table [表名] move [tablespace USERS];

In order to understand the high water mark, first create a table t2:

------之前创建了表空间 TESTTBS;
create table t2 tablespace TESTTBS as select * from dba_objects; --count(*)=72092

How many data blocks in the table can be analyzed later

analyze table t2 compute statistics for table;
select table_name,blocks,num_rows from user_tables where table_name='T2';

Then delete 30,000 rows of data in the t2 data table:

delete t2 where rownum < 30000;
commit;
analyze table t2 compute statistics for table;
select table_name,blocks,num_rows from user_tables where table_name='T2';

It can be found that there are 30,000 fewer data rows, but the occupied data blocks have not changed.

Use move to adjust the high water mark of the table: 

alter table t2 move tablespace USERS;
analyze table t2 compute statistics for table;
select table_name,blocks,num_rows from user_tables where table_name='T2';

It can be seen that the high water mark in the T2 table has been refreshed. But when using move, the table cannot have other applications, and the index on the table must be re-created.

 

Shrink the table with the Shrink statement, and move the data block of the data row to another data block. This is the data shrinking. After the shrinking, the high water mark is lowered. DML operation (addition, deletion and modification) can be performed when shrinking, and DML operation is not allowed when the high water mark is lowered. The premise of using Shrink statement is: ASSM automatic segment management is enabled in the table space where the table is located, and ROW MOVEMENT is also enabled on the table

select s.TABLESPACE_NAME,s.SEGMENT_SPACE_MANAGEMENT from dba_tablespaces s
select table_name,u.ROW_MOVEMENT from user_tables u where table_name='T2';
alter table t2 enable row movement;
alter table t2 shrink space;
analyze table t2 compute statistics for table;
select table_name,blocks,num_rows from user_tables where table_name='T2';
---

truncate table [table name]: Keep the table structure and delete all table data, which cannot be rolled back. This operation can also minimize HWM.

 

When the amount of data is too much, the speed of deleting the table structure will be very slow: you can first set the changed column to unused, and then delete the unused column, so that the operation of deleting the column will be automatically completed when the database is idle.

alter table [表名] set unused column [列名];      alter table t2 drop unused columns;

5.IOT table, cluster table, temporary table

IOT index-organized table: This table must have a primary key, and it will be automatically sorted according to the primary key. It is an ordered table. Unlike ordinary tables, ordinary tables and indexes on the primary key of the table need to set aside storage space for them, and IOT does not have the space overhead of the primary key. The index (primary key) is stored together; the data is stored in the index block; so if the data is often accessed through the primary key, the IOT table is more suitable;

create table student_iot(
 sno int,
 sname varchar2(20),
 sage int , 
 constraints pk_student primary key(sno)
)
organization index    
pctthreshold 30 overflow tablespace users; --指定阈值30%,当一行记录太大超出了数据索引块的30%,其他列(sname 、sage)就会放到指定的表空间里users

To delete the overflow table of the index table, first delete the index table, and then empty the recycle bin, command: purge recyclebin;

 

Cluster table: Two interrelated table data are placed in one data block at the same time. In this way, only one data block can be scanned for associative query.

The steps to create a cluster table are as follows: the data type of the cluster must be consistent with the data type of the table field.

----1.创建簇-----
create cluster cluster1(ckey int);
----2.创建表时关联簇---------
create table student_cluster(
 sno int ,
 sname varchar2(20),
 sage int )
 cluster cluster1(sno);
----3.创建另一张表时关联簇---------
create table record_cluster(
 sno int ,
 record int )
 cluster cluster1(sno);
----4.簇上要建立索引-------
 create index index1 on cluster cluster1;


select * from user_clusters;  --查询簇的信息
 select * from user_clu_columns;  --查询簇的关联信息

----------5删除簇要先把簇表删除-----------
 drop table student_cluster;
 drop table record_cluster;
 drop cluster cluster1;

Temporary table: The creation of a temporary table must be in the temporary table space TEMP. The data of the temporary table can only be seen in the same session, and the data is not shared by different sessions. The data in the temporary table of each session is completely independent;

The syntax for creating a temporary table is as follows:

create global temporary table student_temp(
 sno int ,
 sname varchar2(20),
 sage int ) 
 --on commit delete rows --(默认值);
 on commit preserve rows;

Among them, on commit delete rows; specifies that the temporary table data is automatically cleared when things are submitted or rolled back; on commit Preserve rows specifies that the data in the temporary table will only be deleted when the Session is disconnected from the database;

It can be seen from the figure that the temporary tables in the two sessions are completely independent and do not interfere with each other, even though the users are the same. Specify the temporary table on commit preserve rows;, the data in it will not be emptied because of commit. However, when the user disconnects, the data in the temporary table will be deleted.

The premise of dropping the temporary table is that no data is allowed in the table in all sessions that use the temporary table.

When using on commit delete rows to create a temporary table, once the data is submitted, the data in the table is emptied

Query temporary table information:

 select u.TABLE_NAME as 表名,
 u.TEMPORARY as 是否是临时表,
 u.DURATION as 是否是事务型的建表方式
  from user_tables u  where  u.TABLE_NAME='STUDENT_TEMP';

 

Guess you like

Origin blog.csdn.net/superSmart_Dong/article/details/104504199