云数据库 GAUSSDB-步骤4:将数据导入GaussDB

时间:2024-01-23 20:09:29

步骤4:将数据导入 GaussDB

  1. 使用如下语句在GaussDB中创建目标表product_info,用于存储导入的数据。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    postgres=# DROP TABLE IF EXISTS product_info;
    postgres=# CREATE TABLE product_info
    (
        product_price                integer        not null,
        product_id                   char(30)       not null,
        product_time                 date           ,
        product_level                char(10)       ,
        product_name                 varchar(200)   ,
        product_type1                varchar(20)    ,
        product_type2                char(10)       ,
        product_monthly_sales_cnt    integer        ,
        product_comment_time         date           ,
        product_comment_num          integer        ,
        product_comment_content      varchar(200)                   
    ) 
    WITH (
    orientation = column,
    compression=middle
    ) 
    DISTRIBUTE BY hash (product_id);
    

  2. (可选)本例步骤1中没有创建索引,不用执行这一步。若目标表存在索引,在数据导入过程中,将增量更新索引信息,影响数据导入性能。建议在执行数据导入前,先删除目标表的索引。在数据导入完成后,再重新创建索引。

    1. 假定在导入表“product_info”上的“product_id”字段上存在普通索引“product_idx”。在执行数据导入前,请先删除相关索引。
      1
      postgres=# DROP INDEX product_idx;
      
    2. 在数据导入完成后,重建索引。
      1
      postgres=# CREATE INDEX product_idx ON product_info(product_id);
      
    3. 打开enable_stream_operator。
      1
      postgres=# set enable_stream_operator=on;;
      
    • 在重建索引过程中,用户可以通过临时增加GUC参数“maintenance_work_mem”/“psort_work_mem”来加快索引的重建。
    • 外表的并行导入需要开启stream算子才能够使用。
    • enable_stream_operator设置为on会影响性能,如果该会话后续还有别的sql执行,建议设置set enable_stream_operator=off,如果没有,则直接断开会话即可。

  3. 将数据源文件中的数据通过外表“product_info_ext”导入到表“product_info”中。

    1
    postgres=# INSERT INTO product_info SELECT * FROM product_info_ext ;
    
    出现以下信息,说明数据导入成功。
    1
    INSERT 0 20
    

  4. 执行SELECT命令查询目标表product_info,查看导入到GaussDB中的数据。

    1
    postgres=# SELECT count(*) FROM product_info;
    

    查询结果显示结果如下,表示导入成功。

    1
    2
    3
    4
    count 
    -------
         20
    (1 row)
    

support.huaweicloud.com/devg-v1-gaussdb/gaussdb_devg_0100.html