WHY Index Scan Instead Of Seek While Joining Big Table With #Temp Table

Vikrant Sirohi 0 Reputation points
2024-06-10T11:46:44.9033333+00:00

In my sql server 2016 database, I have a table 'BigData' having around 50 million rows and a Temporary table #TempID which contains 600 rows.

CREATE TABLE BigData (ID INT IDENTITY(1,1) PRIMARY KEY, OtherID INT, Comment nvarchar(max), ........................................................................)

CREATE TABLE #TempID (ID INT PRIMARY KEY)

I have also a NonClustered index on OtherID column of BigData table:

CREATE NONCLUSTERED INDEX IX_BigData_OtherID ON BigData (OtherID) INCLUDES (Comment, .............................................)

I am executing the given query in a procedure:

SELECT A.ID, A.Comment

FROM BigTable A

JOIN #TempID B ON A.OtherID=B.ID

ORDER BY A.ID

The query was taking lots of time (more than 5 minutes). When i check the execution plan in activity monitor, it is showing Index Scan which is taking time. Here, my question is, why there is Index Scan instead of Index Seek even if we have only 600 records in #TempID table???

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,203 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,591 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 42,761 Reputation points
    2024-06-10T16:21:11.07+00:00

    why there is Index Scan instead of Index Seek

    Index Scan on which table/column?

    You have no WHERE clause in your query, why do you expect something different then a FULL SCAN?


  2. LiHongMSFT-4306 25,326 Reputation points
    2024-06-11T03:27:54.8933333+00:00

    Hi @Vikrant Sirohi

    why there is Index Scan instead of Index Seek even if we have only 600 records in #TempID table???

    The statistics for the involved columns might be outdated or inaccurate. Try update statistics on the involved tables and indexes.

    Also, check for index fragmentation and rebuild/reorganize indexes if necessary.

    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".

    0 comments No comments