SQL Server Performance

CLUSTER AND NON CLUSTER INDEX

Discussion in 'General DBA Questions' started by reethu, Dec 6, 2005.

  1. reethu New Member

    WHAT is architecture difference in clustered and non clustered index? if i need to create a index on table what index i should create why? other way if i create a clustered index and non clustered index on table A . and non clustered index and non clustered index on table B.
    what kind of architecutural difference can be seen (made) ? try to provide cleare picture
  2. Luis Martin Moderator

    From BOL:

    Table Indexes
    Microsoft® SQL Server™ 2000 supports indexes defined on any column in a table, including computed columns.

    If a table is created with no indexes, the data rows are not stored in any particular order. This structure is called a heap.

    The two types of SQL Server indexes are:

    Clustered
    Clustered indexes sort and store the data rows in the table based on their key values. Because the data rows are stored in sorted order on the clustered index key, clustered indexes are efficient for finding rows. There can only be one clustered index per table, because the data rows themselves can only be sorted in one order. The data rows themselves form the lowest level of the clustered index.

    The only time the data rows in a table are stored in sorted order is when the table contains a clustered index. If a table has no clustered index, its data rows are stored in a heap.

    Nonclustered
    Nonclustered indexes have a structure completely separate from the data rows. The lowest rows of a nonclustered index contain the nonclustered index key values and each key value entry has pointers to the data rows containing the key value. The data rows are not stored in order based on the nonclustered key.

    The pointer from an index row in a nonclustered index to a data row is called a row locator. The structure of the row locator depends on whether the data pages are stored in a heap or are clustered. For a heap, a row locator is a pointer to the row. For a table with a clustered index, the row locator is the clustered index key.

    The only time the rows in a table are stored in any specific sequence is when a clustered index is created on the table. The rows are then stored in sequence on the clustered index key. If a table only has nonclustered indexes, its data rows are stored in a unordered heap.

    Indexes can be unique, which means no two rows can have the same value for the index key. Otherwise, the index is not unique and multiple rows can share the same key value.

    There are two ways to define indexes in SQL Server. The CREATE INDEX statement creates and names an index. The CREATE TABLE statement supports the following constraints that create indexes:

    PRIMARY KEY creates a unique index to enforce the primary key.


    UNIQUE creates a unique index.


    CLUSTERED creates a clustered index.


    NONCLUSTERED creates a nonclustered index.
    When you create an index on SQL Server 2000, you can specify whether the keys are stored in ascending or descending order.

    SQL Server 2000 supports indexes defined on computed columns, as long as the expression defined for the column meets certain restrictions, such as only referencing columns from the table containing the computed column, and being deterministic.

    A fill factor is a property of a SQL Server index that controls how densely the index is packed when created. The default fill factor usually delivers good performance, but in some cases it may be beneficial to change the fill factor. If the table is going to have many updates and inserts, create an index with a low fill factor to leave more room for future keys. If the table is a read-only table that will not change, create the index with a high fill factor to reduce the physical size of the index, which lowers the number of disk reads SQL Server uses to navigate through the index. Fill factors are only applied when the index is created. As keys are inserted and deleted, the index will eventually stabilize at a certain density.

    Indexes not only speed up the retrieval of rows for selects, they also usually increase the speed of updates and deletes. This is because SQL Server must first find a row before it can update or delete the row. The increased efficiency of using the index to locate the row usually offsets the extra overhead needed to update the indexes, unless the table has a lot of indexes.

    This example shows the Transact-SQL syntax for creating indexes on a table.

    USE pubs
    GO
    CREATE TABLE emp_sample
    (emp_id int PRIMARY KEY CLUSTERED,
    emp_name char(50),
    emp_address char(50),
    emp_title char(25) UNIQUE NONCLUSTERED )
    GO
    CREATE NONCLUSTERED INDEX sample_nonclust ON emp_sample(emp_name)
    GO

    Deciding which particular set of indexes will optimize performance depends on the mix of queries in the system. Consider the clustered index on emp_sample.emp_id. This works well if most queries referencing emp_sample have equality or range comparisons on emp_id in their WHERE clauses. If the WHERE clauses of most queries reference emp_name instead of emp_id, performance could be improved by instead making the index on emp_name the clustered index.

    Many applications have a complex mix of queries that is difficult to estimate by interviewing users and programmers. SQL Server 2000 provides an Index Tuning Wizard to help design indexes in a database. The easiest way to design indexes for large schemas with complex access patterns is to use the Index Tuning Wizard.

    You provide the Index Tuning Wizard with a set of SQL statements. This could be a script of statements you build to reflect a typical mix of statements in the system, but it is usually a SQL Profiler trace of the actual SQL statements processed on the system during a period of time that reflects the typical load on the system. The Index Tuning Wizard analyzes the workload and the database, and then recommends an index configuration that will improve the performance of the workload. You can choose to either replace the existing index configuration, or to keep the existing index configuration and implement new indexes to improve the performance of a slow-running subset of the queries.



    Luis Martin
    Moderator
    SQL-Server-Performance.com

    One of the symptoms of an approaching nervous breakdown is the belief that one's work is terribly important
    Bertrand Russell


    All postings are provided “AS IS” with no warranties for accuracy.



  3. FrankKalis Moderator

    quote:
    try to provide cleare picture
    SQL Server comes along with a free and easy to search online manual called Books Online (BOL). You should get into the habit consulting BOL first as almost any question you might have is answered there.

    TANSTAAFL. Don't expect us to do your homework. Read BOL, do basic research on your own and come back with more specific questions.

    --
    Frank Kalis
    Microsoft SQL Server MVP
    http://www.insidesql.de
    Heute schon gebloggt?http://www.insidesql.de/blogs
    Ich unterstütze PASS Deutschland e.V. http://www.sqlpass.de)
  4. satya Moderator

    This sounds like an interview question, please follow as suggested above to refer through books online and Microsoft technet & MSDN for more information.

    Satya SKJ
    Contributing Editor & Forums Moderator
    http://www.SQL-Server-Performance.Com
    This posting is provided “AS IS” with no rights for the sake of knowledge sharing.
  5. derrickleggett New Member

Share This Page