Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an Index in Amazon Redshift

I'm trying to create indexes in Amazon Redshift but I received an error

create index on session_log(UserId); 

UserId is an integer field.

like image 475
user3600910 Avatar asked Aug 19 '15 06:08

user3600910


People also ask

Can you create indexes in Redshift?

It doesn't support indexes – You can't define indexes in Redshift. Instead, each table has a user-specified sort key, which determines how rows are ordered. ** The query planner uses this information to optimize queries.

How do I create an index in SQL?

The CREATE INDEX statement is used to create indexes in tables. Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes, they are just used to speed up searches/queries.

What are indexes AWS?

In a relational database, an index is a data structure that lets you perform fast queries on different columns in a table. You can use the CREATE INDEX SQL statement to add an index to an existing table, specifying the columns to be indexed.


1 Answers

If you try and create an index (with a name) on a Redshift table:

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

You'll receive the error

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.

This is because, like other data warehouses, Redshift uses columnar storage, and as a result, many of the indexing techniques (like adding non-clustered indexes) used in other RDBMS aren't applicable.

You do however have the option of providing a single sort key per table, and you can also influence performance with a distribution key for sharding your data, and selecting appropriate compression encodings for each column to minimize storage and I/O overheads.

For example, in your case, you may elect to use UserId as a sort key:

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

You might want to read a few primers like these

like image 65
StuartLC Avatar answered Oct 14 '22 12:10

StuartLC