diff --git a/README.md b/README.md
index e9a7091..3c0c793 100644
--- a/README.md
+++ b/README.md
@@ -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(
@@ -349,6 +350,7 @@ Each Standard Column in the `ColumnOptions.Store` list and any custom columns yo
* `AllowNull`
* `DataLength`
* `NonClusteredIndex`
+* `NonClusteredIndexDirection`
### ColumnName
@@ -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`
diff --git a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlCreateTableWriter.cs b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlCreateTableWriter.cs
index e1ce102..d0d3bcb 100644
--- a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlCreateTableWriter.cs
+++ b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/Platform/SqlCreateTableWriter.cs
@@ -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});"));
}
}
diff --git a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/SqlColumn.cs b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/SqlColumn.cs
index 626e196..340d20c 100644
--- a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/SqlColumn.cs
+++ b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/SqlColumn.cs
@@ -100,6 +100,12 @@ public SqlDbType DataType
///
public bool NonClusteredIndex { get; set; }
+ ///
+ /// Specifies the sort direction for a non-clustered index on the SQL column.
+ /// The default value is . This property is only used when auto-creating a log table.
+ ///
+ public SqlIndexDirection NonClusteredIndexDirection { get; set; }
+
///
/// 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.
diff --git a/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/SqlIndexDirection.cs b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/SqlIndexDirection.cs
new file mode 100644
index 0000000..247f154
--- /dev/null
+++ b/src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/SqlIndexDirection.cs
@@ -0,0 +1,18 @@
+namespace Serilog.Sinks.MSSqlServer
+{
+ ///
+ /// Defines the sort order for an SQL index.
+ ///
+ public enum SqlIndexDirection
+ {
+ ///
+ /// Represents the ascending direction for SQL indexing.
+ ///
+ Asc,
+
+ ///
+ /// Represents the descending direction for SQL indexing.
+ ///
+ Desc
+ }
+}