Module 06_ Planning Indexes

Embed Size (px)

Citation preview

  • 8/9/2019 Module 06_ Planning Indexes

    1/59

    Module 6:Planning, Creating &Maintaining Indexes

    Vidya Vrat Agarwal. | MCT, MCSD

  • 8/9/2019 Module 06_ Planning Indexes

    2/59

    Overview

    Introduction to Indexes

    B Tree

    Clustered Index

    NonClustered IndexCreating and Dropping Index

    Creating Unique Index

    Creating Composite Index

    Creating Index Options FillFactor & Pad_Index

    Maintaining Indexes

  • 8/9/2019 Module 06_ Planning Indexes

    3/59

    What is an Index

    Indexes in databases are similar to indexes in books.In a book, an index allows you to find informationquickly without reading the entire book.

    In a database, an index allows the database programto find data in a table without scanning the entiretable.

    An Index is an Internal Table Structure that Databaseuses to provide quick access to rows of a table.

  • 8/9/2019 Module 06_ Planning Indexes

    4/59

    Introduction to Indexes

    How Data Is Stored

    Rows are stored in data pages

    Heaps are a collection of data pages for a table

    How Data Is Accessed

    Scanning all data pages in a table (Table Scan)

    Using anIndex that points to data on a page (Indexes)Data Pages

    Page 7 Page 8 Page 9

    Akhtar

    Akhtar Funk

    FunkSmith

    SmithMartin

    Martin...

    ...

    Page 4 Page 5 Page 6...

    ......

    ......

    ......

    ......

    ...

    Con

    ConFunk

    FunkWhite

    White...

    ......

    ...

    Rudd

    RuddWhite

    WhiteBarr

    Barr ...

    ......

    ...

    Smith

    SmithOta

    OtaJones

    Jones...

    ......

    ...

    Martin

    MartinPhua

    PhuaJones

    JonesSmith

    Smith...

    ...

    Ganio

    GanioJones

    JonesHall

    Hall...

    ......

    ...

    ...

    ......

    ......

    ......

    ......

    ...

    ...

    ......

    ......

    ......

    ......

    ...

    ...

    ......

    ......

    ......

    ......

    ...

    ...

    ......

    ......

    ......

    ......

    ...

    ...

    ......

    ......

    ......

    ......

    ...

  • 8/9/2019 Module 06_ Planning Indexes

    5/59

    Use of Table ScanWhen a Table Scan is performed SQL Server starts at the

    beginning of tablelooking through every row in the table. Asit finds rows that match the Criteria of Query, it includesthem in theResult Set.

    Use of IndexesOnce the index is chosen, SQL Server navigates the tree

    structure to the point of data that matches criteria andextracts only the records it needs.

    During the query optimization process, the optimizer takes alook at all the available indexes and choose the best one.

  • 8/9/2019 Module 06_ Planning Indexes

    6/59

    Whether to Create Indexes

    Why to Create an Index ( Advantages)

    Speeds up data access

    Enforces uniqueness of rows

    Speeds joins between tables

    Why Not to Create an Index ( Disadvantages)

    Consumes disk space

    Incurs overhead

    Data Modification takes longer as Indexes have to beupdated.

  • 8/9/2019 Module 06_ Planning Indexes

    7/59

    Indexing Guidelines

    Columns to Index

    Primary and foreign keys

    Those frequently searched in ranges

    Those frequently accessed in sorted order

    Columns Not to Index

    Those seldom referenced in queries

    Those that contain few unique valuesThose defined withbit, text, or image data types

  • 8/9/2019 Module 06_ Planning Indexes

    8/59

    How SQL Server Uses Indexes

    111

    SQL Server:

    Determines Whether an IndexExists and Is Useful

    Navigates Through the Index

    Evaluates the Search Value Against EachKey Value andRepeats This Evaluation Until One of Following Occurs:

    The search value is not greater than or equal to thekey valueThe search value is greater than or equal to the last keyvalue on the index page

    222

    333

  • 8/9/2019 Module 06_ Planning Indexes

    9/59

    Maintaining Heaps

    SQL Server:

    Uses Index Allocation Map (IAM) Pages

    Contain information on where the extents of a heapare stored

    Navigate through the heap and find available space for new rows being inserted

    Connect data pages

    Reclaims Space for New Rows in the Heap When a RowIs Deleted

    Heaps are a collection of data pages for a table

  • 8/9/2019 Module 06_ Planning Indexes

    10/59

    1

    157

    534

    1

    53

    104

    157

    270

    410

    534

    600

    755

    1

    10

    20

    53

    65

    78

    104

    110

    121

    157

    190

    210

    270

    310

    335

    410

    430

    450

    534

    545

    557

    Root

    Non- Leaf

    Leaf

    B - Tree

  • 8/9/2019 Module 06_ Planning Indexes

    11/59

    B - Tree

    Balanced Tree or B Tree attempts to provide a consistent andrelatively low cost method of finding way to a particular piece of information.

    A Tree starts at the Root node. ThisRoot node can point

    directly to the actual location of the data (if there is smallamount of data).

    Non- Leaf nodes are in somewhere between the root and thenode that tells, where data is physically stored.

    Leaf Level nodes are the nodes where obtain the leaf reference to the actual physical data.

  • 8/9/2019 Module 06_ Planning Indexes

    12/59

    Clustered Index

    A Clustered index is Unique for any table i.e. can have onlyone per table.

    The Characteristic of Clustered Index is thatLeaf Levelcontains the actual Data Pages.

    Any new record is inserted according to its actual physicalorder in the clustered index.

    In case of a new record that needs to be inserted into themiddle of the index structure, a normalPage Splitoccurs.

  • 8/9/2019 Module 06_ Planning Indexes

    13/59

    Clustered Indexes

    Clustered indexes are useful for columns that are searchedfrequently for ranges of key values, or are accessed in sorted order.When you create a clustered index, consider the following facts andguidelines:

    Each table can have only one clustered index.

    The physical row order of the table and the order of rows inthe index are the same. You should create clusteredindexes before you create any nonclustered indexesbecause a clustered index changes the physical row order of the table. Rows are sorted into a sequenced order andmaintained in that order.

    Key value uniqueness is maintained explicitly, with theUNIQUE keyword, or implicitly, with an internal uniqueidentifier. These unique identifiers are internal to SQLServer and are not accessible to the user.

  • 8/9/2019 Module 06_ Planning Indexes

    14/59

    Clustered Indexes

    The average size of a clustered index is about five percentof the table size.

    However, clustered index size varies depending on the sizeof the indexed column.When a row is deleted, space is reclaimed and is availablefor a new row.

    During index creation, SQL Server temporarily uses disk

    space from the current database. A clustered index requiresabout 1.2 times the table size for working space when theindex is created. The disk space that is used during indexcreation is reclaimed automatically after the index is created.

  • 8/9/2019 Module 06_ Planning Indexes

    15/59

    Using Nonclustered Indexes

    The Leaf Level is not the Data Page instead it is the level toobtain a pointer to the data.

    Nonclustered Indexes Are theSQL Server Default

    Existing Nonclustered Indexes Are Automatically Rebuilt

    When:An existing clustered index is dropped

    A clustered index is created

    You can have up to 249 nonclustered indexes per table.

  • 8/9/2019 Module 06_ Planning Indexes

    16/59

    USE NorthwindCREATE CLUSTERED INDEX CL_lastnameON employees(lastname)

    USE NorthwindCREATE CLUSTERED INDEX CL_lastnameON employees(lastname)

    Creating and Dropping Indexes

    Using the CREATE INDEX Statement

    Indexes are created automatically on tables withPRIMARY KEY or UNIQUE constraints

    Using the DROP INDEX Statement

    USE NorthwindDROP INDEX employees.CL_lastname

    USE NorthwindDROP INDEX employees.CL_lastname

  • 8/9/2019 Module 06_ Planning Indexes

    17/59

    Create Index Statement

    SQL Server automatically creates indexes when a PRIMARY KEY or UNIQUE constraint is created on a table. Defining a PRIMARY KEYor UNIQUE constraint is preferred over creating standard indexes.

    Indexes can be created on views.

    SQL Server stores index information in the sysindexes systemtable.

    Before you create an index on a column, determine whether indexes already exist on that column.

    Keep your indexes small by defining them on columns that aresmall in size. Typically, smaller indexes are more efficient thanindexes with larger key values.

    Select columns on the basis of uniqueness so that each key value

    identifies a small number of rows.When you create a clustered index, all existing nonclusteredindexes are rebuilt.

  • 8/9/2019 Module 06_ Planning Indexes

    18/59

    Drop Index Statement

    Use the DROP INDEX statement to remove an index on a table. When youdrop an index, consider the following facts:

    SQL Server reclaims disk space that is occupied by the index when you

    execute the DROP INDEX statement.

    You cannot use the DROP INDEX statement on indexes that are created by

    PRIMARY KEY or UNIQUE constraints. You must drop the constraint inorder to drop these indexes.

    When you drop a table, all indexes for that table are also dropped.

    When you drop a clustered index, all nonclustered indexes on the tableare

    rebuilt automatically.

    You must be in the database in which an index resides in order to dropthat

    index.

    The DROP INDEX statement cannot be used on system tables .

  • 8/9/2019 Module 06_ Planning Indexes

    19/59

    Creating Unique Indexes

    USE NorthwindCREATE UNIQUE NONCLUSTERED INDEX U_CustID

    ON customers(CustomerID)

    USE NorthwindCREATE UNIQUE NONCLUSTERED INDEX U_CustIDON customers(CustomerID)

    RANCHRANCH Sant GourmetSant Gourmet Jonas BergulfsenJonas Bergulfsen

    Duplicate key values are not allowedwhen a new row is added to the table

    Duplicate key values are not allowedwhen a new row is added to the table

    Customers Customers Customers

    CustomerID CustomerID CompanyName CompanyName ContactName ContactName

    QUICKBONAP12

    QUICKBONAP12

    QUICK-StopBon app'Walking

    QUICK-StopBon app'Walking

    Horst KlossLaurence LebihanHenry David Thoreau

    Horst KlossLaurence LebihanHenry David ThoreauRANCH Rancho grande Sergio Gutirrez

  • 8/9/2019 Module 06_ Planning Indexes

    20/59

  • 8/9/2019 Module 06_ Planning Indexes

    21/59

    Creating Composite Indexes

    USE NorthwindCREATE UNIQUE NONCLUSTERED INDEX U_OrdID_ProdIDON [Order Details] (OrderID, ProductID)

    USE NorthwindCREATE UNIQUE NONCLUSTERED INDEX U_OrdID_ProdIDON [Order Details] (OrderID, ProductID)

    Composite KeyComposite Key

    Column 1 Column 2

    Order Details Order Details Order Details

    OrderID OrderID

    ProductID ProductID

    UnitPrice UnitPrice

    Quantity Quantity

    102481024810248

    102481024810248

    114272

    114272

    14.0009.80034.800

    14.0009.80034.800

    12105

    12105

    Discount Discount

    0.00.00.0

    0.00.00.0

  • 8/9/2019 Module 06_ Planning Indexes

    22/59

    Obtaining Information on Existing Indexes

    Using the sp_helpindex System Stored Procedure

    Using the sp_help tablename System Stored Procedure

    USE NorthwindEXEC sp_helpindex Customers

    USE NorthwindEXEC sp_helpindex Customers

  • 8/9/2019 Module 06_ Planning Indexes

    23/59

    Creating Index Options

    Using the FILLFACTOR Option

    Using the PAD_INDEX Option

  • 8/9/2019 Module 06_ Planning Indexes

    24/59

    Using the FILLFACTOR Option

    Specifies How Much to Fill the PageImpacts Leaf-Level Pages

    Data Pages FullCon

    FunkWhiteRudd

    ...

    ......

    ...

    470401

    470402470403470501

    White ...470502Barr ...470503

    Akhtar

    FunkSmithMartinSmith

    ...

    ......

    ...

    ...

    470601

    470602470603470604470701

    Ota ...470702

    Martin

    PhuaJonesSmithGanio

    ...

    ......

    ...

    ...

    470801

    470802470803470804470901

    Jones ... 470902

    Fillfactor 50 = Leaf Pages 50% Full

    ConFunkWhite

    ......

    ...

    470401470402470403

    RuddWhiteBarr

    ......

    ...

    470501470502470503

    Akhtar FunkSmith

    ......

    ...

    470601470402470603

    MartinSmithOta

    ......

    ...

    470604470701470702

    MartinPhuaJones

    ......

    ...

    470801470802470803

    SmithGanioWhite

    ......

    ...

    470804470901470902

  • 8/9/2019 Module 06_ Planning Indexes

    25/59

    Using the PAD_INDEX Option

    The PAD_INDEX OptionApplies to Non-Leaf-LevelIndexPages

    If PAD_INDEX Is Not Specified, the Default Leaves Spacefor One Row Entry in Non-Leaf-Level Pages

    Number of Rows on Non-Leaf-Level Pages Is Never LessThan Two

    PAD_INDEX Uses the Fillfactor Value

    USE NorthwindCREATE INDEX OrderID_ind

    ON Orders(OrderID)WITH PAD_INDEX, FILLFACTOR=70

    USE NorthwindCREATE INDEX OrderID_ind

    ON Orders(OrderID)WITH PAD_INDEX, FILLFACTOR=70

  • 8/9/2019 Module 06_ Planning Indexes

    26/59

    Maintaining Indexes

    Data Fragmentation

    DBCC SHOWCONTIG Statement

    DROP_EXISTING Option

  • 8/9/2019 Module 06_ Planning Indexes

    27/59

    Data Fragmentation

    How Fragmentation Occurs

    SQL Server reorganizes index pages when data is modified

    Reorganization causes index pages to split

    Methods of Managing FragmentationDrop and recreate an index and specify a fillfactor value

    Rebuild an index and specify a fillfactor value

    Business Environment

    Data fragmentation can be good for OLTP environmentData fragmentation can be bad for Analysis Servicesenvironment

  • 8/9/2019 Module 06_ Planning Indexes

    28/59

    DBCC SHOWCONTIG Statement

    What DBCC SHOWCONTIG Determines

    Whether a table or index is heavily fragmented

    Whether data and index pages are fullWhen to Execute

    If tables have been heavily modified

    If tables contain imported dataIf tables seem to cause poor query performance

  • 8/9/2019 Module 06_ Planning Indexes

    29/59

    CREATE UNIQUE NONCLUSTERED INDEX U_OrdID_ProdIDON [Order Details] (OrderID, ProductID)WITH DROP_EXISTING, FILLFACTOR=65

    CREATE UNIQUE NONCLUSTERED INDEX U_OrdID_ProdIDON [Order Details] (OrderID, ProductID)WITH DROP_EXISTING, FILLFACTOR=65

    DROP_EXISTING Option

    Rebuilding an IndexReorganizes leaf pagesRemoves fragmentationRecalculates index statistics

    Use the DROP_EXISTING option to change thecharacteristics of an index or to rebuild indexes withouthaving to drop the index and recreate it.

  • 8/9/2019 Module 06_ Planning Indexes

    30/59

    Performance Considerations

    Create Indexes on Foreign Keys

    Create the Clustered Index Before Nonclustered Indexes

    Consider Creating Composite IndexesCreate Multiple Indexes for a Table That Is ReadFrequently

    Use the Index Tuning Wizard

  • 8/9/2019 Module 06_ Planning Indexes

    31/59

    Check Your Understanding.

  • 8/9/2019 Module 06_ Planning Indexes

    32/59

    Q.1. What is an Index.? What are the data access

    methods.?

  • 8/9/2019 Module 06_ Planning Indexes

    33/59

    Q.2. What are the Advantages of Creating an Index.?

  • 8/9/2019 Module 06_ Planning Indexes

    34/59

    Q.3. What are the Disadvantages of creating an Index.?

  • 8/9/2019 Module 06_ Planning Indexes

    35/59

    Q.4. What are the Columns to Index.?

  • 8/9/2019 Module 06_ Planning Indexes

    36/59

    Q.5. What are the Columns Not to Index.?

  • 8/9/2019 Module 06_ Planning Indexes

    37/59

    Q.6. How SQL Server uses Index.?

  • 8/9/2019 Module 06_ Planning Indexes

    38/59

    Q.7. What is Heap .?

  • 8/9/2019 Module 06_ Planning Indexes

    39/59

    Q.8. What is IAM .?

  • 8/9/2019 Module 06_ Planning Indexes

    40/59

    Q.9. What is B Tree.?

  • 8/9/2019 Module 06_ Planning Indexes

    41/59

    Q.10. What are the types of Indexes.?

  • 8/9/2019 Module 06_ Planning Indexes

    42/59

    Q.11. What is Clustered Index.?

  • 8/9/2019 Module 06_ Planning Indexes

    43/59

  • 8/9/2019 Module 06_ Planning Indexes

    44/59

  • 8/9/2019 Module 06_ Planning Indexes

    45/59

    Q.14. How many Columns a Unique and Composite Indexcan have.?

  • 8/9/2019 Module 06_ Planning Indexes

    46/59

    Q.15. What is the Mistake done by a databaseProgrammer in the given SQL Statement .?

    CREATE UNIQUE NONCLUSTEREDINDEX U_OrdID_ProdIDON Order Details (OrderID, ProductID)

  • 8/9/2019 Module 06_ Planning Indexes

    47/59

    Q.16. What is FillFactor.?

  • 8/9/2019 Module 06_ Planning Indexes

    48/59

    Q.17.What is Pad_index.?

  • 8/9/2019 Module 06_ Planning Indexes

    49/59

  • 8/9/2019 Module 06_ Planning Indexes

    50/59

    Q.19. What is the feature of DROP_EXISTING.?

  • 8/9/2019 Module 06_ Planning Indexes

    51/59

    Want to Be an MCP

  • 8/9/2019 Module 06_ Planning Indexes

    52/59

  • 8/9/2019 Module 06_ Planning Indexes

    53/59

    Q.2. Which SQL Server service handles alerts?

    a) MSSQL Server b) Server Agent

    c) Microsoft Distributed Transaction Coordinator

    d)Microsoft Search

  • 8/9/2019 Module 06_ Planning Indexes

    54/59

    Q.3. You have recently created several jobs in order toautomate basic tasks. You discover, however, that the jobs are not always running properly. In which system

    database should you look for job history?a) Master

    b) Msdb

    c) Model

    d) Distribution

  • 8/9/2019 Module 06_ Planning Indexes

    55/59

    Q.4. You are designing a database. Data integrity is aprime concern. Which type of database object will youmost likely implement?

    a) User-defined data type

    b) View

    c) Constraintd) Non-clustered index

  • 8/9/2019 Module 06_ Planning Indexes

    56/59

    Q.5. Where is the system catalog found?

    a) In the master databaseb) In every database

    c) In the database catalog

    d)In system tables

  • 8/9/2019 Module 06_ Planning Indexes

    57/59

    Q.6. You receive a phone call from a junior databaseadministrator. She is having trouble writing a query toupdate values in an identity field. What should you tell

    her to do?a) Use the IDENTITYCOL keyword.

    b) Identity fields cannot be updated.

    c) Change the values to null and then repopulate thecolumn.

    d) Make a second identity field and populate thecolumn.

  • 8/9/2019 Module 06_ Planning Indexes

    58/59

    Review

    Introduction to IndexesB Tree

    Clustered Index

    NonClustered Index

    Creating and Dropping Index

    Creating Unique Index

    Creating Composite Index

    Creating Index Options FillFactor & Pad_Index

    Maintaining Indexes

  • 8/9/2019 Module 06_ Planning Indexes

    59/59

    NO pain, No Gain

    Thank You.