search
HomeDatabaseMysql Tutorial使用在线重定义方式将普通表转换成分区表

使用在线重定义方式将普通表转换成分区表

Jun 07, 2016 pm 03:28 PM
usePartition TablecreateonlinedefinitionReplace withWayusually

1、创建一张普通表(源表:li.p_table) create table li.p_table(id_n number,date_n date); 2、向源表插入数据 insert into li.p_table values (1,to_date(2013-01-02 01:01:01,yyyy-mm-dd hh24:mi:ss)); insert into li.p_table values (2,to_date(2013-0

1、创建一张普通表(源表:li.p_table)

create table li.p_table(id_n number,date_n date);

2、向源表插入数据

insert into li.p_table values (1,to_date('2013-01-02 01:01:01','yyyy-mm-dd hh24:mi:ss'));

insert into li.p_table values (2,to_date('2013-02-02 01:01:01','yyyy-mm-dd hh24:mi:ss'));

insert into li.p_table values (3,to_date('2013-03-02 01:01:01','yyyy-mm-dd hh24:mi:ss'));

insert into li.p_table values (4,to_date('2013-04-02 01:01:01','yyyy-mm-dd hh24:mi:ss'));

insert into li.p_table values (5,to_date('2013-05-02 01:01:01','yyyy-mm-dd hh24:mi:ss'));

insert into li.p_table values (6,to_date('2013-06-02 01:01:01','yyyy-mm-dd hh24:mi:ss'));

insert into li.p_table values (7,to_date('2013-07-02 01:01:01','yyyy-mm-dd hh24:mi:ss'));

insert into li.p_table values (8,to_date('2013-08-02 01:01:01','yyyy-mm-dd hh24:mi:ss'));

insert into li.p_table values (9,to_date('2013-09-02 01:01:01','yyyy-mm-dd hh24:mi:ss'));

insert into li.p_table values (10,to_date('2013-10-02 01:01:01','yyyy-mm-dd hh24:mi:ss'));

insert into li.p_table values (11,to_date('2013-11-02 01:01:01','yyyy-mm-dd hh24:mi:ss'));

insert into li.p_table values (12,to_date('2013-12-02 01:01:01','yyyy-mm-dd hh24:mi:ss'));

insert into li.p_table values (13,to_date('2014-01-02 01:01:01','yyyy-mm-dd hh24:mi:ss'));

insert into li.p_table values (14,to_date('2014-02-02 01:01:01','yyyy-mm-dd hh24:mi:ss'));

commit;

3、创建一张临时分区表

创建的临时分区表的表结构必须与源表保持一致

create table li.p_partion_table(id_n number,date_n date)

PARTITION BY RANGE (date_n)

(PARTITION P_201301 VALUES LESS THAN (TO_DATE('2013-02-01 00:00:00','yyyy-mm-dd hh24:mi:ss')),

PARTITION P_201302 VALUES LESS THAN (TO_DATE('2013-03-01 00:00:00','yyyy-mm-dd hh24:mi:ss')),

PARTITION P_201303 VALUES LESS THAN (TO_DATE('2013-04-01 00:00:00','yyyy-mm-dd hh24:mi:ss')),

PARTITION P_201304 VALUES LESS THAN (TO_DATE('2013-05-01 00:00:00','yyyy-mm-dd hh24:mi:ss')),

PARTITION P_201305 VALUES LESS THAN (TO_DATE('2013-06-01 00:00:00','yyyy-mm-dd hh24:mi:ss')),

PARTITION P_201306 VALUES LESS THAN (TO_DATE('2013-07-01 00:00:00','yyyy-mm-dd hh24:mi:ss')),

PARTITION P_201307 VALUES LESS THAN (TO_DATE('2013-08-01 00:00:00','yyyy-mm-dd hh24:mi:ss')),

PARTITION P_201308 VALUES LESS THAN (TO_DATE('2013-09-01 00:00:00','yyyy-mm-dd hh24:mi:ss')),

PARTITION P_201309 VALUES LESS THAN (TO_DATE('2013-10-01 00:00:00','yyyy-mm-dd hh24:mi:ss')),

PARTITION P_201310 VALUES LESS THAN (TO_DATE('2013-11-01 00:00:00','yyyy-mm-dd hh24:mi:ss')),

PARTITION P_201311 VALUES LESS THAN (TO_DATE('2013-12-01 00:00:00','yyyy-mm-dd hh24:mi:ss')),

PARTITION P_201312 VALUES LESS THAN (TO_DATE('2014-01-01 00:00:00','yyyy-mm-dd hh24:mi:ss')),

PARTITION P_201401 VALUES LESS THAN (TO_DATE('2014-02-01 00:00:00','yyyy-mm-dd hh24:mi:ss')),

PARTITION MAXVALUE VALUES LESS THAN (MAXVALUE));

4、检测源表(li.p_table)是否可以在线重定义(以ROWID方式)

EXEC DBMS_REDEFINITION.CAN_REDEF_TABLE(UNAME=>'LI',TNAME=>'p_table',OPTIONS_FLAG=>DBMS_REDEFINITION.CONS_USE_ROWID);

如果由于某种原因不能在线重定义,则会报出相应错误提示。

(1)对于无主键的表,在使用DBMS_REDEFINITION.CAN_REDEF_TABLE检测表是否可以在线重定义时,指定以ROWID方式确定数据的唯一行性

(2)对于有主键的表,在使用DBMS_REDEFINITION.CAN_REDEF_TABLE检测表是否可以在线重定义时,指定以cons_use_pk方式确定数据的唯一行性

5、将源表的数据交换到临时表

EXEC DBMS_REDEFINITION.START_REDEF_TABLE(UNAME=>'LI',ORIG_TABLE=>'p_table',INT_TABLE=>'p_partion_table',OPTIONS_FLAG=>DBMS_REDEFINITION.CONS_USE_ROWID);

6、将源表转换成分区表

EXEC DBMS_REDEFINITION.FINISH_REDEF_TABLE(UNAME=>'LI',ORIG_TABLE=>'p_table',INT_TABLE=>'p_partion_table');

7、查看源表(li.p_table)的分区状态

select table_owner,table_name,partition_name from dba_tab_partitions where table_name='P_TABLE' and table_owner='LI'

结果如下:

LI P_TABLE MAXVALUE

LI P_TABLE P_201301

LI P_TABLE P_201302

LI P_TABLE P_201303

LI P_TABLE P_201304

LI P_TABLE P_201305

LI P_TABLE P_201306

LI P_TABLE P_201307

LI P_TABLE P_201308

LI P_TABLE P_201309

LI P_TABLE P_201310

LI P_TABLE P_201311

LI P_TABLE P_201312

LI P_TABLE P_201401

以上说明,到此步为止,源表li.p_table已经从非分区表转换成了按月分区的时间范围分区表

8、查看源表(li.p_table)各个分区的数据分布情况

select * from li.p_table partition (P_201301);

结果:

1 2013/1/2 1:01:01

select * from li.p_table partition (P_201303);

结果:

3 2013/3/3 1:01:01

说明,已经重定义成了分区表,而且数据还全部按照分区范围进行了分区存放

9、查看临时表(li.p_partion_table)的分区状态

select table_owner,table_name,partition_name from dba_tab_partitions where table_name='P_PARTION_TABLE' and table_owner='LI'

结果:空

select owner,table_name,partitioned from dba_tables where table_name='P_PARTION_TABLE' and owner='LI';

结果:

LI P_PARTION_TABLE NO

原本创建的临时分区表名,这时已经是一张非分区表了。

另有一点需要注意:SYS用户下的表,是不允许重定义的。

本文为“踩点”原作,转发请说明出处!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools