Oracle分区表管理的一些笔记
Oracle分区表的管理笔记(仅限于对普通表,即堆表的分区管理,IOT跟CLUSTER TABLE不再讨论范围内)
Oracle分区表的管理笔记(仅限于对普通表,即堆表的分区管理,IOT跟CLUSTER TABLE不再讨论范围内)
1. 增加分区(add partition)
语法是:alter table xxx add partition…
需要注意的是如果分区中存在maxvalue或default分区add partition会报错,应使用split
如:
Alter table t_range add partition p5 values less than (50) [tablespace users];
--50 要大于之前分区的所有值
Alter table t_list add partition p5 values (7,8,9) [tablespace users];
--7,8,9均不能在之前分区中出现
Alter table t_hash add partition [p5] [tablespace users];
增加子分区:
Alter table xxx modify partition p1 add subpartition …
如:增加RANGE-HASH子分区
ALTER TABLE diving MODIFY PARTITION locations_us
ADD SUBPARTITION us_locs5 TABLESPACE us1;
Range,list增加分区不会影响索引(包括global 跟local),HASH增加分区会让数据重新分配,产生IO,如果不指定update indexes 选项则会导致有数据移动的索引unusable,需要重新编译。
当然,我们说的对索引的影响都是在表中有数据的情况下,没数据当然影响不到索引了。
2. 合并分区(coalesce partition)
Alter table xxx coalesce partion [update indexes];
Alter table xxx modify partition p1 coalesce subpartition;
仅适用于HASH分区或子分区,合并一次会减少一个分区(最少能减少到1个),数据重新分配,产生IO,有数据移动的索引失效(如果不指定update indexes的话).
3. 删除分区(drop partition)
Alter table xxx drop partition ppp;
删除子分区:
Alter table xxx drop subpartition ppp;
此功能hash不支持。同时要注意,删除分区会同时删除该分区内数据。
同样,如果不指定update indexes的话该操作会导致GLOBAL索引失效,而LOCAL不会,因为对应的LOCAL索引分区也被删除了嘛,其他分区的LOCAL不会受到影响。
4. 交换分区(exchange partition)
Alter table tb1 exchange partition/subpartition p1 with table tb2;
据说是采用了更改数据字典的方式,所以速度比较快。
可以是分区跟非分区表交换,子分区跟非分区表交换,组合分区跟分区表交换。
例如:
组合分区跟分区表交换:
CREATE TABLE t1 (i NUMBER, j NUMBER)
PARTITION BY HASH(i)
(PARTITION p1, PARTITION p2);
CREATE TABLE t2 (i NUMBER, j NUMBER)
PARTITION BY RANGE(j)
SUBPARTITION BY HASH(i)
(PARTITION p1 VALUES LESS THAN (10)
SUBPARTITION t2_pls1
SUBPARTITION t2_pls2,
PARTITION p2 VALUES LESS THAN (20)
SUBPARTITION t2_p2s1
SUBPARTITION t2_p2s2));
ALTER TABLE t2 EXCHANGE PARTITION p1 WITH TABLE t1
WITH VALIDATION;
如果指定WITH VALIDATION(默认) 会对交换进来的数据进行合法检查,看是否符合该分区的规则,WITHOUT VALIDATION 会忽略合法检查(比如ID=12的记录此时可以交换到ID VALUES LESS THAN (10)的分区里),但如果表上有primary key 或unique 约束的话,指定without validation会被忽略。
同样,,如果不指定UPDATE INDEXES ,GLOBAL 索引会失效,需要重新编译。
5. 合并分区(merge partitions)
Alter table xxx merge partitions/subpartitions p1,p2 into partiton/subpartition p3 [TABLESPACE tablespace_name];
HASH不适用,因为它有COALESCE了嘛。
表分区必须是相邻的。
跟COALESCE一样,会产生IO,数据量大的话,IO也是相当大的。
同样可以用UPDATE INDEXES 避免索引失效
6. 修改LIST分区—ADD VALUES
Alter table xxx modify partition/subpartition p1 add values(7,9);
要注意的是,增加的VALUES不能在其他分区列的VALUES值中存在,也不能在DEFAULT分区(如果有的话)中有相应VALUES.
不会影响索引
7. 修改LIST 分区—DROP VALUES
Alter table xxx modify partition/subpartition p1 drop values(8,9);
同样,删除的values 不能存在记录.
不会影响索引
8. 拆分分区(split partitions)
功能与MERGE PARTITIONS相反。通常我们会用来拆分MAXVALUE/DEFAULT分区。
Range partition:
Alter table xxx split partition/subpartition p1 at (15) into (partition/subpartition p1_new1,partition/subpartition p1_new2);
List partition:
Alter table xxx split partition/subpartition p1 values(15,16) into (partition/subpartition p1_new1,partition/subpartition p1_new2);
原分区中符合新值定义的记录会存入第一个分区,其他存入第二个分区,当然,在新分区后面可以指定属性,比如TABLESPACE。
HASH分区不适用。
会产生IO
同样,可用update indexes 来避免索引失效
9. 截断分区(truncate partition)
跟TRUNCATE TABLE一样,截断该分区内的数据。
Alter table xxx truncate partition/subpartition p1;
同样,可用update indexes 来避免索引失效
10. 移动分区(move partition)
Alter table xxx move partition/subpartition p1 …;
有些功能比如改变分区表空间,modify partition就做不到,此时就可以用move partition来做。
Use the MOVE PARTITION clause of the ALTER TABLE statement to:
Re-cluster data and reduce fragmentation
Move a partition to another tablespace
Modify create-time attributes
Store the data in compressed format using table compression
如:
ALTER TABLE parts MOVE PARTITION depot2
TABLESPACE ts094 NOLOGGING COMPRESS;
(如果指定compress,affects only future storage, but not existing data.)
同样,可用update indexes 来避免索引失效
11. 重命名分区(rename partition)
Alter table xxx rename partition/subpartition p1 to p1_new;
跟重命名表差不多。
12. 修改分区默认属性(modify default attributes)
修改表属性:alter table xxx modify default attributes …
修改分区属性(适用于组合分区):alter table xxx modify default attributes for partition p1 …
只对以后添加的分区产生影响,适用于所有分区,其中hash分区只能修改表空间属性。
如:
Alter table xxx modify default attributes tablespace users;
13. 修改子分区模板属性(set subpartition template)
Alter table xxx set subpartition template (…);
仅影响以后的子分区,当前的子分区属性不会改变
如:
Alter table xxx set subpartition template
(partition p1 tablespace tbs_1,
Partition p2 tablespace tbs_2);
如果要取消掉子分区模板:
Alter table xxx set subpartition template ();

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

Use the DataAccessObjects (DAO) library in C++ to connect and operate the database, including establishing database connections, executing SQL queries, inserting new records and updating existing records. The specific steps are: 1. Include necessary library statements; 2. Open the database file; 3. Create a Recordset object to execute SQL queries or manipulate data; 4. Traverse the results or update records according to specific needs.
