Storage-attached Indexing (SAI) Quickstart

To get started with Storage-Attached Indexing (SAI), we’ll do the following steps:

  • Create a keyspace.

  • Create a table.

  • Create an index using SAI.

  • Add data.

  • Create and run a query using SAI.

The examples in this quickstart topic show SAI indexes with non-partition key columns.

Create a keyspace

In cqlsh, define the cycling keyspace to try the commands in a test environment:

  1. CREATE KEYSPACE IF NOT EXISTS cycling
  2. WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : '1' };

Create a database table

Using cqlsh or the CQL Console, create a cyclist_semi_pro database table in the cycling keyspace or the keyspace name of your choice:

  1. CREATE TABLE IF NOT EXISTS cycling.cyclist_semi_pro (
  2. id int,
  3. firstname text,
  4. lastname text,
  5. age int,
  6. affiliation text,
  7. country text,
  8. registration date,
  9. PRIMARY KEY (id));

Create SAI indexes on the database table

To test a non-trivial query, you’ll need some SAI indexes. Use CREATE CUSTOM INDEX commands to create SAI indexes on a few non-primary-key columns in the cyclist_semi_pro table:

  1. CREATE CUSTOM INDEX lastname_sai_idx ON cycling.cyclist_semi_pro (lastname)
  2. USING 'StorageAttachedIndex'
  3. WITH OPTIONS = {'case_sensitive': 'false', 'normalize': 'true', 'ascii': 'true'};
  4. CREATE CUSTOM INDEX age_sai_idx ON cycling.cyclist_semi_pro (age)
  5. USING 'StorageAttachedIndex';
  6. CREATE CUSTOM INDEX country_sai_idx ON cycling.cyclist_semi_pro (country)
  7. USING 'StorageAttachedIndex'
  8. WITH OPTIONS = {'case_sensitive': 'false', 'normalize': 'true', 'ascii': 'true'};
  9. CREATE CUSTOM INDEX registration_sai_idx ON cycling.cyclist_semi_pro (registration)
  10. USING 'StorageAttachedIndex';

Let’s take a look at the description of the table and its indexes:

  • Query

  • Result

  1. DESCRIBE TABLE cycling.cyclist_semi_pro;
  1. CREATE TABLE cycling.cyclist_semi_pro (
  2. id int PRIMARY KEY,
  3. affiliation text,
  4. age int,
  5. country text,
  6. firstname text,
  7. lastname text,
  8. registration date
  9. ) WITH additional_write_policy = '99PERCENTILE'
  10. AND bloom_filter_fp_chance = 0.01
  11. AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
  12. AND comment = ''
  13. AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
  14. AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
  15. AND crc_check_chance = 1.0
  16. AND default_time_to_live = 0
  17. AND gc_grace_seconds = 864000
  18. AND max_index_interval = 2048
  19. AND memtable_flush_period_in_ms = 0
  20. AND min_index_interval = 128
  21. AND nodesync = {'enabled': 'true', 'incremental': 'true'}
  22. AND read_repair = 'BLOCKING'
  23. AND speculative_retry = '99PERCENTILE';
  24. CREATE CUSTOM INDEX registration_sai_idx ON cycling.cyclist_semi_pro (registration) USING 'StorageAttachedIndex';
  25. CREATE CUSTOM INDEX country_sai_idx ON cycling.cyclist_semi_pro (country) USING 'StorageAttachedIndex' WITH OPTIONS = {'normalize': 'true', 'case_sensitive': 'false', 'ascii': 'true'};
  26. CREATE CUSTOM INDEX age_sai_idx ON cycling.cyclist_semi_pro (age) USING 'StorageAttachedIndex';
  27. CREATE CUSTOM INDEX lastname_sai_idx ON cycling.cyclist_semi_pro (lastname) USING 'StorageAttachedIndex' WITH OPTIONS = {'normalize': 'true', 'case_sensitive': 'false', 'ascii': 'true'};

Add data to your table

Use CQLSH INSERT commands to add some data to the cyclist_semi_pro database table:

  1. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (1, 'Carlos', 'Perotti', 22, 'Recco Club', 'ITA', '2020-01-12');
  2. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (2, 'Giovani', 'Pasi', 19, 'Venezia Velocità', 'ITA', '2016-05-15');
  3. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (3, 'Frances', 'Giardello', 24, 'Menaggio Campioni', 'ITA', '2018-07-29');
  4. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (4, 'Mark', 'Pastore', 19, 'Portofino Ciclisti', 'ITA', '2017-06-16');
  5. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (5, 'Irene', 'Cantona', 24, 'Como Velocità', 'ITA', '2012-07-22');
  6. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (6, 'Hugo', 'Herrera', 23, 'Bellagio Ciclisti', 'ITA', '2004-02-12');
  7. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (7, 'Marcel', 'Silva', 21, 'Paris Cyclistes', 'FRA', '2018-04-28');
  8. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (8, 'Theo', 'Bernat', 19, 'Nice Cavaliers', 'FRA', '2007-05-15');
  9. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (9, 'Richie', 'Draxler', 24, 'Normandy Club', 'FRA', '2011-02-26');
  10. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (10, 'Agnes', 'Cavani', 22, 'Chamonix Hauteurs', 'FRA', '2020-01-02');
  11. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (11, 'Pablo', 'Verratti', 19, 'Chamonix Hauteurs', 'FRA', '2006-05-15');
  12. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (12, 'Charles', 'Eppinger', 24, 'Chamonix Hauteurs', 'FRA', '2018-07-29');
  13. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (13, 'Stanley', 'Trout', 30, 'Bolder Boulder', 'USA', '2016-02-12');
  14. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (14, 'Juan', 'Perez', 31, 'Rutgers Alumni Riders', 'USA', '2017-06-16');
  15. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (15, 'Thomas', 'Fulton', 27, 'Exeter Academy', 'USA', '2012-12-15');
  16. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (16, 'Jenny', 'Hamler', 28, 'CU Alums Crankworkz', 'USA', '2012-07-22');
  17. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (17, 'Alice', 'McCaffrey', 26, 'Pennan Power', 'GBR', '2020-02-12');
  18. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (18, 'Nicholas', 'Burrow', 26, 'Aberdeen Association', 'GBR', '2016-02-12');
  19. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (19, 'Tyler', 'Higgins', 24, 'Highclere Agents', 'GBR', '2019-07-31');
  20. INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (20, 'Leslie', 'Boyd', 18, 'London Cyclists', 'GBR', '2012-12-15');

Adding data in this quickstart topic shows simple INSERT commands. To load databases with many rows, consider using DataStax Bulk Loader for Apache Cassandra.

Try out CQL queries

Use the CQLSH SELECT command to submit queries.

The supported query operators for tables with SAI indexes: Numerics: =, <, >, , >=, AND, OR, IN Strings: =, CONTAINS, CONTAINS KEY, AND, OR, IN * Strings or Numerics: LIKE

Find a specific semi-pro cyclist:

  • Query

  • Result

  1. SELECT * FROM cycling.cyclist_semi_pro WHERE lastname = 'Eppinger';
  1. id | affiliation | age | country | firstname | lastname | registration
  2. ----+-------------------+-----+---------+-----------+----------+--------------
  3. 12 | Chamonix Hauteurs | 24 | FRA | Charles | Eppinger | 2018-07-29
  4. (1 rows)

Find semi-pro cyclists whose age is less than or equal to 23:

  • Query

  • Result

  1. SELECT * FROM cycling.cyclist_semi_pro WHERE age <= 23;
  1. id | affiliation | age | country | firstname | lastname | registration
  2. ----+--------------------+-----+---------+-----------+----------+--------------
  3. 10 | Chamonix Hauteurs | 22 | FRA | Agnes | Cavani | 2020-01-02
  4. 11 | Chamonix Hauteurs | 19 | FRA | Pablo | Verratti | 2006-05-15
  5. 1 | Recco Club | 22 | ITA | Carlos | Perotti | 2020-01-12
  6. 8 | Nice Cavaliers | 19 | FRA | Theo | Bernat | 2007-05-15
  7. 2 | Venezia Velocità | 19 | ITA | Giovani | Pasi | 2016-05-15
  8. 4 | Portofino Ciclisti | 19 | ITA | Mark | Pastore | 2017-06-16
  9. 20 | London Cyclists | 18 | GBR | Leslie | Boyd | 2012-12-15
  10. 7 | Paris Cyclistes | 21 | FRA | Marcel | Silva | 2018-04-28
  11. 6 | Bellagio Ciclisti | 23 | ITA | Hugo | Herrera | 2004-02-12
  12. (9 rows)

Find semi-pro cyclists from Great Britain:

  • Query

  • Result

  1. SELECT * FROM cycling.cyclist_semi_pro WHERE country = 'GBR';
  1. id | affiliation | age | country | firstname | lastname | registration
  2. ----+----------------------+-----+---------+-----------+-----------+--------------
  3. 19 | Highclere Agents | 24 | GBR | Tyler | Higgins | 2019-07-31
  4. 18 | Aberdeen Association | 26 | GBR | Nicholas | Burrow | 2016-02-12
  5. 20 | London Cyclists | 18 | GBR | Leslie | Boyd | 2012-12-15
  6. 17 | Pennan Power | 26 | GBR | Alice | McCaffrey | 2020-02-12
  7. (4 rows)

Find semi-pro cyclists who registered between a given date range:

  • Query

  • Result

  1. SELECT * FROM cycling.cyclist_semi_pro WHERE registration > '2010-01-01' AND registration < '2015-12-31' LIMIT 10;
  1. id | affiliation | age | country | firstname | lastname | registration
  2. ----+---------------------+-----+---------+-----------+----------+--------------
  3. 5 | Como Velocità | 24 | ITA | Irene | Cantona | 2012-07-22
  4. 16 | CU Alums Crankworkz | 28 | USA | Jenny | Hamler | 2012-07-22
  5. 15 | Exeter Academy | 27 | USA | Thomas | Fulton | 2012-12-15
  6. 20 | London Cyclists | 18 | GBR | Leslie | Boyd | 2012-12-15
  7. 9 | Normandy Club | 24 | FRA | Richie | Draxler | 2011-02-26
  8. (5 rows)

For query examples with CONTAINS clauses that take advantage of SAI collection maps, lists, and sets, be sure to see SAI collection map examples with keys, values, and entries.

Removing an SAI index

To remove an SAI index, use DROP INDEX.

Example:

  1. DROP INDEX IF EXISTS cycling.age_sai_idx;

Resources

SAI overview