Cassandra Create Index

Command 'Create index' creates an index on the column specified by the user. If the data already exists for the column you want to index, Cassandra creates indexes on the data during the 'create index' statement execution.

  • After creating an index, Cassandra indexes new data automatically when data is inserted.
  • The index cannot be created on primary key as a primary key is already indexed.
  • Indexes on collections are not supported in Cassandra.
  • Without indexing on the column, Cassandra can't filter that column unless it is a primary key.

That's why, for filtering columns in Cassandra, indexes needs to be created.

Syntax

Create index IndexName on KeyspaceName.TableName(ColumnName);

Example

Here is the snapshot where it was tried to filter "dept" column without creating the index. In response, the error was returned.

Create & Drop INDEX in Cassandra: Complete Tutorial

Here is the snapshot where index is created on dept column.

Create & Drop INDEX in Cassandra: Complete Tutorial

Create index DeptIndex on University.Student(dept);

Here is the snapshot where it will be successfully filtered 'dept' column.

Create & Drop INDEX in Cassandra: Complete Tutorial

select * from University.Student where dept='CS';

Cassandra Drop Index

Command 'Drop index' drops the specified index. If index name was not given during index creation, then index name is TableName_ColumnName_idx.

  • If the index does not exist, it will return an error unless IF EXISTS is used that will return no-op.
  • During index creation, you have to specify keyspace name with the index name otherwise index will be dropped from the current keyspace.

Syntax

Drop index IF EXISTS KeyspaceName.IndexName 

Example

Here is the snapshot of the executed command 'Drop index' that drops the index DeptIndex.

Create & Drop INDEX in Cassandra: Complete Tutorial

drop index IF EXISTS University.DeptIndex;

After successful execution of the command, DeptIndex will be dropped from the keyspace. Now data cannot be filtered by the column dept.

 

YOU MIGHT LIKE:
Cassandra

Cassandra PDF

Cassandra is a distributed database management system designed for handling a high volume of...