2008年7月19日星期六

Sybase ASE15的分区

Sybase ASE15的分区

1.为什么要使用分区?
假定有一大表如订单表,包含数年上千万条的记录,我们知道访问和维护这样的大表都是比较耗费资源的。有些使用数据库有经验的用户,可能会在数据库物理设计阶段想到每年(或月)产生一张表来减轻大表的压力;除此之外使用分区也是一个很好的选择。使用分区我们就可以做到比如2005的订单存放在磁盘1,2006的订单存放在磁盘2,2007的订单存放在磁盘3……。一般我们常做的都是OLTP的增删改,只对当年数据操作,如当年是2007我们就可以在很多层面上对磁盘3做些性能优化的工作来提高速度;如果我们要做DSS的大表全表扫描或进行近三年的销售订单变化趋势分析,通过一定的配置系统可以把一个查询任务分为三个或更多个并行的任务,每个任务访问一个磁盘,以此达到更快的完成查询;此外我们还可以在对大表进行维护时通过使用分区仅维护活动分区信息如更新活动分区统计值等来改善大表维护性能。

2.使用ASE15分区的前提条件是什么?
条件1.获得允许使用分区的许可并安装在资产管理器(SYSAM-2_0)中
条件2.用isql等连接ASE中,用如下方式设置允许使用分区
1> sp_configure "enable semantic partitioning",1
2> go
……
3.如何修改number of open partitions分区数配置参数?
ASE使用 "number of open partitions" 参数来控制允许最多的分区数,注意该参数配置愈大将占用更多的内存。
1> sp_configure "number of open partitions",800
2> go
4.初始时如何计算合适的分区数?
在系统闲时,执行如下命令可得到所应该配置的分区数
1> sp_countmetadata "open partitions"
2> go
There are 647 user partitions in all database(s), requiring 617 Kbytes of
memory. The 'open partitions' configuration parameter is currently set to 500.

5.使用过程中如何监控分区数是否合适?
在系统高峰时段或大量建表删除表和分区后使用如下命令监控分区数
1> sp_monitorconfig "open partition"
2> go
……

Name Num_free Num_active Pct_act Max_Used Num_Reuse
------------------------- ----------- ----------- ------- ----------- -----------
number of open partitions 443 357 44.63 397 0

在“Max_Used”基础上再加10%的开销既是应该配置的合适分区数
397 * 1.1 = 436.7 à437
1> sp_configure "number of open partitions",437
2> go

6.如何估计分区数所占用的内存或一定内存所能配置的分区数?
1> sp_helpconfig "number of open partitions","1000"
2> go
……
Configuration parameter, 'number of open partitions', will consume 953K of memory if configured at 1000.
Changing the value of 'number of open partitions' to '1000' increases the amount of memory ASE uses by 474 K.

1> sp_helpconfig "number of open partitions","10M"
2> go
……
Configuration parameter, 'number of open partitions', can be configured to 10736 to fit in 10M of memory.

7.如何对表分区?(举例)
l哈希分区
create table lineitem
( l_orderkey int not null,
l_partkey char(10) not null,
l_suppkey char(4) not null,
l_linenumber int not null,
l_quantity int not null)
partition by hash (l_orderkey, l_linenumber)
(litem_hash1 on seg1,
litem_hash2 on seg2,
litem_hash3 on seg3)

l列表分区
create table customers
(cust_id char(10) not null,
cust_name varchar(30) not null,
state char(2) not null,
phone char(10) not null)
partition by list (state)
(west values ('CA', 'OR', 'WA') on seg1,
east values ('NY', 'NJ') on seg2)

l范围分区
create table sales
(cust_id char(10) not null,
ord_id char(10) not null,
salesdate date not null,
salespsn int not null)
partition by range (salesdate)
(q1 values <= ('3/31/2005') on seg1,
q2 values <= ('6/30/2005') on seg2,
q3 values <= ('9/30/2005') on seg3,
q4 values <= (MAX) on seg4)

l轮循分区
create table office_table
(office_id char(3) not null,
office_name varchar(30) not null,
address varchar(40) not null,
office_mgr int not null)
partition by roundrobin
(part1 on seg1,
part2 on seg2,
part3 on seg3)

8.如何取消分区及重新分区?

假定我们建了一个按 hash 分三区的表
1> create table lineitem
2> ( l_orderkey int not null,
3> l_partkey char(10) not null,
4> l_suppkey char(4) not null,
5> l_linenumber int not null,
6> l_quantity int not null)
7> partition by hash (l_orderkey, l_linenumber)
8> (litem_hash1 on seg1,
9> litem_hash2 on seg2,
10> litem_hash3 on seg3)
11> go

如要取消分区,必须先将分区修改为 roundrobin,且在一个段上。
1> alter table lineitem partition by roundrobin (part1)
2> go
其实如此该表就没分区了

然后就可重新分区
1> alter table lineitem partition by hash(l_orderkey)
2> (hash1,hash2,hash3,hash4)
3> go

9.分区注意事项
1>.代理表和系统表不能分区
2>.“alter table”与“create table”的分区语法相同,可以:
l将单个分区修改为多分区
l在改变分区类型的同时改变分区的数量
l改变键值和边界值
l改变分区所在的段
l仅有范围(range)和列表(list)分区可以增加分区
注意修改分区类型等时,ASE系统要将表中的数据按照修改后的分区类型重新分配,如果是大表最好在并行方式且数据库的“select into”开关应打开,这样速度快.
3>分区操作要注意:
l分区不能删除,但分区的表可“truncate table”和“select into”
l取消分区可通过将分区修改为一个roundrobin的分区来实现
l分区的键值和边界值要与数据类型兼容。“rang”分区的键值必须是升序且数据类型不能是“BLOB”、“Java”、“bit”和计算列
4>.分区与索引
l当改变分区策略或分区键,或者将没分区的表分区时均应删除索引
l改变分区其它属性时不必删除索引
l主键必须是分区的条件,因为主键和分区都会决定数据的物理存放顺序,一个表是不可能有两种数据的物理存放顺序的。如果主键不是分区的条件,一个变通的办法是将主键建为唯一值索引,如:
create table sales
(cust_id char(10) not null,
ord_id char(10) not null,
salesdate date not null,
salespsn int not null)
partition by range (salesdate)
(q1 values <= ('3/31/2005') on seg1,
q2 values <= ('6/30/2005') on seg2,
q3 values <= ('9/30/2005') on seg3,
q4 values <= (MAX) on seg4)

create unique index idx1 on sales(cust_id,ord_id)

10.可以对单个分区做的操作

truncate table sales partition jan
update statistics sales partition feb
update table statistics sales partition mar
delete statistics sales partition apr
reorg forwarded_rows sales partition may
reorg reclaim_space sales partition jun
reorg compact sales partition jul
reorg rebuild sales local_idx partition aug
bcp custdb..sales partition sept, oct, nov out months.dat ...
dbcc checktable (sales, null, dec)

没有评论: