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 151841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:31:00+00:00 2026-05-11T09:31:00+00:00

I want to find the highest AutoIncremented value from a field. (its not being

  • 0

I want to find the highest AutoIncremented value from a field. (its not being fetched after an insert where I can use @@SCOPE_IDENTITY etc) Which of these two queries would run faster or gives better performance. Id is the primary key and autoincrement field for Table1. And this is for Sql Server 2005.

SELECT MAX(Id) FROM Table1  SELECT TOP 1 Id FROM Table1 ORDER BY Id DESC 

[Edit]
Yes in this case Id is the field on which I have defined the clustered index.
If the index is ID DESC then what..
And yes it would be nice to know how the performance would be affected if
1. Id is a clustered index + primary key.
2. Id is a clustered index and not primary key.
3. Id is a non clustered index ASC + primary key.
4. Id is a non clustered index ASC and not primary key.
5. Id is a non clustered index DESC + primary key.
6. Id is a non clustered index DESC and not primary key.
7. Id is just AutoIncrement

Hope its not a tall order!

  • 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. 2026-05-11T09:31:01+00:00Added an answer on May 11, 2026 at 9:31 am

    In theory, they will use same plans and run almost same time.

    In practice,

    SELECT TOP 1 Id FROM Table1 ORDER BY Id DESC 

    will more probably use a PRIMARY KEY INDEX.

    Also, this one is more extendable if you will decide to select some else column along with id.

    An actual plan on MAX() says:

    SELECT <- AGGREGATE <- TOP <- CLUSTERED INDEX SCAN 

    , while plan for TOP 1 says:

    SELECT <- TOP <- CLUSTERED INDEX SCAN 

    , i. e. aggregate is omitted.

    Aggregate actually won’t do anything here, as there is but one row.

    P. S. As @Mehrdad Afshari and @John Sansom noted, on a non-indexed field MAX is slightly faster (of course not 20 times as optimizer says):

     -- 18,874,368 rows  SET LANGUAGE ENGLISH SET STATISTICS TIME ON SET STATISTICS IO ON PRINT 'MAX' SELECT MAX(id) FROM master PRINT 'TOP 1' SELECT TOP 1 id FROM master ORDER BY id DESC PRINT 'MAX' SELECT MAX(id) FROM master PRINT 'TOP 1' SELECT TOP 1 id FROM master ORDER BY id DESC PRINT 'MAX' SELECT MAX(id) FROM master PRINT 'TOP 1' SELECT TOP 1 id FROM master ORDER BY id DESC PRINT 'MAX' SELECT MAX(id) FROM master PRINT 'TOP 1' SELECT TOP 1 id FROM master ORDER BY id DESC PRINT 'MAX' SELECT MAX(id) FROM master PRINT 'TOP 1' SELECT TOP 1 id FROM master ORDER BY id DESC  Changed language setting to us_english.  SQL Server Execution Times:    CPU time = 0 ms,  elapsed time = 1 ms.  SQL Server Execution Times:    CPU time = 0 ms,  elapsed time = 1 ms.  SQL Server Execution Times:    CPU time = 0 ms,  elapsed time = 1 ms. MAX  SQL Server Execution Times:    CPU time = 0 ms,  elapsed time = 20 ms.  (строк обработано: 1) Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 447, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.  SQL Server Execution Times:    CPU time = 5452 ms,  elapsed time = 2766 ms. TOP 1  SQL Server Execution Times:    CPU time = 0 ms,  elapsed time = 1 ms.  (строк обработано: 1) Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 2, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.  SQL Server Execution Times:    CPU time = 6813 ms,  elapsed time = 3449 ms. MAX  SQL Server Execution Times:    CPU time = 0 ms,  elapsed time = 1 ms.  (строк обработано: 1) Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 44, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.  SQL Server Execution Times:    CPU time = 5359 ms,  elapsed time = 2714 ms. TOP 1  SQL Server Execution Times:    CPU time = 0 ms,  elapsed time = 1 ms.  (строк обработано: 1) Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.  SQL Server Execution Times:    CPU time = 6766 ms,  elapsed time = 3379 ms. MAX  SQL Server Execution Times:    CPU time = 0 ms,  elapsed time = 1 ms.  (строк обработано: 1) Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.  SQL Server Execution Times:    CPU time = 5406 ms,  elapsed time = 2726 ms. TOP 1  SQL Server Execution Times:    CPU time = 0 ms,  elapsed time = 1 ms.  (строк обработано: 1) Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.  SQL Server Execution Times:    CPU time = 6780 ms,  elapsed time = 3415 ms. MAX  SQL Server Execution Times:    CPU time = 0 ms,  elapsed time = 1 ms.  (строк обработано: 1) Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 85, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.  SQL Server Execution Times:    CPU time = 5392 ms,  elapsed time = 2709 ms. TOP 1  SQL Server Execution Times:    CPU time = 0 ms,  elapsed time = 1 ms.  (строк обработано: 1) Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 10, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.  SQL Server Execution Times:    CPU time = 6766 ms,  elapsed time = 3387 ms. MAX  SQL Server Execution Times:    CPU time = 0 ms,  elapsed time = 1 ms.  (строк обработано: 1) Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.  SQL Server Execution Times:    CPU time = 5374 ms,  elapsed time = 2708 ms. TOP 1  SQL Server Execution Times:    CPU time = 0 ms,  elapsed time = 1 ms.  (строк обработано: 1) Table 'master'. Scan count 3, logical reads 32655, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.  SQL Server Execution Times:    CPU time = 6797 ms,  elapsed time = 3494 ms. 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.