data access strategies , code is poetry

Upload: divandann

Post on 13-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Data Access Strategies , Code is Poetry

    1/2

    Page 1L Server: Data access strategies , Code is poetry

    11.5.2010 11:59:48//blogs.developpeur.org/raptorxp/pages/sql-server-data-access-strategies.aspx

    Bienvenue Blogs CodeS-SourceSIdentification|Inscription|Aide

    Code is poetryBlog de RaptorXP (Flavien Charlon) : dveloppement .NET et nouvelles technologies

    QL Server: Data access strategies

    QL Server can use different data access strategies when retrieving rows from the table. The strategy that will be used depends one columns of the table, the available indexes, the query, the data in the table, and the statistics. There are 7 basic data accessrategies.

    lustered index seek

    clustered index seek can only happen on a clustered table. If SQL Server identifies a seek predicate usable in the clustered index, itan perform a clustered index seek. The ordering of the clustered index is leveraged to scan only the rows satisfying the seekredicate.

    his is the most efficient data access strategy.

    lustered index scan and table scan

    hey are both the same operation. It is called a clustered index scan when it is performed on a clustered table and table scan when itperformed on a heap. The whole table is scanned, and only the rows satisfying the query condition are returned.

    full table scan is a good strategy when the number of rows returned is big, and the query is not selective.

    onclustered index scan + key lookup or RID lookup

    hen SQL Server cannot identify a seek predicate for the clustered index, but it can identify one for a nonclustered index, it will try tose it. It will perform an index seek in the nonclustered index to find all the rows qualifying for the seek predicate. Then, for everyow found in the index, it will perform a lookup into the actual data table to retrieve all the columns needed by the query, and notresent in the index. SQL Server uses the row locator in the index rows to locate the corresponding data rows.

    he cost for that operation is always at least an index seek in the nonclustered index. Then:

    If the table is a clustered table, the row locator is a clustering key. For every row returned by the nonclustered index seek, the

    corresponding clustering key is used to perform a clustered index seek (key lookup) and retrieve all the columns needed. Sothere will be as many clustered index seek as the number of rows returned by the nonclustered index seek.If the table is a heap, the row locator is a row ID. For every row returned by the nonclustered index seek, the correspondingrow ID is used to access directly the data row in the table (RID lookup). This counts as a single logical IO. However, if the rowhas been forwarded (forwarded record), SQL Server might have to perform a second logical IO to read the forwarded row (asecond logical IO).

    his data access strategy is efficient if the number of rows returned is small. However, if the nonclustered index scan returns a largeumber of rows, there will be a lot of lookups. Lookups are random IOs, so they are very slow.

    he query optimizer will rather choose a full table scan (clustered index scan or table scan) if the number of rows exceeds fewercents of the whole table. More logical IO will be performed (more pages read), but they will be sequential IOs, so they will bester than random IOs.

    overing nonclustered index seeks

    hen a nonclustered index seek is performed, if all the columns needed by the query are stored in the nonclustered index (columnsf the nonclustered key for instance), the additional lookups wont be needed.

    he cost of that operation is just a single index seek. It is a very fast operation. the case of a query returning a large number of rows, this strategy is much more efficient than a full table scan or a nonclustereddex scan + lookup.

    overing nonclustered index scan

    hen SQL Server cannot identify any seek predicate for any index in a query, the full table scan is not always the only option. If aonclustered index contains all the columns needed by the query, SQL Server will choose to scan this nonclustered index rather thane table.

    onclustered indexes contain a subset of the columns of the table, so a row in a nonclustered index will be smaller than a row in theble, so every leaf page of the nonclustered index will contain more rows than a leaf page in the table structure, containing all the

    olumns. Scanning the entire leaf level of a nonclustered index will require less logical IOs than scanning the entire level of the tableructure. Therefore, it is faster to scan a nonclustered index than the table (heap of clustered table), but it can only be done if aonclustered index is covering the query.

    ubli mercredi 7 janvier 2009 11:58 parRaptorXPass sous :Dveloppement,SQL Server

    Ce post vous a plu ? Ajoutez le dans vos favoris pour ne pas perdre de temps le retrouver le jour o vous en aurez besoin :

    ommentaires

    as de commentaireses commentaires anonymes sont dsactivs

  • 7/27/2019 Data Access Strategies , Code is Poetry

    2/2

    Page 2L Server: Data access strategies , Code is poetry

    11.5.2010 11:59:48//blogs.developpeur.org/raptorxp/pages/sql-server-data-access-strategies.aspx

    es 10 derniers blogs posts

    Personnaliser les colonnes dans les vues des files dattente CRM 4.0parBiancale il y a 1 heure et 11 minutes

    MIC Appel projet Multi-TouchparLe Blog (Vert) d'Arnaud JUNDle il y a 1 heure et 33 minutes

    Office et SharePoint 2010 Gestion du cache local parLe Blog (Vert) d'Arnaud JUNDle il y a 1 heure et 48 minutes

    [OData] Le OData RoadShow dbarque Paris le 17 juin 2010 !parBlog Technique d'Audrey PETITle il y a 13 heures et 7inutes

    SYNCHRONISATIONS DE DONNEES AVEC LA REPLICATION DE FUSION (PARTIE 1)parLe Blog de Pi-R (Pierre Cambier)ley a 18 heures et 34 minutes

    Inclure les IEnumerable dans les requtes LINQ To EntitiesparMatthieu MEZILle 05-10-2010, 01:10

    Add-In Reflector : PowerCommandsparCoqBlogle 05-09-2010, 14:05

    XBOX 360 : Lire le contenu de son iPhone et iPod parBlog Technique de Romelard Fabricele 05-09-2010, 13:52

    Diverses lectures intressantesparCoqBlogle 05-09-2010, 11:59

    PRISM / Composite Application for Silverlight pour WP7parNicolas Humannle 05-08-2010, 22:13

    CodeS-SourceS (c) , Les crits sur ces blogs n'appartiennent qu'a leurs auteurs respectifs