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

techresearch7777777 1,796 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
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,730 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Greg Low 1,495 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. CosmogHong-MSFT 23,166 Reputation points Microsoft Vendor
    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".