i want to add sort key to a column in a table but the table is created. so is there any command by which we can add sort key to the column(field).
Thanks in advance.
i want to add sort key to a column in a table but the table is created. so is there any command by which we can add sort key to the column(field).
Thanks in advance.
I followed this approach for adding the sort columns to my table table_transactons its more or less same approach only less number of commands.
1) alter table table_transactions rename to table_transactions_backup; 2) create table table_transactions compound sortkey(key1, key2, key3, key4) as select * from table_transactions_backup; 3) drop table table_transactions_backup;
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.
In my experience, this SQL is used for not only changing distkey and sortkey, but also setting the encoding(compression) type.
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.
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.
You can figure out which encoding to use by running
analyze compression test_table;
At the moment I think its not possible (hopefully that will change in the future). In the past when I ran into this kind of situation I created a new table and copied the data from the old one into it.
from http://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html: