Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What "Clustered Index Scan (Clustered)" means on SQL Server execution plan?

I have a query that fails to execute with "Could not allocate a new page for database 'TEMPDB' because of insufficient disk space in filegroup 'DEFAULT'".

On the way of trouble shooting I am examining the execution plan. There are two costly steps labeled "Clustered Index Scan (Clustered)". I have a hard time find out what this means?

I would appreciate any explanations to "Clustered Index Scan (Clustered)" or suggestions on where to find the related document?

like image 837
Bin Avatar asked Aug 11 '15 18:08

Bin


People also ask

What is Clustered index scan in SQL Server execution plan?

We can say, a Clustered Index Scan is same like a Table Scan operation i.e. entire index is traversed row by row to return the data set. If the SQL Server optimizer determines there are so many rows need to be returned it is quicker to scan all rows than to use index keys.

Why clustered index is called clustered?

Clustered means that records with similar keys are stored (for the most part) next to each other on disk. So if you have a key with just 1 integer column, the record with a value of "1" will be located next to the record with value "2".

Is Clustered index scan better than table scan?

single SELECT performance: clustered index wins by about 16% due to the second lookup needed for a heap. range SELECT performance: clustered index wins by about 29% due to the random ordering for a heap. concurrent INSERT : heap table wins by 30% under load due to page splits for the clustered index.


2 Answers

I would appreciate any explanations to "Clustered Index Scan (Clustered)"

I will try to put in the easiest manner, for better understanding you need to understand both index seek and scan.

SO lets build the table

use tempdb GO   create table scanseek  (id  int , name varchar(50) default ('some random names')  )  create clustered index IX_ID_scanseek on scanseek(ID)   declare @i int SET @i = 0 while (@i <5000) begin  insert into scanseek select @i, 'Name' + convert( varchar(5) ,@i) set @i =@i+1 END 

An index seek is where SQL server uses the b-tree structure of the index to seek directly to matching records

enter image description here

you can check your table root and leaf nodes using the DMV below

-- check index level  SELECT  index_level ,record_count ,page_count  ,avg_record_size_in_bytes FROM sys.dm_db_index_physical_stats(DB_ID('tempdb'),OBJECT_ID('scanseek'),NULL,NULL,'DETAILED') GO 

Now here we have clustered index on column "ID"

lets look for some direct matching records

select * from scanseek where id =340 

and look at the Execution plan

enter image description here

you've requested rows directly in the query that's why you got a clustered index SEEK .

Clustered index scan: When Sql server reads through for the Row(s) from top to bottom in the clustered index. for example searching data in non key column. In our table NAME is non key column so if we will search some data in the name column we will see clustered index scan because all the rows are in clustered index leaf level.

Example

select * from scanseek where name = 'Name340' 

enter image description here

please note: I made this answer short for better understanding only, if you have any question or suggestion please comment below.

like image 64
Neeraj Prasad Sharma Avatar answered Sep 21 '22 04:09

Neeraj Prasad Sharma


Expanding on Gordon's answer in the comments, a clustered index scan is scanning one of the tables indexes to find the values you are doing a where clause filter, or for a join to the next table in your query plan.

Tables can have multiple indexes (one clustered and many non-clustered) and SQL Server will search the appropriate one based upon the filter or join being executed.

Clustered Indexes are explained pretty well on MSDN. The key difference between clustered and non-clustered is that the clustered index defines how rows are stored on disk.

If your clustered index is very expensive to search due to the number of records, you may want to add a non-clustered index on the table for fields that you search for often, such as date fields used for filtering ranges of records.

like image 36
Martin Noreke Avatar answered Sep 24 '22 04:09

Martin Noreke