How to add a sort key to an existing table in AWS Redshift

后端 未结 7 897
孤街浪徒
孤街浪徒 2021-01-01 09:24

In AWS Redshift, I want to add a sort key to a table that is already created. Is there any command which can add a column and use it as sort key?

7条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-01 09:59

    To add to Yaniv's answer, the ideal way to do this is probably using the CREATE TABLE AS command. You can specify the distkey and sortkey explicitly. I.e.

    CREATE TABLE test_table_with_dist 
    distkey(field) 
    sortkey(sortfield) 
    AS 
    select * from test_table
    

    Additional examples:

    http://docs.aws.amazon.com/redshift/latest/dg/r_CTAS_examples.html

    EDIT

    I've noticed that this method doesn't preserve encoding. Redshift only automatically encodes during a copy statement. If this is a persistent table you should redefine the table and specify the encoding.

    create table test_table_with_dist(
        field1 varchar encode row distkey
        field2 timestam pencode delta sortkey);
    
    insert into test_table select * from test_table;
    

    You can figure out which encoding to use by running analyze compression test_table;

提交回复
热议问题