我在Oracle Linux Server版本6.3上使用postgresql 9.2.1。表和索引的預期存儲大小
我正在爲表和索引獲取預期的存儲大小。
感謝來自這個網站的一些意見,我做了我fomula像下面的表...
- 表的情況下...
postgres=# \d test
Table "public.test"
Column | Type | Modifiers
---------------+-----------------------+-----------
c1 | integer | not null
c2 | character varying(20) | not null
c3 | character varying(8) | not null
c4 | character varying(6) | not null
c5 | character varying(15) |
c6 | character varying(20) |
c7 | character varying(20) |
c8 | character varying(20) |
Indexes:
"idx_test" PRIMARY KEY, btree (c1, c3, c4, c5)
Tablespace: "test"
postgres=# insert into test values(1,
'11111111111111111111', -- 20(exactly same with max length of each column)
'11111111', -- 8
'111111', -- 6
'111111111111111', -- 15
'11111111111111111111', -- 20
'11111111111111111111', -- 20
'11111111111111111111');-- 20
INSERT 0 1
postgres=# select * from pgstattuple('test');
table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent
-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+--------------
8192 | 1 | 81 | 0.99 | 0 | 0 | 0 | 8072 | 98.54
postgres=# insert into test values(2,
'11111111111111111111', -- 20(exactly same with max length of each column)
'11111111', -- 8
'111111', -- 6
'111111111111111', -- 15
'11111111111111111111', -- 20
'11111111111111111111', -- 20
'11111111111111111111');-- 20
INSERT 0 1
postgres=# select * from pgstattuple('test');
table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent
-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+--------------
8192 | 2 | 162 | 1.98 | 0 | 0 | 0 | 7980 | 97.41
所以,我想通了,每個頁面可以容納88(X)個元組。
- 元組大小在實際存儲:8072(第一插入之後FREE_SPACE)-7980(第二插入之後FREE_SPACE)= 92
- 頁面默認= 8192 - 8072(第一插入之後FREE_SPACE) - 92(元組大小) = 28
- 9182 - 28 = 92 * X(最大元組中的每個頁編號)
在索引的情況下,...
postgres=# \d test_pkey
Index "public.idx_test"
Column | Type | Definition
---------------+-----------------------+---------------
c1 | integer | c1
c2 | character varying(20) | c2
c3 | character varying(8) | c3
c4 | character varying(6) | c4
primary key, btree, for table "public.test"
postgres=# truncate table test;
postgres=# vacuum;
postgres=# analyze;
postgres=# insert into test values(1,
'11111111111111111111', -- 20(exactly same with max length of each column)
'11111111', -- 8
'111111', -- 6
'111111111111111', -- 15
'11111111111111111111', -- 20
'11111111111111111111', -- 20
'11111111111111111111');-- 20
INSERT 0 1
postgres=# select * from pgstattuple('idx_test');
table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent
-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+--------------
16384 | 1 | 56 | 0.34 | 0 | 0 | 0 | 8088 | 49.37
(1 row)
postgres=# select * from pgstatindex('idx_test');
version | tree_level | index_size | root_block_no | internal_pages | leaf_pages | empty_pages | deleted_pages | avg_leaf_density | leaf_fragmentation
---------+------------+------------+---------------+----------------+------------+-------------+---------------+------------------+--------------------
2 | 0 | 8192 | 1 | 0 | 1 | 0 | 0 | 0.79 | 0
(1 row)
postgres=# insert into test values(1,
'11111111111111111111', -- 20(exactly same with max length of each column)
'11111111', -- 8
'111111', -- 6
'111111111111111', -- 15
'11111111111111111111', -- 20
'11111111111111111111', -- 20
'11111111111111111111');-- 20
INSERT 0 1
postgres=# select * from pgstattuple('idx_test');
table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent
-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+--------------
16384 | 2 | 112 | 0.68 | 0 | 0 | 0 | 8028 | 49
(1 row)
postgres=# select * from pgstatindex('idx_test');
version | tree_level | index_size | root_block_no | internal_pages | leaf_pages | empty_pages | deleted_pages | avg_leaf_density | leaf_fragmentation
---------+------------+------------+---------------+----------------+------------+-------------+---------------+------------------+--------------------
2 | 0 | 8192 | 1 | 0 | 1 | 0 | 0 | 1.52 | 0
(1 row)
再一次,我發現每個頁面可以容納135(Y)個元組。
- 元組大小在實際存儲:8088(第一插入之後FREE_SPACE)-8028(第二插入之後FREE_SPACE)= 60
- 頁面默認= 8192 - 8088(第一插入之後FREE_SPACE) - 60(元組大小) = 44
- 8192 - 44 = 60 * Y(在每個頁面元組的最大數量)
當我在表中插入1350行....我得到這個...
postgres=# select * from pgstattuple('test');
table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent
-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+--------------
131072 | 1350 | 109350 | 83.43 | 0 | 0 | 0 | 6424 | 4.9
(1 row)
postgres=# select * from pgstattuple('idx_test');
table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent
-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+--------------
90112 | 1350 | 54000 | 59.93 | 0 | 0 | 0 | 13580 | 15.07
(1 row)
postgres=# select * from pgstatindex('idx_test');
version | tree_level | index_size | root_block_no | internal_pages | leaf_pages | empty_pages | deleted_pages | avg_leaf_density | leaf_fragmentation
---------+------------+------------+---------------+----------------+------------+-------------+---------------+------------------+--------------------
2 | 1 | 81920 | 3 | 0 | 9 | 0 | 0 | 81.49 | 0
(1 row)
表的文件大小?
1350(行數)/ 88(X)= 15.34 - >這意味着需要16頁,這意味着文件大小爲16 * 8192 = 131072.它看起來是正確的。
然而,索引SISE是不同...
1350(行數)/ 135(Y)= 10 ...準確10 * 8192需要文件大小,但90112.
插入1更多的行,除此之外,index_size應該擴展(如果我是正確的),所以我試過但沒有改變。
postgres=# insert into test values(27108,'sanjuk1052','20121022','233338','172,20,30,177','win7','IE','9,0');
INSERT 0 1
postgres=# select * from pgstattuple('test');
table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent
-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+--------------
131072 | 1351 | 109431 | 83.49 | 0 | 0 | 0 | 6332 | 4.83
(1 row)
postgres=# select * from pgstattuple('idx_test');
table_len | tuple_count | tuple_len | tuple_percent | dead_tuple_count | dead_tuple_len | dead_tuple_percent | free_space | free_percent
-----------+-------------+-----------+---------------+------------------+----------------+--------------------+------------+--------------
90112 | 1351 | 54040 | 59.97 | 0 | 0 | 0 | 13536 | 15.02
(1 row)
postgres=# select * from pgstatindex('idx_test');
version | tree_level | index_size | root_block_no | internal_pages | leaf_pages | empty_pages | deleted_pages | avg_leaf_density | leaf_fragmentation
---------+------------+------------+---------------+----------------+------------+-------------+---------------+------------------+--------------------
2 | 1 | 81920 | 3 | 0 | 9 | 0 | 0 | 81.55 | 0
(1 row)
我什至不知道這種做法是合理的,但這項工作必須完成,即使是並不完美......
我需要我自己fomula尤其是用於獲取索引大小的預期,包括額外的文件被TOAST ...
任何意見將不勝感激。