
This is just going to be a quick post but I was asked the question the other day, how can I find all the tables in the database that don’t have a clustered index?
Tables without clustered indexes, known as heaps can give you quite a few different performance problems for various reasons. I don’t really want to go into those here, that could be a subject for another post but finding these heaps is actually very simple. Your friend is sys.indexes.
Have a look in that table and you can easily spot the heaps as those with a type of 0 or type_desc of HEAP.
You can use the following code to return all your heap tables.
SELECT OBJECT_NAME(object_id)
FROM sys.indexes
WHERE type = 0
The query is returning multi-statement valued table functions in the results (not inline table functions for some reason).
To exclude the table functions:
SELECT OBJECT_NAME(object_id), *
FROM sys.indexes
WHERE type = 0
and object_id not in (SELECT o.object_id
FROM sys.sql_modules m
INNER JOIN sys.objects o
ON m.object_id=o.object_id
WHERE type_desc = ‘SQL_TABLE_VALUED_FUNCTION’)
LikeLike
Well I suppose it doesn’t return inline table valued functions since they don’t manifest anything, whereas a multistatement table function actually creates a table variable to return… but it’s still interesting.
LikeLike
Good spot! I hadn’t thought about multi-statement table valued functions but they do show up in sys.indexes so it makes sense that the query would return them.
LikeLike