<p>Since both a <code>Table Scan</code> and a <code>Clustered Index Scan</code> essentially scan all records in the table, why is a Clustered Index Scan supposedly better?</p>

<p>As an example - what's the performance difference between the following when there are many records?:</p>

<pre><code>declare @temp table(
    SomeColumn varchar(50)
)

insert into @temp
select 'SomeVal'

select * from @temp

-----------------------------

declare @temp table(
    RowID int not null identity(1,1) primary key,
    SomeColumn varchar(50)
)

insert into @temp
select 'SomeVal'

select * from @temp
</code></pre>

[{'answer_id': 18782, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': -1, 'selected': False, 'text': "<p>A table scan has to examine every single row of the table.  The clustered index scan only needs to scan the index.  It doesn't scan every record in the table. That's the point, really, of indices.</p>\n"}, {'answer_id': 18807, 'author': 'Stu', 'author_id': 414, 'author_profile': 'https://Stackoverflow.com/users/414', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://msdn.microsoft.com/en-us/library/aa216840(SQL.80).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/aa216840(SQL.80).aspx</a></p>\n\n<p>The Clustered Index Scan logical and physical operator scans the clustered index specified in the Argument column. When an optional WHERE:() predicate is present, only those rows that satisfy the predicate are returned. If the Argument column contains the ORDERED clause, the query processor has requested that the rows\' output be returned in the order in which the clustered index has sorted them. If the ORDERED clause is not present, the storage engine will scan the index in the optimal way (not guaranteeing the output to be sorted).</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/aa178416(SQL.80).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/aa178416(SQL.80).aspx</a></p>\n\n<p>The Table Scan logical and physical operator retrieves all rows from the table specified in the Argument column. If a WHERE:() predicate appears in the Argument column, only those rows that satisfy the predicate are returned.</p>\n'}, {'answer_id': 18906, 'author': 'Mark Brackett', 'author_id': 2199, 'author_profile': 'https://Stackoverflow.com/users/2199', 'pm_score': 7, 'selected': True, 'text': '<p>In a table without a clustered index (a heap table), data pages are not linked together - so traversing pages requires a <a href="http://msdn.microsoft.com/en-us/library/ms188270.aspx" rel="noreferrer">lookup into the Index Allocation Map</a>.</p>\n\n<p>A clustered table, however, has it\'s <a href="http://msdn.microsoft.com/en-us/library/ms177443.aspx" rel="noreferrer">data pages linked in a doubly linked list</a> - making sequential scans a bit faster. Of course, in exchange, you have the overhead of dealing with keeping the data pages in order on <code>INSERT</code>, <code>UPDATE</code>, and <code>DELETE</code>. A heap table, however, requires a second write to the IAM.</p>\n\n<p>If your query has a <code>RANGE</code> operator (e.g.: <code>SELECT * FROM TABLE WHERE Id BETWEEN 1 AND 100</code>), then a clustered table (being in a guaranteed order) would be more efficient - as it could use the index pages to find the relevant data page(s). A heap would have to scan all rows, since it cannot rely on ordering.</p>\n\n<p>And, of course, a clustered index lets you do a CLUSTERED INDEX SEEK, which is pretty much optimal for performance...a heap with no indexes would always result in a table scan.</p>\n\n<p>So:</p>\n\n<ul>\n<li><p>For your example query where you select all rows, the only difference is the doubly linked list a clustered index maintains. This should make your clustered table just a tiny bit faster than a heap with a large number of rows.</p></li>\n<li><p>For a query with a <code>WHERE</code> clause that can be (at least partially) satisfied by the clustered index, you\'ll come out ahead because of the ordering - so you won\'t have to scan the entire table.</p></li>\n<li><p>For a query that is not satisified by the clustered index, you\'re pretty much even...again, the only difference being that doubly linked list for sequential scanning. In either case, you\'re suboptimal.</p></li>\n<li><p>For <code>INSERT</code>, <code>UPDATE</code>, and <code>DELETE</code> a heap may or may not win. The heap doesn\'t have to maintain order, but does require a second write to the IAM. I think the relative performance difference would be negligible, but also pretty data dependent.</p></li>\n</ul>\n\n<p>Microsoft has a <a href="http://www.microsoft.com/technet/prodtechnol/sql/bestpractice/clusivsh.mspx" rel="noreferrer">whitepaper</a> which compares a clustered index to an equivalent non-clustered index on a heap (not exactly the same as I discussed above, but close). Their conclusion is basically to put a clustered index on all tables. I\'ll do my best to summarize their results (again, note that they\'re really comparing a non-clustered index to a clustered index here - but I think it\'s relatively comparable):</p>\n\n<ul>\n<li><code>INSERT</code> performance: clustered index wins by about 3% due to the second write needed for a heap.</li>\n<li><code>UPDATE</code> performance: clustered index wins by about 8% due to the second lookup needed for a heap.</li>\n<li><code>DELETE</code> performance: clustered index wins by about 18% due to the second lookup needed and the second delete needed from the IAM for a heap.</li>\n<li>single <code>SELECT</code> performance: clustered index wins by about 16% due to the second lookup needed for a heap.</li>\n<li>range <code>SELECT</code> performance: clustered index wins by about 29% due to the random ordering for a heap.</li>\n<li>concurrent <code>INSERT</code>: heap table wins by 30% under load due to page splits for the clustered index.</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/18764', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/357/']