发布网友 发布时间:2022-04-22 23:58
共2个回答
懂视网 时间:2022-05-02 07:41
-- Create table
create table TT_FVP_OCR_ADDRESS
(
id NUMBER not null,
waybill_no VARCHAR2(32) not null,
dest_zone_code VARCHAR2(32),
confidence NUMBER(16,4),
input_tm DATE,
insert_tm DATE default sysdate not null,
deal_flg NUMBER(2) default 0 not null,
deal_count NUMBER(2) default 0 not null,
deal_ip VARCHAR2(30),
deal_tm DATE,
ocr_addr VARCHAR2(1000)
)
partition by range (INSERT_TM)
(
partition TT_FVP_OCR_ADDRESS_P20170616 values less than (TIMESTAMP‘ 2017-06-17 00:00:00‘),
partition TT_FVP_OCR_ADDRESS_P20170617 values less than (TIMESTAMP‘ 2017-06-18 00:00:00‘),
partition TT_FVP_OCR_ADDRESS_P20170618 values less than (TIMESTAMP‘ 2017-06-19 00:00:00‘),
partition TT_FVP_OCR_ADDRESS_P20170619 values less than (TIMESTAMP‘ 2017-06-20 00:00:00‘),
partition TT_FVP_OCR_ADDRESS_P20170620 values less than (TIMESTAMP‘ 2017-06-21 00:00:00‘),
partition TT_FVP_OCR_ADDRESS_P20170621 values less than (TIMESTAMP‘ 2017-06-22 00:00:00‘),
partition TT_FVP_OCR_ADDRESS_P20170622 values less than (TIMESTAMP‘ 2017-06-23 00:00:00‘),
partition TT_FVP_OCR_ADDRESS_P20170623 values less than (TIMESTAMP‘ 2017-06-24 00:00:00‘),
partition TT_FVP_OCR_ADDRESS_P20170624 values less than (TIMESTAMP‘ 2017-06-25 00:00:00‘),
partition TT_FVP_OCR_ADDRESS_P20170625 values less than (TIMESTAMP‘ 2017-06-26 00:00:00‘),
partition TT_FVP_OCR_ADDRESS_P20170626 values less than (TIMESTAMP‘ 2017-06-27 00:00:00‘)
);
-- Add comments to the table
comment on table TT_FVP_OCR_ADDRESS
is ‘ocr地址表‘;
-- Add comments to the columns
comment on column TT_FVP_OCR_ADDRESS.id
is ‘ID‘;
comment on column TT_FVP_OCR_ADDRESS.waybill_no
is ‘运单号‘;
comment on column TT_FVP_OCR_ADDRESS.dest_zone_code
is ‘收方城市代码‘;
comment on column TT_FVP_OCR_ADDRESS.confidence
is ‘可信度‘;
comment on column TT_FVP_OCR_ADDRESS.input_tm
is ‘录单时间‘;
comment on column TT_FVP_OCR_ADDRESS.insert_tm
is ‘插入时间‘;
comment on column TT_FVP_OCR_ADDRESS.deal_flg
is ‘处理标志‘;
comment on column TT_FVP_OCR_ADDRESS.deal_count
is ‘处理次数‘;
comment on column TT_FVP_OCR_ADDRESS.deal_ip
is ‘处理IP‘;
comment on column TT_FVP_OCR_ADDRESS.deal_tm
is ‘处理时间‘;
comment on column TT_FVP_OCR_ADDRESS.ocr_addr
is ‘纠错地址‘;
create index IDX_TT_FVP_OCR_ADDRESS on TT_FVP_OCR_ADDRESS (DEAL_FLG)
local;
-- Create/Recreate primary, unique and foreign key constraints
alter table TT_FVP_OCR_ADDRESS
add constraint PK_TT_FVP_OCR_ADDRESS primary key (ID, INSERT_TM)
using index
local;
--创建FVP_OCR_ADDRESS表的序列
create sequence SEQ_TT_FVP_OCR_ADDRESS
minvalue 1
maxvalue 999999999999999999999
start with 1000
increment by 1
cache 20;
--维护分区
DECLARE
V_OUT VARCHAR2(2000);
BEGIN
DBAMON.CONFIG_TAB_POLICY(V_OUT,‘SSS‘,‘TT_FVP_OCR_ADDRESS‘,7,1,0,24,‘DAY‘);
DBMS_OUTPUT.PUT_LINE(V_OUT);
END;
SQL分区表示例
标签:input time dea incr cache default traints 序列 end
热心网友 时间:2022-05-02 04:49
有两种方法可以实现对一个表分区.一是创建一个新的标识为分区表的表(你可参照此步骤),然后把数据复制到这张新表,再对这两张表分别改名.或者,像我写在下面的,通过重建或创建一个聚集索引来达到分区一个表.
一个SQL Server表和数据进行分区示例
--Table/Index creation
CREATE TABLE [dbo].[TABLE1]
([pkcol] [int] NOT NULL,
[datacol1] [int] NULL,
[datacol2] [int] NULL,
[datacol3] [varchar](50) NULL,
[partitioncol] datetime)
GO
ALTER TABLE dbo.TABLE1 ADD CONSTRAINT PK_TABLE1 PRIMARY KEY CLUSTERED (pkcol)
GO
CREATE NONCLUSTERED INDEX IX_TABLE1_col2col3 ON dbo.TABLE1 (datacol1,datacol2)
WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
GO
-- Populate table data
DECLARE @val INT
SELECT @val=1
WHILE @val < 1000
BEGIN
INSERT INTO dbo.Table1(pkcol, datacol1, datacol2, datacol3, partitioncol)
VALUES (@val,@val,@val,'TEST',getdate()-@val)
SELECT @val=@val+1
END
GO
通过查看sys.partitions系统视图,上面的代码创建了一个传统的单分区表.
SELECT o.name objectname,i.name indexname, partition_id, partition_number, [rows]
FROM sys.partitions p
INNER JOIN sys.objects o ON o.object_id=p.object_id
INNER JOIN sys.indexes i ON i.object_id=p.object_id and p.index_id=i.index_id
WHERE o.name LIKE '%TABLE1%'
objectname
indexname
partition_id
partition_number
rows
TABLE1 PK_TABLE1 720575940427120 1 999
TABLE1 IX_TABLE1_col2col3 72057594042777600 1 999
创建已分区的SQL Server表
为了创建一个分区表,需要先创建一个分区函数和分区方案. 下面的示例中,将通过datatime字段对一个表分区. 这里是创建这些对象与在系统视图中查看这些元数据的代码.
CREATE PARTITION FUNCTION myDateRangePF (datetime)
AS RANGE RIGHT FOR VALUES ('20110101', '20120101','20130101')
GO
CREATE PARTITION SCHEME myPartitionScheme
AS PARTITION myDateRangePF ALL TO ([PRIMARY])
GO
SELECT ps.name,pf.name,boundary_id,value
FROM sys.partition_schemes ps
INNER JOIN sys.partition_functions pf ON pf.function_id=ps.function_id
INNER JOIN sys.partition_range_values prf ON pf.function_id=prf.function_id
现在我们有一个分区方案,可对表进行分区操作. 既然我们想通过聚集索引来对一个创建了聚集索引的表进行分区,我们需要先删除掉这个索引并通过一个非聚集索引重建这个约束. 若这张表没有聚集索引,我们可忽略这一步,直接执行创建聚集索引语句. 类似地,若有一个聚集索引创建在分区字段上,我们可执行带DROP_EXISTING的创建聚集索引语句. 最后,若关注于执行此任务时数据库停止服务时间并且你使用的是SQL Server企业版本,可通过创建索引的ONLINE=ON选项来使数据库的停止服务时间最小. 记住,在使用ONLINE选项重建索引时,你可能会看到一些性能降低的现象. 这里是一些可能会乃至的脚本.
ALTER TABLE dbo.TABLE1 DROP CONSTRAINT PK_TABLE1
GO
ALTER TABLE dbo.TABLE1 ADD CONSTRAINT PK_TABLE1 PRIMARY KEY NONCLUSTERED (pkcol)
WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE CLUSTERED INDEX IX_TABLE1_partitioncol ON dbo.TABLE1 (partitioncol)
WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON myPartitionScheme(partitioncol)
GO
在这些语句结束后,我们可再次查看sys.partitions系统视图(看上面的代码)并确认我们的表有4个分区.
objectname
indexname
partition_id
partition_number
rows
TABLE1 IX_TABLE1_partitioncol 72057594043039744 1 233
TABLE1 IX_TABLE1_partitioncol 72057594043105280 2 365
TABLE1 IX_TABLE1_partitioncol 72057594043170816 3 366
TABLE1 IX_TABLE1_partitioncol 72057594043236352 4 35
TABLE1 IX_TABLE1_col2col3 72057594043301888 1 999
TABLE1 PK_TABLE1 72057594043367424 1 999
SQL Server表和数据示例清除
--cleanup
DROP TABLE TABLE1
DROP PARTITION SCHEME myPartitionScheme
DROP PARTITION FUNCTION myDateRangePF