CREATE INDEX命令用于在用户指定的列上创建一个索引。 如果您选择索引的列已存在数据,则Cassandra会在“create index”语句执行后在指定数据列上创建索引。
语法:
CREATE INDEX <identifier> ON <tablename>
创建索引的规则
- 由于主键已编入索引,因此无法在主键上创建索引。
- 在Cassandra中,不支持集合索引。
- 没有对列进行索引,Cassandra无法过滤该列,除非它是主键。
示例:
让我们举个例子来演示如何在列上创建索引。 在这里,我们为表“student”中的“student_name”列创建一个索引。
cqlsh:zaixian_ks> SELECT * FROM student;
 student_id | student_fees | student_name
------------+--------------+--------------
(0 rows)
cqlsh:zaixian_ks>
执行以下命令创建一个索引 -
CREATE INDEX name ON student (student_name);
上面语句中,是在“student_name”列上创建了索引。
cqlsh:zaixian_ks> CREATE INDEX name ON student (student_name);
cqlsh:zaixian_ks> describe student;
CREATE TABLE zaixian_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 zaixian_ks.student (student_name);
cqlsh:zaixian_ks>
注意:您可以再次使用创建索引查询来验证索引是否已创建。 它将显示已创建索引的消息。
						上一篇:
								Cassandra截断表
												下一篇:
								Cassandra删除索引
												
						
						
					
					
					