DISTSTYLE and SORTKEY examples for ALTER MATERIALIZED VIEW - Amazon Redshift
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

DISTSTYLE and SORTKEY examples for ALTER MATERIALIZED VIEW

The examples in this topic show you how to perform DISTSTYLE and SORTKEY changes, using ALTER MATERIALIZED VIEW.

The following example queries show how to alter a DISTSTYLE KEY DISTKEY column using a sample base table:

CREATE TABLE base_inventory( inv_date_sk int4 NOT NULL, inv_item_sk int4 NOT NULL, inv_warehouse_sk int4 NOT NULL, inv_quantity_on_hand int4 ); INSERT INTO base_inventory VALUES(1,1,1,1); CREATE materialized VIEW inventory diststyle even AS SELECT * FROM base_inventory; SELECT "table", diststyle FROM svv_table_info WHERE "table" = 'inventory'; ALTER materialized VIEW inventory ALTER diststyle KEY distkey inv_warehouse_sk; SELECT "table", diststyle FROM svv_table_info WHERE "table" = 'inventory'; ALTER materialized VIEW inventory ALTER distkey inv_item_sk; SELECT "table", diststyle FROM svv_table_info WHERE "table" = 'inventory'; DROP TABLE base_inventory CASCADE;

Alter a materialized view to DISTSTYLE ALL:

CREATE TABLE base_inventory( inv_date_sk int4 NOT NULL, inv_item_sk int4 NOT NULL, inv_warehouse_sk int4 NOT NULL, inv_quantity_on_hand int4 ); INSERT INTO base_inventory VALUES(1,1,1,1); CREATE materialized VIEW inventory diststyle even AS SELECT * FROM base_inventory; SELECT "table", diststyle FROM svv_table_info WHERE "table" = 'inventory'; ALTER MATERIALIZED VIEW inventory ALTER diststyle ALL; SELECT "table", diststyle FROM svv_table_info WHERE "table" = 'inventory'; DROP TABLE base_inventory CASCADE;

The following commands show ALTER MATERIALIZED VIEW SORTKEY examples using a sample base table:

CREATE TABLE base_inventory (c0 int, c1 int); INSERT INTO base_inventory VALUES(1,1); CREATE materialized VIEW inventory interleaved sortkey(c0, c1) AS SELECT * FROM base_inventory; SELECT "table", sortkey1 FROM svv_table_info WHERE "table" = 'inventory'; ALTER materialized VIEW inventory ALTER sortkey(c0, c1); SELECT "table", diststyle, sortkey_num FROM svv_table_info WHERE "table" = 'inventory'; ALTER materialized VIEW inventory ALTER sortkey NONE; SELECT "table", diststyle, sortkey_num FROM svv_table_info WHERE "table" = 'inventory'; ALTER materialized VIEW inventory ALTER sortkey(c0); SELECT "table", diststyle, sortkey_num FROM svv_table_info WHERE "table" = 'inventory'; DROP TABLE base_inventory CASCADE;