SQL 2016 - does a simple SELECT TOP 1 row do an entire Table scan?

techresearch7777777 1,981 Reputation points
2024-04-17T22:47:12.8733333+00:00

Hello, does a simple SELECT TOP 1 [City] FROM [Locations_Table] do an entire Table scan (this Table having 300,000 rows)?

Thanks in advance.

SQL Server | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Greg Low 1,980 Reputation points Microsoft Regional Director
    2024-04-18T00:35:51.36+00:00

    No it doesn't.

    I presume [Locations_Table] is actually a table, not a view. That might change things.

    But for a table, no it doesn't. Where that would change is if you have an ORDER BY clause added. Then it might need to.

    But as your query is written, you're saying you don't care which row is returned, so there's no reason for SQL Server to actually read it all.


  2. LiHongMSFT-4306 31,566 Reputation points
    2024-04-18T01:51:10.0933333+00:00

    Hi @techresearch7777777

    Without ORDER BY, there is no need to do a sort. Is there any index on that table?

    It is suggested to make sure your statistics are up to date, so that you give the optimizer accurate information to find a good plan.

    Best regards,

    Cosmog Hong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.