Microsoft SQL Server 2025 Troubleshooting Guide: Error 1945 – Maximum Key Length Exceeded
Why This Error Happens
This warning occurs because SQL Server 2025 enforces a hard limit on the total byte size of columns that make up an index key—900 bytes for standard indexes and 1,700 bytes for nonclustered indexes with included columns. When a table schema defines a composite index using variable-length columns (such as NVARCHAR, VARCHAR, or VARBINARY) that could combine to exceed this threshold, SQL Server allows the index to be created but throws Error 1945 at runtime when an actual data combination surpasses the byte limit. This is not a corruption issue but a schema design constraint colliding with real-world data entry.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Index and Columns
- Run the following query to pull the exact index name from the error message and inspect its definition:
SELECT i.name AS IndexName, c.name AS ColumnName, c.max_length
FROM sys.indexes i
JOIN sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
JOIN sys.columns c ON ic.object_id = c.object_id AND ic.column_id = c.column_id
WHERE i.name = 'YourIndexNameFromError';
- Sum the
max_lengthvalues for all key columns to confirm whether you are exceeding 900 bytes (or 1,700 for nonclustered withINCLUDE).
Step 2: Reduce the Effective Key Size
- Shorten variable-length columns where possible (e.g., change
NVARCHAR(450)toNVARCHAR(200)if business logic allows). - Move low-selectivity or large columns (like
NVARCHAR(MAX)or wideVARCHARfields) out of the key and into anINCLUDEclause instead:
CREATE NONCLUSTERED INDEX IX_Example
ON dbo.YourTable (ShortKeyColumn)
INCLUDE (LargeDataColumn);
- Consider using a computed column with a
HASHBYTESchecksum if you need to index very long text fields for lookups.
Step 3: Rebuild the Index with Corrected Definition
- Drop and recreate the index using
DROP_EXISTING = ONto minimize downtime and fragmentation:
CREATE NONCLUSTERED INDEX IX_Example
ON dbo.YourTable (ShortKeyColumn)
WITH (DROP_EXISTING = ON, ONLINE = ON);
- On Windows Server 2025, ensure you run this during a maintenance window if the table is large, as
ONLINE = ONrebuilds require Enterprise/Standard edition support and sufficient tempdb space on theC:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Datavolume.
Step 4: Validate Application-Side Data Insertion Logic
- Check the application or ETL pipeline (e.g., SSIS packages, .NET inserts) for any logic that allows oversized string concatenation into the indexed columns.
- Add a
CHECKconstraint or trigger validation to prevent future inserts from silently failing at the index level:
ALTER TABLE dbo.YourTable
ADD CONSTRAINT CK_MaxLength CHECK (DATALENGTH(ColumnName) <= 900);
Alternative or Verification Steps
- Verify the fix by inserting a test row with maximum-length values into the previously failing columns and confirming the
INSERTsucceeds without Error 1945 in SQL Server Management Studio (SSMS) or the Windows Event Viewer (Application log, source: MSSQLSERVER). - If schema changes aren’t feasible immediately, as a temporary workaround, disable the index (
ALTER INDEX IX_Example ON dbo.YourTable DISABLE;) to unblock data loads, but note this will degrade query performance until a permanent fix is applied.