GBase8s索引的空间计算

GBase8s索引的空间计算

  1. GBase 8s B+索引
    GBase 8s 中采用了 B+树索引结构,该索引的内部存储结构如图所示,一个页中除了页头尾信息占用的空间28B,中间可以存储多个Index entry(索引项)。例如:根据以下 SQL 语句建立索引:create index idx_name on tabname(c1,c2,c3),表中的一行记录对应的索引值(c1 、c2、c3的值)即一个 Index entry。

  2. 索引空间计算
    2.1 一个叶子节点存放 Index entry 数量估算
    公式1:一个数据页可存放index entry的剩余空间大小page_free_size=pagesize-28Byte=2020 Byte
    一个 2K 的 page,pagesize=2048Byte
    28 是 page 的头尾信息占用的总空间
    公式2:每个index entry大小
    index_entry_size=keysize *propunique+ 4 +N
    其中 4 为 Slot 的大小,为指向数据行的指针大小
    在非分片表中,N=5,若是分片表,则 N=9
    propunique 为索引唯一性系数。对于唯一索引,propunique=1,如果索引可重复,那么 propunique=索引不同值数量/行记录,比如表有 10000 行记录,索引不同值的个数为 4000,那么 propunique=4000/10000=0.25,如果比值小于 0.01,那么取值 0.01
    公式3:一个数据页index entry 的数量
    page_indexs=trunk(page_free_size FILLFACTOR/index_entry_size)
    FILLFACTOR 为索引的填充因子,默认值是 90%,
    trunk 是向下取整函数
    2.2 叶子节点数
    索引叶子节点的个数:leaf_nodes= ceiling(rows/ page_indexs)
    其中 ceiling 是向上取整函数。 rows:表的总记录数(行数)
    2.3 非叶子节点数
    node_ents= trunk(page_free_size
    FILLFACTOR /(keysize+4)+4)
    第 2 层非叶子节点数:branches0=ceiling(leaf_nodes/ node_ents)
    第 3 层非叶子节点数:branches1=ceiling(branches0/ node_ents)
    第 N+1 层非叶子节点数:branchesN+1=ceiling(branchesN/ node_ents)

2.4 层数估算
根据非叶子节点数的计算公式,当某层非叶子节点数 branchesN<=1 时,N+2 即索引的层数。
2.5 索引空间计算
索引占用空间大小的计算公式是:
(leaf_nodes+ branches0+ branches1+…+ branchesN+1)* page size
3. 索引空间计算示例1
假设按如下 SQL 语句创建表 customer 以及一个唯一索引 ix_cust,已知该表记录数为20000000 行,表空间pagesize为2k,估算索引空间大小:
CREATE TABLE customer (customer_num integer,company_id integer, customer_name char(18),phone_num char(12)) extent size 1024 next size 2048;
CREATE unique INDEX idx_cust ON customer (customer_num) ;
idx_cust 空间的估算如下:
1)每个 Index 项占用的空间为:
index_entry_size=keysizepropunique+4+N=41+4+5=13 Byte
(索引列customer_num为integer,integer在数据库中占用4个字节,故keysize=4)
2)一个数据页包含的 index 项为:
page_indexs= trunk(page_free_size FILLFACTOR/index_entry_size)
=trunk(2020
0.9/13)=139
3)叶子节点数为:
leaf_nodes= ceiling(rows/ page_indexs)
=ceiling(20000000/139) =143885
4)非叶子节点数为:
node_ents= trunk(page_free_sizeFILLFACTOR /(keysize+4)+4)
=trunk(2020
0.9/(4 +4)+4) =231
branches0=ceiling(leaf_nodes/ node_ents) = ceiling(143885/231) =623
branches1=ceiling(branches0/ node_ents) = ceiling(623/231) =3
branches2=ceiling(branches1/ node_ents) = ceiling(3/231) =1
5)索引层次为:4
6)索引总使用的空间为:
Used_space=(leaf_nodes+ branches0+ branches1+branches2)*pagesize
=(143885+623+3+1)2K =1445122K =282.25M
通过如上步骤可以计算出索引 ix_cust 总共有 4 层,一共使用了 144 512 个数据页,总
共占用的空间为 282.25M。
7)索引实际使用空间为:
可使用 dbaccess sysmaster 查看:
Select st.dbsname databasename,dt.tabname,di.idxname,sd.name dbs_name,
di.levels,sin.ti_npused npused, format_units(sin.ti_npused,sd.pagesize)
used_size
from sysmaster:systabnames st, sysmaster:sysdbspaces
sd,sysmaster:systabinfo sin, testdb:sysindexes di,testdb:systables dt
where sd.dbsnum = trunc(st.partnum/1048576)
and dt.tabid>99
and di.idxname = st.tabname
and dt.tabid=di.tabid
and st.partnum=sin.ti_partnum
and st.dbsname=‘testdb’
and dt.tabname=‘customer’ ;
查询结果如下:
databasename tabname idxname
dbs_name levels npused
used_size
testdb
customer ix_cust
bigdatadbs 4
144889
283 MB

  1. 索引空间计算示例2
    假设按如下 SQL 语句创建表 bmsql_stock 以及一个唯一索引 ix_cust,已知该表记录数为50000000 行,datadbs表空间pagesize为4k,估算索引空间大小:
    create table bmsql_stock(s_w_id integer not null ,s_i_id integer not null ,s_quantity integer,s_ytd integer,s_order_cnt integer,s_remote_cnt integer,s_data varchar(50),s_dist_01 char(24),s_dist_02 char(24),s_dist_03 char(24),s_dist_04 char(24),s_dist_05 char(24),s_dist_06 char(24),s_dist_07 char(24),s_dist_08 char(24),s_dist_09 char(24),s_dist_10 char(24)) in datadbs extent size 16 next size 16 lock mode row;
    create unique index bmsql_stock_pkey on “gbasedbt”.bmsql_stock (s_w_id,s_i_id) using btree in datadbs;
    bmsql_stock_pkey 空间的估算如下:
    1)每个 Index 项占用的空间为:
    index_entry_size=keysizepropunique+4+N=81+4+5=17 Byte
    (索引列s_w_id,s_i_id为integer,integer在数据库中占用4个字节,故keysize=8)
    2)一个数据页包含的 index 项为:
    page_indexs= trunk(page_free_size FILLFACTOR/index_entry_size)
    =trunk(4068
    0.9/17)=215
    3)叶子节点数为:
    leaf_nodes= ceiling(rows/ page_indexs)
    =ceiling(50000000/215) =232559
    4)非叶子节点数为:
    node_ents= trunk(page_free_sizeFILLFACTOR /(keysize+4)+4)
    =trunk(4068
    0.9/(8 +4)+4) =309
    branches0=ceiling(leaf_nodes/ node_ents) = ceiling(232559/309) =753
    branches1=ceiling(branches0/ node_ents) = ceiling(753/309) =3
    branches2=ceiling(branches1/ node_ents) = ceiling(3/309) =1
    5)索引层次为:4
    6)索引总使用的空间为:
    Used_space=(leaf_nodes+ branches0+ branches1+branches2)*pagesize
    =(232559+753+3+1)4K =2333164K =911.40M
    通过如上步骤可以计算出索引 bmsql_stock_pkey 总共有 4 层,一共使用了 233316 个数据页,总共占用的空间为 911.40M。
    7)索引实际使用空间为:
    可使用 dbaccess sysmaster 查看
    select st.dbsname databasename,dt.tabname,di.idxname,sd.name dbs_name,
    di.levels,sin.ti_npused npused, format_units(sin.ti_npused,sd.pagesize)
    used_size
    from sysmaster:systabnames st, sysmaster:sysdbspaces sd,sysmaster:systabinfo sin,benchmarksql:sysindexes di,benchmarksql:systables dt
    where sd.dbsnum = trunc(st.partnum/1048576)
    and dt.tabid>99
    and di.idxname = st.tabname
    and dt.tabid=di.tabid
    and st.partnum=sin.ti_partnum
    and st.dbsname=‘benchmarksql’
    and dt.tabname=‘bmsql_stock’ ;
    sql执行结果如下:
    databasename benchmarksql
    tabname bmsql_stock
    idxname bmsql_stock_pkey
    dbs_name datadbs
    levels 4
    npused 233655
    used_size 913 MB

官网:www.gbase.cn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值