SQL> create table t as select * from dba_objects; Table created SQL> create index t_ind on t(object_id); Index created
SQL> exec dbms_stats.gather_table_stats(user,'T',cascade => true); PL/SQL procedure successfully completed
SQL> select count(*) from t; COUNT(*) ---------- 50805 SQL> select a.segment_name,a.segment_type,a.BYTES from user_segments a where a.segment_name in ('T','T_IND'); SEGMENT_NAME SEGMENT_TYPE BYTES ------------- ------------------ ---------- T TABLE 6291456 T_IND INDEX 983040 --让我们来顺便计算一下该表中的索引和表大小的百分比 SQL> select trunc((983040/6291456)*100) as "ind_pct(%)" from dual; ind_pct(%) ---------- 15
比如说我们最终估计这个表保存的数据大概在10亿条,我们就可以算出这张表所需空间大小。
SQL> select ((6291456+983040)/50805*1000000000)/1024/1024/1024 as "T表所需空间大小(G)" from dual; T表所需空间大小(G) ------------------ 133.351094257455
真实环境下面有product这张表,最终估计商品总数应该不会超过100w条。
1)收集统计信息
SQL> exec dbms_stats.gather_table_stats(user,tabname => 'PRODUCT',cascade => true); PL/SQL procedure successfully completed
SQL> select index_name,index_type,table_name from user_indexes where table_name ='PRODUCT'; INDEX_NAME INDEX_TYPE TABLE_NAME ------------------------------ --------------------------- ------------------------------ PRODUCT_ID NORMAL PRODUCT SYS_IL0000054761C00013$ LOB PRODUCT VISICOUNT2 NORMAL PRODUCT PRODUCT_STATUS NORMAL PRODUCT SID_INDEX2 NORMAL PRODUCT PRODUCT_NAME2 NORMAL PRODUCT CREATE_TIME2 NORMAL PRODUCT CREATE_SID2 NORMAL PRODUCT SYS_IL0000054761C00118$ LOB PRODUCT 9 rows selected
select a.segment_name, a.segment_type, a.BYTES from user_segments a where a.segment_name in (select INDEX_NAME from user_indexes where table_name = 'PRODUCT') or a.segment_name = 'PRODUCT'; SEGMENT_NAME SEGMENT_TYPE BYTES -------------------------------------------------------------------------------- ------------------ ---------- PRODUCT TABLE 5242880 SYS_IL0000054761C00013$ LOBINDEX 65536 PRODUCT_ID INDEX 196608 CREATE_SID2 INDEX 458752 CREATE_TIME2 INDEX 327680 PRODUCT_NAME2 INDEX 524288 SID_INDEX2 INDEX 196608 PRODUCT_STATUS INDEX 196608 VISICOUNT2 INDEX 131072 SYS_IL0000054761C00118$ LOBINDEX 65536 10 rows selected
select sum(a.BYTES) from user_segments a where a.segment_name in (select INDEX_NAME from user_indexes where table_name = 'PRODUCT') or a.segment_name = 'PRODUCT'; SUM(A.BYTES) ------------ 7405568
SQL> select count(*) from product; COUNT(*) ---------- 2572
假如有100w条数据算出大小
SQL> select ((7405568/2572)*1000000)/1024/1024/1024 as "PRODUCT表所需空间(G)" from dual; PRODUCT表所需空间(G) -------------------- 2.68156013073484