Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « To SA or not to SACalculating The Percentage Of Null Values In A Column With Transact SQL »
    comments

    I saw this question in in Google Analytics from a Google search that hit our site.
    So, how can you determice if an index is clustered or not? There are two ways, you can use either the INDEXPROPERTY function or the sysindexes/sys.sysindexes system table/view

    To see how it works we will create a table with one clustered and one non clustered index on it
    Here is the code for that

    1. USE tempdb
    2. go
    3.  
    4. CREATE TABLE Test (id INT, col1 VARCHAR(40), col2 INT, col3 INT)
    5. go
    6.  
    7.  
    8. CREATE NONCLUSTERED  INDEX ix_test ON test (id ASC, col1 DESC,col2 DESC, col3 ASC)
    9. go
    10.  
    11.  
    12. CREATE CLUSTERED  INDEX ix_test_clust ON test (id ASC, col1 DESC)
    13. go

    INDEXPROPERTY
    To use INDEXPROPERTY you need to know the table ID, the name of the index and use IsClustered for the property. To get the table id, you use the OBJECT_ID function with the table name passed in
    So for the index ix_test on table Test we will use INDEXPROPERTY(OBJECT_ID('Test'), 'ix_test','IsClustered')

    1. SELECT 'ix_test',INDEXPROPERTY(OBJECT_ID('Test'), 'ix_test','IsClustered')
    2. union all
    3. SELECT 'ix_test_clust',INDEXPROPERTY(OBJECT_ID('Test'), 'ix_test_clust', 'IsClustered')


    Output
    -------------------
    ix_test		0
    ix_test_clust	1




    SYSINDEXES
    indid holds the information needed to determine if the index is clustered or not
    This info is for SQL Server 2000
    1 = Clustered index
    >1 = Nonclustered
    255 = Entry for tables that have text or image data

    This info is for SQL Server 2005 and up

    0 = Heap
    1 = Clustered index
    >1 = Nonclustered index

    1. select name,case indid when 1 then 'Clustered' else 'Non Clustered' end as TypeOfIndex
    2. from sysindexes --or sys.sysindexes on sql 2005 and up
    3. where name in( 'ix_test', 'ix_test_clust')


    Output
    -----------------------
    name	TypeOfIndex
    ix_test_clust	Clustered
    ix_test	Non Clustered



    And of course you can combine the two methods

    1. SELECT name,INDEXPROPERTY(id, name,'IsClustered')
    2. from sysindexes
    3. where name in( 'ix_test', 'ix_test_clust')


    Output
    -------------
    ix_test_clust	1
    ix_test		0




    As you can see it is pretty easy to determine if an index is a clustered index or a non clustered index. I prefer to use INDEXPROPERTY instead of SYSINDEXES, what about you?




    *** If you have a SQL related question try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    2837 views
    Instapaper

    3 comments

    Comment from: Ted Krueger (onpnt) [Member]
    *****
    Ted Krueger (onpnt) I use INDEXPROPERTY also.
    10/06/09 @ 12:45
    Comment from: SQLDenis [Member] Email
    SQLDenis Yes, INDEXPROPERTY seems cleaner since intent is shown
    10/06/09 @ 12:49
    Comment from: Naomi Nosonovsky [Member]
    *****
    10/06/09 @ 22:11

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)