indexes in database

Upload: saillesh-pawar

Post on 28-Feb-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 Indexes in DataBase

    1/14

    Indexes

    Abstract

    This is a small article on the Index in DB and defining Clustered and Non-Clustered Index with

    reference to the Microsoft SQL server DB

    ! Introduction

    In our day to day business application huge amount of transactioninformation is being stored on our database. In the beginning when theamount of records is less and everything is working ne and thensuddenly after some time the support guy comes and say query is takingtime to execute and the system is taking the huge amount of time toprocess the records. Indexes comes to prevention where we forecast our

    SQL queries and how db generating the xecution SQL plan for the query.Index result in a tremendous improvement in SQL query if used properlyand vice versa also is true.

    " Indexes

    Indexes are used to explicitly speed up the SQL statement execution on a table. !heindex points to the location of the rows containing the value.

    !he default type of the Index used in the "# are #$!ree index

    #y default% when we execute a select command with no primary key and indexbased on a search criteria e.g. !ake the below numbers as an Id and we will bewriting our select command with id in where clause. So when the database willexecute the query it will search sequentially row by row the Id right from thebeginning till it nd the desired row. So we can imagine the scenario if we havebillions of data.

  • 7/25/2019 Indexes in DataBase

    2/14

    So in order to minimi&e the sequential search problem there when #$!ree comes intothe picture. #$!ree is a self$balancing tree data structure which is an extension of#inary tree that keeps data sorted and allows searches% sequential% insertion anddeletion in a quick manner.

    Figure 2:B-Tree

    Image Source:https://technet.microsoft.com/en-us/library/ms177443(vs!l.1"#$.asp%

    Sequential Search is called !able Scan and when they are searched using #$treethey are called Index seek or index scan.

    Let's do a demo to check what kind of scan does SQL server does when there are noindexes and primary key in table.

    So below we have a table called (ity)amewhich doesn't have any indexer and willcontain information regarding the (ity.

    https://technet/https://technet/
  • 7/25/2019 Indexes in DataBase

    3/14

    Figure 3:T#lCustomer Data

    So let's run our select command with one particular (ityid selected

    *s shown in the diagram and we will use% display stimated xecution plan.

    Figure 4:Demonstrating Dis$la% &stimated &xecution $lan o$tion

    xecution plan tells us how the SQL query performs when we don't have any indexin our table.

  • 7/25/2019 Indexes in DataBase

    4/14

    Figure 5: Demonstrating the execution $lan of the 'uer%

    +e will make use of S!*!IS!I(S I,. STATISTICS IO provides the detailedinformation about the query impact on the "# server. It tells the number of logicalreads% physical reads and how many tables was scanned. * good performing queryis that query which has less number of logical reads.

    ,nce we execute below sets of command and check the output statistics-

    S!S!*!IS!I(SI,,)SL(!/0,1(ity)ame+20(ityI"34567849

    Figure 6:Num#er of logical reads "(

    Scan Count:!his count species that the optimi&er has chosen a plan that

    caused 6 Scan count. Logical Reads:!his number species the number of pages read from the

    cache. !his is the important parameter that is needed to be focused. !hisnumber can be decreased using index structure. Physical Read:!his number species the number of pages actually read

    from the disk. !hese are pages that were not in data cache. SQL serverperforms everything on cache if the requested page is not present it will readthe page from the disk and then put the same in the cache% then use thatpage.

  • 7/25/2019 Indexes in DataBase

    5/14

    Lob logical Reads:!his number grows if request any large ob:ect such as

    an image% varchar;max

  • 7/25/2019 Indexes in DataBase

    6/14

    Figure 8:*dding an Index and setting Create as Clustered Index as True

    )ow let's again run our execution plan and check is it again using !able Scan=

    Figure 9:Demonstrating Index scan now

  • 7/25/2019 Indexes in DataBase

    7/14

    )ow let's run our performance statistics and check is there any di>erence=

    ;6 row;s< a>ected

    +e can see the di>erence before the logical reads were ?5 and now they havedecreased to ?. So we can say there is a performance improvement by using anIndex.

    Let's talk about (lustered and )on (lustered Index.

    CL.ST&/&D IND&01

    (lustered Index determines the physical order of data in the table. In (lusteredIndex the rows are stored physically on the disk in the same order as the Index. Sotherefor there can only be one (lustered Index because the records can only beordered in one order.

  • 7/25/2019 Indexes in DataBase

    8/14

    Figure 10:Structure of a clustered index in a single partition

    Image Source:https://technet.microsoft.com/en-us/library/ms177443(vs!l.1"#$.asp%

    #y the statement (lustered Index determines the physical order of data in the tablemeans that data inserted in the table is order wise reference to the Index (olumn.

    !hat means that if we insert data in unordered way still SQL server will insertin order wise according to the index. If the table is not a (lustered Index% its rowsare inserted in unordered structure known as heap.

    Let take an example and create two same table with di>erent name and create

    index in one table and leave the second and try to insert the values in the table andfetch the same.

    (0*!!*#Ltbl,rder;orderAidint%skuAid int%description varchar;B@

  • 7/25/2019 Indexes in DataBase

    9/14

    mrp decimal%sp decimalerent )on$(lustered Index. +e can determine the following as shown belowwhere we have a pointer table with a row address pointing to the actual data.

    orderAid skuAid "escription mrp sp? 6?77 *pple Iphone C 66@@@ 6@@@@

    6@ 6?7C *pple Iphone B ?7@@@ ??@@@

    6 6?7B Samsung )ote 7 ?6@@@ ?@@@@

    Table 1.

    *ctual "ata Stored.

    orderAid 0,+ Locator? 0,+ *""0SS

    6@ 0,+ *""0SS

    6 0,+ *""0SS

    Table 2.0eference !able pointing towards the *ctual !able rows address.

    F

  • 7/25/2019 Indexes in DataBase

    13/14

    Figure 17.)on (lustered Index pointing towards (lustered Index

    Image source: https://technet.microsoft.com/en-us/library/ms177484(v=sl.1!"#.asp$

    * )on (lustered Index can be typically be imagined as #ook Index.

    Img source: https://%%%.prismnet.com/&hce$res/te$tboo'/images/printin)e$.gif

    Figure 18.#ook Index Gage.

    +e can have a many )on$(lustered Index as shown in above diagram where wehave a number of Indexes. In a )on$(lustered Index the data is stored in theascending and descending order of the )on$(lustered Index key. !he )on$(lusteredIndex doesn't inHuence the way records get inserted in the table. !he leaf node ofthe )on$(lustered index is made up of index pages rather than data pages.

    (oncluding the (lustered and )on$(lustered.

    +e can have only one clustered Index while we can have as many )on$clustered Index. (lustered Index are faster than the )on (lustered as (lustered Index

    determines the physical order of the records.

    https://www.prismnet.com/~hcexres/textbook/images/print_index.gifhttps://www.prismnet.com/~hcexres/textbook/images/print_index.gif
  • 7/25/2019 Indexes in DataBase

    14/14

    "isadvantage of (lustered Index- *s we have already discussed about theadvantages of (lustered Index lets discuss the disadvantage of (lusteredindex.*s we know that in (lustered Index determines the physical order of data inthe table. So if update a record and change the value of the indexed column%the database will need to move an entire column to a new position to makesure the every row is sorted. So the resulting in update query into a deletequery followed by an Insert query which obviously leads to decrease inperformance.

    /eferences

    htt$1++odetocodecom+articles+34as$x

    htt$s1++wwwsim$le-tal5com+content+articleas$x6article782

    http://odetocode.com/articles/70.aspxhttp://odetocode.com/articles/70.aspx