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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:49:56+00:00 2026-06-06T16:49:56+00:00

Recently, I came across a pattern (not sure, could be an anti-pattern) of sorting

  • 0

Recently, I came across a pattern (not sure, could be an anti-pattern) of sorting data in a SELECT query. The pattern is more of a verbose and non-declarative way for ordering data. The pattern is to dump relevant data from actual table into temporary table and then apply orderby on a field on the temporary table. I guess, the only reason why someone would do that is to improve the performance (which I doubt) and no other benefit.

For e.g. Let’s say, there is a user table. The table might contain rows in millions. We want to retrieve all the users whose first name starts with ‘G’ and sorted by first name. The natural and more declarative way to implement a SQL query for this scenario is:

More natural and declarative way

SELECT * FROM Users
WHERE NAME LIKE 'G%'
ORDER BY Name

Verbose way

SELECT * INTO TempTable
FROM Users
WHERE NAME LIKE 'G%'

SELECT * FROM TempTable
ORDER BY Name

With that context, I have few questions:

  1. Will there be any performance difference between two ways if there is no index on the first name field. If yes, which one would be better.

  2. Will there be any performance difference between two ways if there is an index on the first name field. If yes, which one would be better.

  3. Should not the SQL Server optimizer generate same execution plan for both the ways?

  4. Is there any benefit in writing a verbose way from any other persective like locking/blocking?

Thanks in advance.

  • 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-06T16:49:58+00:00Added an answer on June 6, 2026 at 4:49 pm

    Reguzlarly: Anti pattern by people without an idea what they do.

    SOMETIMES: ok, because SQL Server has a problem that is not resolvable otherwise – not seen that one in yeas, though.

    It makes things slower because it forces the tmpddb table to be fully populated FIRST, while otherwise the query could POSSIBLY be resoled more efficiently.

    last time I saw that was like 3 years ago. We got it 3 times as fast by not being smart and using a tempdb table 😉

    Answers:

    1: No, it still needs a table scan, obviously.

    2: Possibly – depends on data amount, but an index seek by index would contain the data in order already (as the index is ordered by content).

    3: no. Obviously. Query plan optimization is statement by statement. By cutting the execution in 2, the query optimizer CAN NOT merge the join into the first statement.

    4: Only if you run into a query optimizer issue or a limitation of how many tables you can join – not in that degenerate case (degenerate in a technical meaning – i.e. very simplistic). BUt if you need to join MANY MANY tables it may be better to go with an interim step.

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

Sidebar

Related Questions

I recently came across a bit of not-well-tested legacy code for writing data that's
I recently came across a great data structures book, Data Structures Using C (c)
I recently came across the dataType called bytearray in python. Could someone provide scenarios
I recently came across a problem with My.Computer.FileSystem.DeleteDirectory() . It will not delete read
I recently came across this blog post which basically says that we should not
I recently came across the data structure known as a skip list . It
I recently came across a kind of dispatch pattern based on the concepts of
I recently came across this expression - but reading up on Wikipedia did not
I recently came across this interesting term and searched on Net to know more
I recently came across a way to develop pluggable application modules when using ASP.NET

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.