
openGauss数据库内存优化表MOT实践-磁盘表转换为MOT表
本篇主要介绍怎么把普通的表转换为MOT表。磁盘表直接转换为MOT尚不能实现,这意味着尚不存在将基于磁盘的表转换为MOT的ALTER TABLE语句。下面介绍如何手动将基于磁盘的表转换为MOT,如何使用gs_dump工具导出数据,以及如何使用gs_restore工具导入数据。
目录
前言
本篇主要介绍怎么把普通的表转换为MOT表。
磁盘表直接转换为MOT尚不能实现,这意味着尚不存在将基于磁盘的表转换为MOT的ALTER TABLE语句。
下面介绍如何手动将基于磁盘的表转换为MOT,如何使用gs_dump工具导出数据,以及如何使用gs_restore工具导入数据。
一、前置条件
-
检查待转换为MOT的磁盘表的模式是否包含所有需要的列。
-
检查架构是否包含任何不支持的列数据类型,具体参见“不支持的数据类型”章节。
-
如果不支持特定列,则建议首先创建一个更新了模式的备磁盘表。此模式与原始表相同,只是所有不支持的类型都已转换为支持的类型。
二、转换步骤介绍
要将基于磁盘的表转换为MOT,请执行以下步骤:
-
暂停应用程序活动。
-
使用gs_dump工具将表数据转储到磁盘的物理文件中。请确保使用data only。
-
重命名原始基于磁盘的表。
-
创建同名同模式的MOT。请确保使用创建FOREIGN关键字指定该表为MOT。
-
使用gs_restore将磁盘文件的数据加载/恢复到数据库表中。
-
浏览或手动验证所有原始数据是否正确导入到新的MOT中。
-
恢复应用程序活动。
须知:
由于表名称保持不变,应用程序查询和相关数据库存储过程将能够无缝访问新的MOT,而无需更改代码。另一种方法是通过INSERT INTO SELECT语句将数据从普通(堆)表复制到新的MOT表。
INSERT INTO [MOT_table] SELECT * FROM [PG_table] WHERE condition;
此方法受MOT事务大小限制,小于1GB。
三、开始转换实践
假设要将数据库benchmarksql中一个基于磁盘的表customer迁移到MOT中。
将customer表迁移到MOT,操作步骤如下:
1、创建测试表并插入数据
-
创建表
CREATE TABLE customer( c_customer_sk integer, c_customer_id char(5), c_first_name char(6), c_last_name char(8), Amount integer);
-
插入数据
INSERT INTO customer(c_customer_sk, c_customer_id, c_first_name,Amount) VALUES (3769, 'hello', 'Grace', 1000);INSERT INTO customer (c_customer_sk, c_first_name) VALUES (3769, 'Grace');INSERT INTO customer (c_customer_sk, c_customer_id, c_first_name) VALUES (3769, 'hello', DEFAULT);INSERT INTO customer (c_customer_sk, c_customer_id, c_first_name,Amount) VALUES (6885, 'maps', 'Joes',2200), (4321, 'tpcds', 'Lily',3000), (9527, 'world', 'James',5000);
2、检查测试表列类型。
验证MOT支持所有类型,详情请参阅附录:不支持的数据类型。
openGauss=# \d+ customer Table "public.customer" Column | Type | Modifiers | Storage | Stats target | Description---------------+--------------+-----------+----------+--------------+------------- c_customer_sk | integer | | plain | | c_customer_id | character(5) | | extended | | c_first_name | character(6) | | extended | | c_last_name | character(8) | | extended | | amount | integer | | plain | |Has OIDs: noOptions: orientation=row, compression=no
openGauss=#
3、请检查测试表数据。
openGauss=# select * from customer; c_customer_sk | c_customer_id | c_first_name | c_last_name | amount---------------+---------------+--------------+-------------+-------- 3769 | hello | Grace | | 1000 3769 | | Grace | | 3769 | hello | | | 6885 | maps | Joes | | 2200 4321 | tpcds | Lily | | 3000 9527 | world | James | | 5000(6 rows)
4、使用gs_dump转储表数据。
[omm@master01 ~]$ gs_dump -Fc postgres -a --table customer -f customer.dump -p 15000gs_dump[port='15000'][postgres][2024-11-08 09:26:28]: Begin scanning database.Progress: [==================================================] 100% (38/37, cur_step/total_step). finish scanning databasegs_dump[port='15000'][postgres][2024-11-08 09:26:29]: Finish scanning database.gs_dump[port='15000'][postgres][2024-11-08 09:26:29]: Start dumping objectsProgress: [==================================================] 100% (3974/3974, dumpObjNums/totalObjNums). dump objectsgs_dump[port='15000'][postgres][2024-11-08 09:26:29]: Finish dumping objectsgs_dump[port='15000'][postgres][2024-11-08 09:26:29]: dump database postgres successfullygs_dump[port='15000'][postgres][2024-11-08 09:26:29]: total time: 487 ms
5、重命名测试表。
openGauss=# alter table customer rename to customer_bk;ALTER TABLE
6、创建与测试表完全相同的MOT。
openGauss=# CREATE foreign TABLE customer( c_customer_sk integer, c_customer_id char(5), c_first_name char(6), c_last_name char(8), Amount integer);openGauss-# openGauss(# openGauss(# openGauss(# openGauss(# openGauss(# openGauss(#CREATE FOREIGN TABLE
7、将测试转储数据导入到新MOT中。
[omm@master01 ~]$ gs_restore -C -d postgres customer.dump -p 15000start restore operation ...Progress: [==================================================] 100% (3/3, restored_entries/total_entries). restore entiresend restore operation ...restore operation successfultotal time: 68 ms
查询数据
openGauss=# select * from customer; c_customer_sk | c_customer_id | c_first_name | c_last_name | amount---------------+---------------+--------------+-------------+-------- 3769 | hello | Grace | | 1000 3769 | | Grace | | 3769 | hello | | | 6885 | maps | Joes | | 2200 4321 | tpcds | Lily | | 3000 9527 | world | James | | 5000(6 rows)
openGauss=# \d List of relations Schema | Name | Type | Owner | Storage--------+--------------+---------------+-------+---------------------------------- public | bmsql_oorder | foreign table | omm | public | customer | foreign table | omm | public | customer_bk | table | omm | {orientation=row,compression=no} public | test | foreign table | omm |(4 rows)
四、使用INSERT方式转换
-
通过INSERT INTO SELECT语句将数据从普通(堆)表复制到新的MOT表。
INSERT INTO [MOT_table] SELECT * FROM [PG_table] WHERE condition;
此方法受MOT事务大小限制,小于1GB。
1、创建MOT表customer_bk01
openGauss=# CREATE foreign TABLE customer_bk01( c_customer_sk integer, c_customer_id char(5), c_first_name char(6), c_last_name char(8), Amount integer);openGauss-# openGauss(# openGauss(# openGauss(# openGauss(# openGauss(# openGauss(#CREATE FOREIGN TABLEopenGauss=#
2、使用INSERT转换
openGauss=# INSERT INTO customer_bk01 SELECT * FROM customer_bk ;INSERT 0 6openGauss=#
-
查询表customer_bk01的数据
openGauss=# select * from customer_bk01; c_customer_sk | c_customer_id | c_first_name | c_last_name | amount---------------+---------------+--------------+-------------+-------- 3769 | hello | Grace | | 1000 3769 | | Grace | | 3769 | hello | | | 6885 | maps | Joes | | 2200 4321 | tpcds | Lily | | 3000 9527 | world | James | | 5000(6 rows)
openGauss=# \d List of relations Schema | Name | Type | Owner | Storage--------+---------------+---------------+-------+---------------------------------- public | bmsql_oorder | foreign table | omm | public | customer | foreign table | omm | public | customer_bk | table | omm | {orientation=row,compression=no} public | customer_bk01 | foreign table | omm | public | test | foreign table | omm |(5 rows)
附录:不支持的数据类型
UUIDUser-Defined Type (UDF)Array data typeNVARCHAR2(n)ClobNameBlobRawPathCircleReltimeBit varying(10)TsvectorTsqueryJSONBoxTextLinePointLSEGPOLYGONINETCIDRMACADDRSmalldatetimeBYTEABitVarbitOIDMoney无限制的varchar/character varyingHSTORE
欢迎小伙伴们交流~
更多推荐










所有评论(0)