Managing Tables
Objectives
Storing User Data
- Regular tables
- Partitioned tables
- Index-organized tables
- Clustered tables
普通表
分區表 : 如果一個表太大了,那么就會在這個大表的基礎上,在分為子表,叫做 partition. 每個 row 是存儲在一起的,比如 有1000行數據,可能 前 500 rows 存儲在一個 partition 里邊,后 500 rows 存儲在一個 partition里。每個分區是一個 segment, 并且可以存儲在不同的tablespace中。
Index-organized tables : 這個“索引表” 不同于以往意義的索引,它是將存儲的數據本身按照某種順序進行了存儲,所以查詢效率很高。with a primary key index on one or more of its columns. an index-organized table maintains a single B- tree containing the primary key of the table and other column values.
集群 : Clustered tables , 很多表是有相關性的,比如 Join 多個表,那么,可以將多個表存儲為集群,這樣在查詢時會提高效率。
Clusters have a cluster key, which is used to identify the rows that need to be stored together.
cluster key can consist of one or more columns.
Tables in a cluster have columns that correspond to the cluster key.
The cluster key is independent of the primary key.
Data Type
99% 用的是 built-in 類型, 99% 使用 Scalar
數字型,文本型,日期型,二進制( LOB )
盡量用 varchar2 , 不要使用 char,? 除非是固定格式,比如性別,只有 男,女
如果 oracle 內部表的列數超過 254列,oracle 會將一行 分成幾塊來存儲。會加重 I/O 負荷。
索引是依附于表的。
對于任何一張表都有 ROWID,隱含的。
select ROWID, ID, NAME from employee;
ROWID 的內容, 可以看到 ROWID 可以快速定位一條記錄的位置。
Table Structure
90% 一行放在一個 database block 中。( 一個 database block 中還可以放行 )
列存儲的順序通常就是你定義的列的順序,并且如果某列的值是 NULL, 那么是不會存儲該列的。
Row data is stored in database blocks as variable-length records.?columns for a row are generally stored in the order in which they are defined and any trailing NULL are not stored. Note: A single byte for column length is required for non trailing NULL columns.
Each row in a table may have a different number of columns. Each row in a table has :
row header : 這行有多少列,還有鏈接信息,還有鎖的情況。row lock
row data : 存儲列的長度,還有列的值。列的值是緊挨著列的長度信息。 for each column, the oracle server stores the column length and value ( one byte is needed to store the column length if the column cannot exceed 250 bytes. A column that can be longer needs three length bytes. The column value is stored immediately following the column length bytes.)
CREATE TABLE
該命令比較復雜。類似準備創建數據庫一樣,有個樣本比較好。
創建 table 中的存儲信息段
The STORAGE clause specifies storage characteristics for the table. 其中 :
Storage 信息STORAGE( INITAL 200k NEXT 200K -- 初始 extent 大小 PCTINCREASE 0 -- 如果需要,下一個extent 大小 MINEXTENTS 1 -- 最少被分配的 extent 數量 MAXEXTENTS 5) -- 最多被分配的 extent 數量 TABLESPACE data ; -- 在哪個 tablespace -- our db create table storage STORAGE (INITIAL 64KMINEXTENTS 1MAXEXTENTS 2147483645PCTINCREASE 0FREELISTS 1FREELIST GROUPS 1BUFFER_POOL DEFAULT) View Code個人感覺創建 table 的時候, 不用特意指定storage, 因為在 tablespace 中已經指定了, 只要將他放在對應的tablespace中就可以了
我的感覺是對的.
The STORAGE clause specifies storage characteristics for the table. The storage allocated for the first extent is 200K. When a second extent is required it will be created at 200K also defined by the NEXT value. When a third extent is required, it will be created at 200K because the PCTINCREASE has been set to 0. The maximum amount of extents that can be used is set at 5. with the minimum set to 1.
PCTFREE 這個參數比較重要 ( 預留的空間,為了將來如果發生 update 等事務時,這個 block 還有空間可以使用, 如果這個值 ( 百分比)分配太小,那么當 update時,如果空間不夠,就會發生將整個 block 搬家的事件,如果分配的空間太大,那么又是一種浪費 )
推薦 自動管理。
INITRANS : 控制同時能夠操作 那個塊的 transaction. Specifies the initial number of transaction entries allowcated within each data block allocated to the table. 范圍 1 ~ 255 , 默認是 1. INITRANS ensures that a minimum number of concurrent transaction can update the block. In general , this value should not be changed from its default.
創建表的原則:
總的原則:減少資源競爭
- Place tables in separate tablespaces.
- Use locally-managed tablespaces to avoid fragmentation(碎片化).
- Use few standard extent sizes for tables to reduce tablespace fragmentation.( 標準的 extent 就只有1種 )
創建臨時表 temporary tables
Temporary tables can be created to hold session-private data that exists only for the duration of a transaction or session.
查詢結果太大時,臨時將結果緩存在磁盤上。
CREATE GLOBAL TEMPORARY TABLE
臨時表的數據,只在一個事物或 session 中有效。
對數據不需要做 DML 加鎖
也可以創建 索引,視圖,觸發器。
只有當前的 session 可以看到相應信息,visable, 即便是兩個 session 使用的同一個臨時表,互相也看不到。
一個用戶不可能阻止別的用戶訪問臨時表。即使 lock 臨時表也無法阻止別的用戶使用該表。
臨時表也會產生 undo 和 redo 信息,但是很少。基本可以忽略。
用戶的臨時表是放在 自己臨時表空間中,A 和 B 都用一個臨時表,但是這個臨時表存儲在 2 個臨時表空間中。
臨時表只是定義了一個表的摸板,但是這個表并不存在,并沒有產生真正的表空間。(沒有分配磁盤), 這是與實際表的區別。
只是在運行時刻,才臨時分配了一個 segment.
即 user1 用的時候,這個臨時表,分配了一個空間, temp1
當 user2 用的時候,這個臨時表,又分配了 temp2, 所以,雖然是一個gloable temporary table, 但其實是2張表。
on commit delete rows to specify that rows are only visible within the transaction
on commit preserve rows to specify that rows are visibile for the entire session
session 有效 : 例子
create global temporary table tmp_session on commit preserve rows
as select * from t where 1=0 ;
transaction 有效 : 例子
create global temporary table tmp_session on commit delete rows
as select * from t where 1=0 ;
提交事物后,session 級別的還會有數據存在,而 事物級別的數據就不存在了。
當該用戶退出后,session 斷了,那么 session 級別的數據也就沒有了。
Manage storage structure in a table
分析表 可以 得到 row size, 在性能調優提到。
Migration 移動 , 帶來的后果就是 oracle 的性能下降。 I/O 要翻一翻。
應該盡量避免? Migration
Row chaining
如果一個條記錄中有太多列,或者列太長,oracle 就會把它分成不同的部分,每個部分是 row piece, 每塊里都包含指針,然后連接成一個完整記錄。
也同樣加重了? oracle 讀取的負擔。
檢測的 Migration, Row chaining , oracle 中有個包,可以提供分析的功能。dbms_metadata 包
修改的內容,只能作用于新的 block.
限制 : The value of INITIAL cannot be modified for a table.
?????????? The value of NEXT specified will be rounded to a value that is multiple of the block size greater than or equal to the value specified.
以下情況,需要使用手工分配 額外空間。
可能需要對表的重新組織,類似磁盤碎片整理。
將數據 移動到另外的 segment中,保存 index, constraints, privilege 等信息。
Is being used to move a table to a different tablespace or reorganize extents
After moving a table you will have to rebuild the indexes to avoid some error.
truncate table
前一章 有介紹過 高水位的問題,這點與 delete? from table; 不一樣。DDL
Drop table
?
Drop column
?
不常使用,8i版本都沒有這個命令
Dropping a column can be time consuming and require a large amount of undo space. While dropping columns from large tables, checkpoints can be specified to minimize the use of undo space.
Using the UNUSED Option
可以設置 column 為 unused
ALTER TABLE hr.employees? SET UNUSED COLUMN 列名 CASCADE CONSTRAINTS
ALTER TABLE hr.employees DROP UNUSED COLUMNS CHECKPOINT 1000;
當你要刪除 2 列時,如果是直接刪除,那么所有的行都會被折騰 2 次,而如果先設置成 UNUSED, 設置完 這兩列之后,再刪除這兩列,那么所有的行就只會被折騰 1 次。因為 drop 是 drop unused column , 所以,一次性將所有的 unused 全部刪除。
Obtaining Table Information
DBA_TABLES
DBA_OBJECTS
Rename column
ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;
盡量不改,改名字很麻煩( 不改, 就當做不知道這個命令好了)
轉載于:https://www.cnblogs.com/moveofgod/archive/2013/01/08/2843631.html
總結
以上是生活随笔為你收集整理的Managing Tables的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深度学习与机器视觉
- 下一篇: 自己写的sqlHelper 以及读取配置