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

后端 未结 7 873
孤街浪徒
孤街浪徒 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 10:20

    As Yaniv Kessler mentioned, it's not possible to add or change distkey and sort key after creating a table, and you have to recreate a table and copy all data to the new table. You can use the following SQL format to recreate a table with a new design.

    ALTER TABLE test_table RENAME TO old_test_table;
    CREATE TABLE new_test_table([new table columns]);
    INSERT INTO new_test_table (SELECT * FROM old_test_table);
    ALTER TABLE new_test_table RENAME TO test_table;
    DROP TABLE old_test_table;
    

    In my experience, this SQL is used for not only changing distkey and sortkey, but also setting the encoding(compression) type.

提交回复
热议问题