Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6032943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:24:38+00:00 2026-05-23T05:24:38+00:00

I’m developing a query against a table that contains a bunch of points in

  • 0

I’m developing a query against a table that contains a bunch of points in a time series. The table can grow quite large, and so I want the query to effectively downsample the output by averaging points over fixed time intervals. After writing the query, I’m surprised by how SQL Server (2008) has opted to execute the query. The execution plan reveals an unnecessary sorting operation that would become expensive as the time series grows. Here is the problem, reduced to a simple example:

CREATE TABLE [dbo].[Example]
(
    [x] FLOAT NOT NULL,
    [y] FLOAT NOT NULL,
    PRIMARY KEY CLUSTERED 
    (
        [x] ASC
    )
);

SELECT FLOOR([x]), AVG([y])
FROM [dbo].[Example]
GROUP BY FLOOR([x]);

Here I have (x,y) pairs that are already sorted by x (because of the clustered primary key), and I’m averaging y for each whole number x (by truncating with the FLOOR function). I would expect that the table is already suitably sorted for the aggregate since FLOOR is a monotonic function. Unfortunately, SQL Server decides that this data needs to be re-sorted, and here is the execution plan:

Example Execution Plan

Shouldn’t SQL Server be able to perform a streaming aggregation over data grouped by a monotonic function of columns that are already suitably sorted?

Is there a general way to rewrite such queries so that SQL Server will see that the order is preserved?

[Update]
I’ve found an article on the subject Things SQL needs: sargability of monotonic functions and, as the title suggests, it seems like this is an optimization that SQL Server doesn’t yet do (in most cases).

Here are even simpler queries over [dbo].[Example] that demonstrate the point:

SELECT [x], [y]
FROM [dbo].[Example]
ORDER BY FLOOR([x]) --sort performed in execution plan

SELECT [x], [y]
FROM [dbo].[Example]
ORDER BY 2*[x] --NO sort performed in execution plan

SELECT [x], [y]
FROM [dbo].[Example]
ORDER BY 2*[x]+1 --sort performed in execution plan

In any single addition or multiplication, the query optimizer understands that the data already has the same order (and this is seen when you group by such expressions too). So it seems like the concept of monotonic functions is understood by the optimizer, just not generally applied.

I’m testing the computed column / index solution now, but it seems like this will dramatically increase the size of the persisted data since I will need several indices to cover the range of possible intervals.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:24:39+00:00Added an answer on May 23, 2026 at 5:24 am

    Some notes:

    • the plan that you see when table is empty and the plan when table has X rows can be absolutely different plans
    • I don’t think it is correct to have primary key on X field. Can there be two points that have the same X values?

    I think you will have the best query performance if you do something like this:

    create table Point
    (
        PointId int identity(1, 1)
            constraint PK_Example_Id primary key,
        X float not null,
        Y float not null,
        FloorX as floor(x) persisted
    )
    
    create index IX_Point_FloorX_Y on Point(FloorX, Y)
    

    Add some rows:

    declare @RowCount int = 10000
    while(@RowCount > 0)
    begin
        insert Point
        values (cast(crypt_gen_random(2) as int), cast(crypt_gen_random(2) as int))
        set @RowCount -= 1
    end
    

    Query:

    select floor(X), avg(Y)
    from Point
    group by floor(X)
    

    or

    select FloorX, avg(Y)
    from Point
    group by FloorX
    

    both will have the same plan

    Plan: no sorting

    enter image description here

    Another option – you can create indexed view. In this case you will have to query the view directly, unless you have Enterprise Edition, which would use indexed view indexes even if you query table directly.

    [Edit] Just realized I didn’t explicitly answer your question. You asked why would SQL perform sort if X is clustered primary key. SQL does not perform sort on X, it performs sort on floor(x). In other words, if x is already sorted, then f(x) would not necessarily have the same order, right?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.