如何在 Amazon Redshift 中创建索引

How to create an Index in Amazon Redshift

我正在尝试在 Amazon Redshift 中创建索引,但收到错误

create index on session_log(UserId);

UserId 是一个整数字段。

如果您尝试在 Redshift table 上创建索引(带有名称):

create index IX1 on "SomeTable"("UserId");

您将收到错误消息

An error occurred when executing the SQL command: create index IX1 on "SomeTable"("UserId") ERROR: SQL command "create index IX1 on "SomeTable"("UserId")" not supported on Redshift tables.

这是因为,与其他 data warehouses, Redshift uses columnar storage 一样,其他 RDBMS 中使用的许多索引技术(如添加非聚集索引)并不适用。

但是您可以选择为每个列提供一个 sort key per table, and you can also influence performance with a distribution key for sharding your data, and selecting appropriate compression encodings 以最小化存储和 I/O 开销。

例如,在您的情况下,您可以选择使用 UserId 作为排序键:

create table if not exists "SomeTable"
(
    "UserId" int,
    "Name" text
)
sortkey("UserId");

您可能想阅读一些入门书 like these

您可以 Define Constraints 但仅供参考,正如 Amazon 所说:它们不受 Amazon Redshift 强制执行。尽管如此,主键和外键用作规划提示,如果您的 ETL 过程或应用程序中的其他一些过程强制执行它们的完整性,则应声明它们。

某些服务,例如具有插入模式的管道 (REPLACE_EXISTING) 将需要在您的 table 中定义的主键。

出于其他性能目的,斯图尔特的回答是正确的。

Redshift 允许创建主键

create table user (
id int ,
phone_number int,
primary key(id))

但由于 Redshift 不强制执行此约束,因此主键接受重复值。

有关该问题的附加文章

http://www.sqlhaven.com/amazon-redshift-what-you-need-to-think-before-defining-primary-key/