Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ columnOpts.Store.Add(StandardColumn.LogEvent);
columnOpts.LogEvent.DataLength = 2048;
columnOpts.PrimaryKey = columnOpts.TimeStamp;
columnOpts.TimeStamp.NonClusteredIndex = true;
columnOpts.TimeStamp.NonClusteredIndexDirection = SqlIndexDirection.Desc;

var log = new LoggerConfiguration()
.WriteTo.MSSqlServer(
Expand Down Expand Up @@ -349,6 +350,7 @@ Each Standard Column in the `ColumnOptions.Store` list and any custom columns yo
* `AllowNull`
* `DataLength`
* `NonClusteredIndex`
* `NonClusteredIndexDirection`

### ColumnName

Expand Down Expand Up @@ -412,9 +414,20 @@ Supported SQL column data types that use this property:

Any individual column can be defined as a non-clustered index, including the table primary key. Use this with caution, indexing carries a relatively high write-throughput penalty. One way to mitigate this is to keep non-clustered indexes offline and use batch reindexing on a scheduled basis.

### NonClusteredIndexDirection

Specifies the sort direction (`ASC` or `DESC`) for a non-clustered index on the SQL column.
The default value is `ASC`.

It is especially useful for the timestamp column,
where choosing the correct sort direction can optimize query performance for workloads that typically scan
recent data first.

This only has effect if `NonClusteredIndex` is `true`.

## Standard Columns

By default (and consistent with the SQL DDL to create a table shown earlier) these columns are included in a new `ColumnOptions.Store` list:
By default (and consistent with the SQL DDL to create a table shown earlier), these columns are included in a new `ColumnOptions.Store` list:

- `StandardColumn.Id`
- `StandardColumn.Message`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public string GetSql()

// collect non-PK indexes for separate output after the table DDL
if (common != null && common.NonClusteredIndex && common != _columnOptions.PrimaryKey)
ix.AppendLine(Invariant($"CREATE NONCLUSTERED INDEX [IX{indexCount++}_{_tableName}] ON [{_schemaName}].[{_tableName}] ([{common.ColumnName}]);"));
ix.AppendLine(Invariant($"CREATE NONCLUSTERED INDEX [IX{indexCount++}_{_tableName}] ON [{_schemaName}].[{_tableName}] ([{common.ColumnName}] {common.NonClusteredIndexDirection});"));
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/SqlColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ public SqlDbType DataType
/// </summary>
public bool NonClusteredIndex { get; set; }

/// <summary>
/// Specifies the sort direction for a non-clustered index on the SQL column.
/// The default value is <see cref="SqlIndexDirection.Asc"/>. This property is only used when auto-creating a log table.
/// </summary>
public SqlIndexDirection NonClusteredIndexDirection { get; set; }

/// <summary>
/// The name of the Serilog property to use as the value when filling the DataTable.
/// If not specified, the ColumnName and PropertyName are the same.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Serilog.Sinks.MSSqlServer
{
/// <summary>
/// Defines the sort order for an SQL index.
/// </summary>
public enum SqlIndexDirection
{
/// <summary>
/// Represents the ascending direction for SQL indexing.
/// </summary>
Asc,

/// <summary>
/// Represents the descending direction for SQL indexing.
/// </summary>
Desc
}
}
Loading