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.
Create index IndexName on KeyspaceName.TableName(ColumnName);
Here is the snapshot where it was tried to filter "dept" column without creating the index. In response, the error was returned.
Here is the snapshot where index is created on dept column.
Create index DeptIndex on University.Student(dept);
Here is the snapshot where it will be successfully filtered 'dept' column.
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.
Drop index IF EXISTS KeyspaceName.IndexName
Here is the snapshot of the executed command 'Drop index' that drops the index DeptIndex.
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.