Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
SQL

SQL Server: Fix a Window Function Re-Sorting an Already Sorted Input

- 29.11.26 - ErcanOPAK

🗄️ Fix a Window Function Re-Sorting an Already Sorted Input

ROW_NUMBER() and its relatives require their own sort to satisfy the PARTITION BY and ORDER BY clause in the OVER() window, and SQL Server’s optimizer only skips that sort when it can prove an existing index already delivers rows in exactly the order the window function needs — partition columns first, then order columns, in the same directions. A source that happens to already be sorted the right way, whether from a CTE’s own ORDER BY or from reading a table that’s naturally insert-ordered, doesn’t automatically satisfy that proof; without an index matching the window’s own partition and order columns, the optimizer adds a redundant Sort operator rather than trusting that the input is already correctly ordered.

🔎 The Problem

SELECT CustomerId, OrderDate,
       ROW_NUMBER() OVER (PARTITION BY CustomerId ORDER BY OrderDate) AS rn
FROM Orders;
-- Execution plan shows a Sort operator before the Sequence Project /
-- Window Spool, even if Orders happens to be scanned in an order that
-- already matches CustomerId, OrderDate - the optimizer doesn't trust
-- "happens to be ordered," it needs an index that GUARANTEES it

✅ Fix: A Window Function Re-Sorting an Already Sorted Input

  • Create or confirm a nonclustered index on (CustomerId, OrderDate) – matching the window’s PARTITION BY and ORDER BY columns in the same order – so the optimizer can prove the sort requirement is already satisfied and skip the extra Sort operator.
  • Check the actual execution plan rather than assuming: look specifically for a Sort operator feeding into the window function’s Window Spool/Sequence Project, which confirms the index isn’t being used for this purpose.
  • Remember that the clustered index’s physical order alone isn’t necessarily enough if the window function partitions or orders by columns that aren’t its leading key columns – the index needs to match the window’s own clause, not just any sort.

🚨 Why This Is Dangerous

  • A redundant Sort operator on a large table can dominate a query’s cost far more than the window function itself, and it’s an easy one to miss since the query still returns correct results — just slower than it needs to be.

SQL Server won’t take your word for it that the data’s already sorted — it needs an index that proves it.

— Database Engineer

Related posts:

SQL: Optimize Complex Joins for Large Datasets

SQL: Optimize Complex Joins for Large Datasets

Fix Deadlocks by Understanding Lock Ordering and Using Proper Isolation Levels

Fix Deadlocks by Understanding Lock Ordering and Using Proper Isolation Levels

SQL Server: Fix Always Encrypted Columns That Silently Break Every Parameterized Query Because the C...

SQL Server: Fix Always Encrypted Columns That Silently Break Every Parameterized Query Because the C...

Views: 7
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

HTML5: Fix a Fieldset Disabled State Leaking Into a Nested Form
The AI Prompt That Finds Unawaited Promises in a Node Script

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 672 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 585 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com