An index is a database structure defined for a table, to improve query and look-up performance for a set of columns. Use the @Index
annotation in code or the <index>
element in the eclipselink-orm.xml
descriptor to create an index on a table.
An index can be defined on an entity or on an attribute. For the entity it must define a set of columns to index.
Index creation is database specific. Some databases may not support indexes. Most databases auto-index primary key and foreign key columns. Some databases support advanced index DDL options. To create more advanced index DDL, a DDL script or native query can be used.
Annotation Elements
Table 2-26 describes this annotation's elements.
Table 2-26 @Index Annotation Elements
Annotation Element | Description | Default |
---|---|---|
|
(Optional) The catalog of the |
Default catalog |
|
(Not required when annotated on a field or method) Specify the set of columns to define the index on. |
For an Entity, none. For an attribute, the attribute's column. |
j |
(Optional) The name of the |
|
|
(Optional) The schema of the |
Default schema |
|
(Optional) The table to define the index on; defaults to entities primary table. |
The entity's primary table. |
|
(Optional) Specify whether the index is unique or non-unique. |
|
Usage
Use @Index
annotation to index any attributes or columns that will commonly be used in queries.
Examples
This example defines three indexes, one on first name, one on last name, and a multiple column index on first name and last name.
Example 2-48 Using @Index Annotation
@Entity @Index(name="EMP_NAME_INDEX", columns={"F_NAME","L_NAME"}) public class Employee{ @Id private long id; @Index @Column(name="F_NAME") private String firstName; @Index @Column(name="L_NAME") private String lastName; ... }
You can also create an index in the eclipselink-orm.xml
descriptor using <index>
, as shown in the following example. Define columns using the <column>
subelement. All the attributes supported in the @Index
annotation are also supported in the <index>
element.
Example 2-49 Using <index> XML
<index name="EMP_NAME_INDEX" table="EMPLOYEE" unique="true"> <column>F_NAME</column> <column>L_NAME</column> </index>
See Also
For more information see: