Syntax of Indexes in Class Definitions
Introduction
An index is a structure maintained by a persistent class that is intended to be used to optimize queries and other functions. These indexes are automatically maintained whenever INSERT, UPDATE, or DELETE SQL-based operations are carried out against the database; likewise for object-based actions. The SQL Query Processor makes use of available indexes when preparing and executing SQL queries.
You can add index definitions to persistent classes. They are not meaningful in other kinds of classes.
Details
An index definition has the following structure:
/// description
Index name On property_expression_list [ keyword_list ];
Where:
-
description (optional) is intended for display in the Class Reference. The description is blank by default. See Creating Class Documentation.
-
name (required) is the name of the index. The name must follow property naming conventions and be unique within this class or table.
This name is used for database administrative purposes (reporting, index building, dropping indexes, and so on). For information on property naming conventions, see Class Members. For information on the relationship between an index name in a class definition and the corresponding SQL index name, see CREATE INDEX.
Tip:It is useful to follow a naming convention so that indexes can be easily distinguished from properties. For example, you could include IDX at the end of all index names.
-
property_expression_list (required) specifies the property or properties on which the index is based and may also include a collation specification for each property. This option is either a single property expression or a comma-separated list of property expressions, enclosed in parentheses.
A given property expression consists of:
-
The name of the property to be indexed.
-
An optional (ELEMENTS) or (KEYS) expression, which provides a means of indexing on collection subvalues.
-
An optional collation expression.
For more details, see Define and Build Indexes.
-
-
keyword_list (optional) is a comma-separated list of keywords that further define the index.
See Index Syntax and Keywords for a complete keyword list.
If this list is omitted, also omit the square brackets.
For example, the following class definition defines two properties and an index based on each of them:
Class MyApp.Student Extends %Persistent
{
Property Name As %String;
Property GPA As %Double;
Index NameIDX On Name;
Index GPAIDX On GPA;
}