DEV Community

InterSystems Developer for InterSystems

Posted on Originally published at community.intersystems.com

Indexes for Beginners in InterSystems IRIS: Which One to Use and When

InterSystems IRIS is, among other things, a relational DBMS. This is why it supports all standard SQL indexes together with some additional ones. For beginners, the variety of index types in IRIS can feel a bit overwhelming. Thus, the goal of this article is to clarify when to use each and provide clear code examples.


1. Standard Index

This is the default index type in InterSystems IRIS. It functions similarly to a traditional SQL index and is best used for the following:

  • High-Cardinality Data: Fields with many unique values, such as names, email addresses, Social Security Numbers, or GUID strings.
  • Range Queries and Exact Lookups: Queries using =, >, <, or BETWEEN on non-numeric or high-cardinality fields.
  • Frequently Modified Properties: Fields that constantly change values, as standard indexes are highly efficient during fast-paced insert, update, and delete loops.

You can define a standard index using either InterSystems ObjectScript (in persistent classes) or standard DDL (Data Definition Language).

ObjectScript Definition:

Class User.Patient Extends %Persistent 
{
Property MedicalRecordNumber As %String;
Property LastName As %String;

/// A unique standard index for MRN
Index MRNIdx On MedicalRecordNumber [ Unique ];

/// A regular standard index for LastName

Index LastNameIdx On LastName;

}

SQL DDL Definition:

CREATE INDEX LastNameIdx ON User.Patient (LastName)

The IRIS query optimizer bypasses a full table scan and jumps directly to the LastNameIdx structure to isolate the matching records.

2. Bitmap Index

This index uses a series of bit vectors (strings of 0s and 1s), with each bit position corresponding to an internal Row ID. If a row possesses a specific value, its bit is set to 1. IRIS compresses these bitmaps using an advanced proprietary scheme, making them incredibly lightweight in memory and on disk. It is best used for the following:

  • Low-Cardinality Data: Properties with a limited number of distinct values across your dataset, such as Gender, Marital Status, Order Status (e.g., Pending, Shipped, Delivered), or US States.
  • The 10,000 Rule: As a rule of thumb, for tables with more than 1 million records, avoid bitmap indexes on fields containing more than 10,000 unique values.
  • Complex Combinations: Queries that utilize multiple boolean operations (AND, OR, NOT) on low-cardinality fields. IRIS can perform bitwise logic directly on the index maps in memory before fetching the actual data rows.

⚠️ Warning: Do not use bitmap indexes on highly transactional tables where rows are continuously inserted or deleted by multiple threads, as this can introduce locking contention within the bitmap chunks.

ObjectScript Definition:

Class User.Patient Extends %Persistent 

{

Property Status As %String(VALUELIST = ",Active,Discharged");

/// Define a bitmap index using the Type keyword

Index StatusIdx On Status [ Type = bitmap ];

Property Gender As %String(VALUELIST = ",M,F");

/// Define a bitmap index using the Type keyword

Index GenderIdx On Gender [ Type = bitmap ];

}

SQL DDL Definition:

CREATE BITMAP INDEX StatusIdx ON User.Patient (Status)

We can use these indices in the following case:

SELECT COUNT() FROM SQLUser.Patient WHERE Status = 'Active' AND Gender = 'M'

IRIS will perform a bitwise AND operation on the Status and Gender bitmaps. The calculation is completed entirely within the index structure, allowing it to return the exact count instantly.

2. Bitmap Extent Index

This is a specialized bitmap index that tracks the rows of the table itself rather than the values of a specific field. Each bit represents a sequential RowID integer value, where 1 or 0 simply indicates whether the corresponding row exists in the database. A table can have, at most, one bitmap extent index. It is best used for the following:

  • Counting Table Rows: InterSystems SQL relies heavily on this index to optimize COUNT() operations by scanning a compressed bitstring rather than traversing row data.
  • Tracking Record Existence: Automatically validating whether specific internal RowIDs are active or have been deleted.

💡 Note: Tables defined via SQL CREATE TABLE automatically receive a bitmap extent index. Tables defined via Persistent Classes do not generate one automatically unless you add a traditional bitmap index to the class, which prompts IRIS to auto-create an extent index.

This index will be used automatically in the following Select statement:

SELECT COUNT(*) FROM SQLUser.Patient

Instead of executing a costly table scan, the query planner reads the single BITMAPEXTENT structure to calculate the total record count.

3. Bitslice Index

This is a unique, highly specialized version of a bitmap index designed exclusively for numeric data. Instead of creating a bitmap for each distinct number, a bitslice index breaks the numeric value into its binary components and creates a "bit plane" position for each bit. It is best used for the following:

  • Numeric Aggregations: Columns where you regularly run mathematical operations like SUM(), AVG(), or range checks on large quantities of rows.
  • Massive Datasets: Tables containing millions of rows, where dynamic totals would be too slow to calculate via traditional row-by-row scanning.

ObjectScript Definition:

Class User.Patient Extends %Persistent 

{

Property Visits As %Numeric;

/// Define a bitslice index using the Type keyword

Index VisitsIdx On Visits [ Type = bitslice ];

}

💡 Note: Bitslice indexes cannot be directly created using standard SQL DDL syntax in IRIS; they must be defined via the persistent class architecture.

We can use these indices in the following Select statement:

SELECT SUM(Visits) FROM SQLUser.Patient WHERE Gender = 'M'

Instead of pulling every row into memory and adding up the amounts, IRIS natively shifts and tallies the bit vectors, computing massive aggregations up to 100 times faster than a standard scan.

4. Columnar Index

This index is designed for fields frequently queried analytically but stored in a table with an underlying row storage structure. By default, IRIS stores table rows horizontally as a $LIST structure within a single global subscript. A columnar index flips this strategy for a specific column, saving its data vertically in a highly compressed, vectorized format.

While a columnar index functions similarly to a bitmap index, it is slightly less efficient for strict equality conditions (=) since it requires a vectorized operation to extract bitstrings. However, it shines dramatically brighter than bitmap indexes for range conditions (>, <, BETWEEN), which can compute ranges in a single vectorized pass rather than combining multiple separate bitstrings.

⚠️ Limitation: A columnar index cannot be defined on a serial property or a subclass. Yet, you can freely determine one on a non-serial property of a subclass (e.g., an %Integer or %Numeric).

It is best used for the following:

  • Hybrid Data Models: Tables that experience heavy transactional row inserts but still require complex analytical querying on specific columns.
  • Range Conditions: Queries looking for numbers, dates, or prices within specific upper and lower bounds.
  • Targeted Aggregations: Queries that compute metrics on a single column while ignoring the rest of the row data.

ObjectScript Definition:

Class User.Patient Extends %Persistent 

{

Property Amount As %Numeric(SCALE = 2);

/// Define a columnar index using the Type keyword

Index AmountIndex On Amount [ Type = columnar ];

}

SQL DDL Definition:

CREATE COLUMNAR INDEX AmountIndex ON SQLUser.Patient(Amount)

We can use these indices in the following Select statement:

SELECT AVG(Amount) FROM SQLUser.Patient WHERE Status = 'Active'

With the AmountIndex active, the query optimizer computes the AVG directly from the compressed, vectorized index data, entirely bypassing row storage.

Summary Cheat Sheet

Index Type Data Profile Primary Use Case Performance Strength
Standard High cardinality (names, IDs, unique strings) Exact lookups, inequality matches, fast write speeds Single row fetches and targeted ranges
Bitmap Low cardinality (<10,000 unique values; status, flags) Combining filters using AND/OR Rapid Boolean operations on large datasets
Bitslice Numeric fields (amounts, ages, scores) Running aggregations like SUM() and AVG() Aggregates without reading raw data
Columnar Fields on row-tables used for heavy scanning/ranges Scans, single-column math, and range conditions High-speed vectorized data operations

I hope this article will shed some light on the basics of indices and their usage in IRIS. You can read more in the documentation listed below.

Top comments (0)