15.13. Indexes
Prev | Chapter 15. Mapping Metadata | Next

## 15.13. Indexes

Indexes are database structures that speed up searches, similar to how the index of a book helps you find what you're looking for. Placing indexes on columns that you often use in queries can drastically improve query performance. Note that primary key columns and columns with a physical foreign key are already naturally indexed by the database; you do not need to place additional indexes on them.

JDOR only uses index markup for table creation; it is not used at runtime. If you are mapping your classes to existing tables, you do not have to denote your existing indexes in the mapping metadata.

As with foreign keys, JDOR allows you to specify an index with either an attribute or an element. In the attribute-based approach, you place the `indexed` attribute on the mapping element whose columns you want to index. This attribute recognizes the values `true`, `false`, and `unique`. A unique index assumes that the combined data in the index's columns is unique for each row. The following mapping elements accept the `indexed` attribute: `discriminator`, `version`, `field`, `join`, `element`, `key`, `value`, `order`.

Example 15.32. Using the `indexed` Attribute

Indexing the columns of the `Article.title` field:

    <class name="Article" table="ART">
        <field name="title" column="TITLE" indexed="true"/>
        ...
    </class>

In place of the `indexed` attribute, you can use a contextual `index` element. Any element that accepts the `indexed` attribute also accepts a nested `index` element. The element goes after any nested `column`s and `foreign-key`s. It has the following attributes:

- `name`: The name of the index. If you do not specify a name, the JDOR implementation will choose a suitable default.

- `unique`: Boolean attribute indicating whether this is a unique index.

The `index` element also permits nested `extension` elements.

Here is the previous example with an `index` element in place of the `indexed` attribute:

Example 15.33. Using Contextual Index Elements

    <class name="Article" table="ART">
        <field name="title" column="TITLE">
            <index name="ART_TTL_IDX"/>
        </field>
        ...
    </class>

Prev | Up | Next
15.12. Foreign Keys | Home | 15.14. Unique Constraints