Metadata Indexes
The performance of specific queries that include document and event metadata columns Marten provides some predefined indexes you may optionally enable
Last Modified
Should you be using the ModifiedSince(DateTimeOffset)
or ModifiedBefore(DateTimeOffset)
you can ask Marten to create an index on the document's mt_last_modified
metadata column either using IndexedLastModifiedAttribute
:
[IndexedLastModified]
public class Customer
{
public Guid Id { get; set; }
}
Or by using the fluent interface:
DocumentStore.For(_ =>
{
_.Schema.For<User>().IndexLastModified();
});
Tenant Id
When working with multi-tenancy tables, you might wonder if you ever need a single index on the tenantId column, given that it's already the first part of your composite primary key. While the composite key is often sufficient for most queries, there are cases where a dedicated index on just the tenant Id could still be beneficial for performance on specific query's. In that case you can ask Marten to create an index for you on the document's tenant_id
metadata column either using IndexedTenantIdAttribute
:
[IndexedTenantId]
public class TenantIdIndexCustomer
{
public Guid Id { get; set; }
}
Or by using the fluent interface:
DocumentStore.For(_ =>
{
_.Schema.For<User>().MultiTenanted();
_.Schema.For<User>().IndexTenantId();
});
Soft Delete
If using the soft deletes functionality you can ask Marten to create a partial index on the deleted documents either using SoftDeletedAttribute
:
[SoftDeleted(Indexed = true)]
public class IndexedSoftDeletedDoc
{
public Guid Id;
}
Or by using the fluent interface:
DocumentStore.For(_ =>
{
_.Schema.For<User>().SoftDeletedWithIndex();
});
This will help Postgres answer queries using IsDeleted()
, DeletedSince(DateTimeOffset)
and DeletedBefore(DateTimeOffset)
much more efficiently, Postgres will only index documents when they are deleted, mt_deleted = true
, which also means that the index does not need to be updated for any insert or update where mt_deleted = false