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

  • Home
  • SEARCH
  • 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 7961273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:59:43+00:00 2026-06-04T04:59:43+00:00

I have written a subquery that goes like this: select top 1 A from

  • 0

I have written a subquery that goes like this:

select top 1 A
from my_table
where B = some_joined_value_from_outer_query
order by C desc

I want to create a non-clustered index that would improve performance on this part.

3 questions here:

  1. What would be the correct order for the indexed columns?
  2. Also, should column A be an indexed column or just included in the index?
  3. Can this be rewritten without a subquery (notice the top 1 and order by desc) and could that improve performance?

EDIT:
Here’s the query (part of a data synchronisation process):

SELECT ProductId, (
    SELECT Id 
    FROM [Order]
    WHERE Number = (
        SELECT TOP 1 OrderNumber
        FROM OtherDatabase..ReferenceTable
        WHERE ProductNumber = Product.Number
        ORDER BY [Date] DESC)
    ) AS OrderId
FROM Product
  • 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-06-04T04:59:44+00:00Added an answer on June 4, 2026 at 4:59 am

    (A)

    CREATE NONCLUSTERED INDEX foo ON dbo.my_table(B, C DESC) INCLUDE (A);
    

    (B)

    Since A is not in WHERE or ORDER BY, it is probably sufficient to be in the list of INCLUDEd columns, since it’s just “along for the ride” and not necessary to be part of the key.

    (C)

    Impossible to answer without more context. Why would you only include the subquery then ask about an outer query we can’t see?

    EDIT with regard to the ongoing conversation with @Quassnoi, I just wanted to demonstrate quickly that the sort direction of trailing columns in a non-clustered index can make a big difference on the plans used by a particular query. Let’s take the following contrived example:

    CREATE TABLE dbo.foo1(A INT, B INT, C INT);
    CREATE NONCLUSTERED INDEX foo1x ON dbo.foo1(B, C) INCLUDE(A);
    
    CREATE TABLE dbo.foo2(A INT, B INT, C INT);
    CREATE NONCLUSTERED INDEX foo2x ON dbo.foo2(B, C DESC) INCLUDE(A);
    
    INSERT dbo.foo1 SELECT TOP (500000) c.[object_id], c.[object_id], -1*c.[object_id]
    FROM sys.all_columns AS c CROSS JOIN sys.all_objects
    ORDER BY c.[object_id];
    
    INSERT dbo.foo2 SELECT TOP (500000) c.[object_id], c.[object_id], -1*c.[object_id]
    FROM sys.all_columns AS c CROSS JOIN sys.all_objects
    ORDER BY c.[object_id];
    

    Now, let’s run these two proposed queries and inspect the plans:

    SELECT  A
    FROM    (
            SELECT  A, ROW_NUMBER() OVER (PARTITION BY B ORDER BY C DESC) RN
            FROM    dbo.foo1
            ) q
    WHERE   rn = 1;
    
    SELECT  A
    FROM    (
            SELECT  A, ROW_NUMBER() OVER (PARTITION BY B ORDER BY C DESC) RN
            FROM    dbo.foo2
            ) q
    WHERE   rn = 1;
    

    Here’s the plan for the query against dbo.foo1 (where C is ASC):

    enter image description here

    enter image description here

    And the sort I mentioned:

    enter image description here


    And here’s the plan for the query against dbo.foo2 (where C is DESC):

    enter image description here

    enter image description here


    Now, if you add a WHERE clause to the inner query (e.g. WHERE B = -1024577103), the plans are more similar. But then that also implies that the PARTITION BY is unnecessary and that there needs to be some matching involved to limit the outer query to that value of B also. However my point is still that while for the specific query in the question, the sort direction of each column in an index might have little effect on the plan, but this is not true for all queries that could use the same index.

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

Sidebar

Related Questions

I have written a program that uses qhttp to get a webpage. This works
I have a query and a loop written that lists all the rows from
I have written the following code. I know that a higher order function is
This seems like an easy thing, but I'm drawing a blank. Select * from
I have written a class that extends JPanel. Is it possible to use this
I have written an AIR Application that downloads videos and documents from a server.
I have written a toolbar that runs on the taskbar. Unfortunately, after it is
I have written a class in python that implements __str__(self) but when I use
I have a query in MS Access (2003) that makes use of a subquery.
have written a stochastic simulation in Java, which loads data from a few CSV

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.