Cassandra 教程

Cassandra键空间操作

Cassandra表操作

Cassandra数据CRUD操作

Cassandra CQL数据类型

original icon
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.knowledgedict.com/tutorial/cassandra-create-index.html

Cassandra创建索引


CREATE INDEX命令用于在用户指定的列上创建一个索引。如果您选择索引的列已存在数据,则 Cassandra 会在“create index”语句执行后在指定数据列上创建索引。

语法:

CREATE INDEX <identifier> ON <tablename>

创建索引的规则

  • 由于主键已编入索引,因此无法在主键上创建索引。
  • 在 Cassandra 中,不支持集合索引。
  • 没有对列进行索引,Cassandra 无法过滤该列,除非它是主键。

示例:

让我们举个例子来演示如何在列上创建索引。在这里,我们为表“student”中的“student_name”列创建一个索引。

cqlsh:yiibai_ks> SELECT * FROM student;

 student_id | student_fees | student_name
------------+--------------+--------------

(0 rows)
cqlsh:yiibai_ks>

执行以下命令创建一个索引 -

CREATE INDEX name ON student (student_name);

上面语句中,是在“student_name”列上创建了索引。

cqlsh:yiibai_ks> CREATE INDEX name ON student (student_name);
cqlsh:yiibai_ks> describe student;

CREATE TABLE yiibai_ks.student (
    student_id int PRIMARY KEY,
    student_fees varint,
    student_name text
) WITH bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND crc_check_chance = 1.0
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99PERCENTILE';
CREATE INDEX name ON yiibai_ks.student (student_name);

cqlsh:yiibai_ks>

注意:您可以再次使用创建索引查询来验证索引是否已创建。它将显示已创建索引的消息。