CREATE INDEX
命令用于在用户指定的列上创建一个索引。 如果您选择索引的列已存在数据,则Cassandra会在“create index
”语句执行后在指定数据列上创建索引。
语法:
CREATE INDEX <identifier> ON <tablename>
创建索引的规则
示例:
让我们举个例子来演示如何在列上创建索引。 在这里,我们为表“student
”中的“student_name
”列创建一个索引。
cqlsh:zyiz_ks> SELECT * FROM student; student_id | student_fees | student_name ------------+--------------+-------------- (0 rows) cqlsh:zyiz_ks>
执行以下命令创建一个索引 -
CREATE INDEX name ON student (student_name);
上面语句中,是在“student_name
”列上创建了索引。
cqlsh:zyiz_ks> CREATE INDEX name ON student (student_name); cqlsh:zyiz_ks> describe student; CREATE TABLE zyiz_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 zyiz_ks.student (student_name); cqlsh:zyiz_ks>
注意:您可以再次使用创建索引查询来验证索引是否已创建。 它将显示已创建索引的消息。