Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions docs/documents/indexing/computed-indexes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ Calculated indexes are a great way to optimize the querying of a document type w
potentially expensive schema changes and extra runtime insert costs.
:::

::: warning
At this time, calculated indexes do not work against `DateTime` or `DateTimeOffset` fields. You will have
to resort to a duplicated field for these types.
:::

If you want to optimize a document type for searches on a certain field within the JSON body without incurring the potential cost of the duplicated field, you can take advantage of Postgresql's [computed index feature](https://www.postgresql.org/docs/9.5/static/indexes-expressional.html) within Marten with this syntax:

<!-- snippet: sample_using-a-simple-calculated-index -->
Expand Down Expand Up @@ -87,6 +82,21 @@ The configuration above creates an index like this:
```sql
CREATE INDEX mt_doc_user_idx_first_namelast_name ON public.mt_doc_user USING btree (((data ->> 'FirstName'::text)), ((data ->> 'LastName'::text)))
```
#### Indexing (parts of) date values

::: warning
At this time, calculated indexes do not work against `DateTime` or `DateTimeOffset` fields. You will have
to resort to a duplicated field for these types.
:::

When you need to query by a component of a `DateOnly` or `DateTime` value,
you can expose that component as a simple read only property and index it.

```csharp
public required DateOnly From { get; set; }

public int FromYear => From.Year;
```

## Multi-Column Indexes <Badge type="tip" text="7.0" />

Expand Down