Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Nakul Vachhrajani is a Technical Specialist & Systems development professional with iGATE. He holds a MCTS (SQL Server 2008: Implementation & Maintenance)
Browse by Tags · View All
#SQLServer 232
SQL Server 231
Administration 198
DBA 187
Tips 176
Development 176
T-SQL 171
#TSQL 169
Guidance 113
Tools and Utilities 112

Archive · View All
April 2011 14
March 2012 11
December 2011 11
March 2011 11
December 2012 10
October 2011 10
January 2011 10
January 2013 9
November 2012 9
October 2012 9

#0186-SQL Server-Quiz-Locks during Index maintenance (REBUILD/REORGANIZE)? Prize: Two (2) Pluralsight training codes!

Aug 13 2012 12:00AM by Nakul Vachhrajani   

What is the first word that comes to mind whenever one mentions the need for optimizing search or for increasing the performance of a database? Index.

Indexes play a vital role in SQL Server performance tuning, but they too need periodic maintenance. An index is logically implemented as a B-tree.

Over time as data is accumulated by the table, fragmentation may set in and the distribution of data within the index may become unbalanced, with some pages becoming full (as governed by the fill factor) leading to “page splits” whereas others might be sparsely filled. If data is deleted from the table, it leaves behind a “hole” in the index page – this free space is not reclaimed, which contributes to the bloating of the index. This reduces the efficiency of the indexes, while possibly consuming more than required disk space. At this stage, administrators need to perform maintenance operations on the index.

Index Maintenance operations can be classified into two:

  1. Index Rebuild
    • During a rebuild, the entire index (from the root to the leaf nodes) is rebuilt using the same columns, index type, uniqueness attribute, and sort order
    • The rebuild of an index effectively recreates the entire B-tree structure
  2. Index Reorganize
    • Index reorganization, on the other hand, only removes fragmentation at the leaf level
    • Intermediate-level pages and the root page are not defragmented

From an availability perspective, Reorganization is better than a rebuild, because reorganization is always an ONLINE process, i.e. it acquires only short-term locks whereas REBUILD prevents any changes to the underlying table until the operation completes, i.e. it acquires long-term locks. The question therefore is:

What types of locks do index REBUILD and index REORGANIZE operations occupy on the underlying table, by default (i.e. the “ONLINE” option is not specified)?

Bonus Exercise

As a bonus exercise, you may want to share any queries or tests that you may have conducted to arrive at the answer to the question above. Please note that the bonus exercise does not carry any rewards this time around – it is only as an exercise for the brain cells.

Rules

The rules are very simple:

  1. You can answer these questions till Wednesday 22:00hrs (IST)/Wednesday 12:30hrs (US-EDT) – August 15, 2012
  2. Answers will be shared and winners declared in my post on Thursday, August 16, 2012
  3. You can answer multiple times – however, at the end of the day, only one entry would be considered
  4. Follow the following on Twitter!
  5. Winners will be chosen randomly
  6. Prizes will be given out through Twitter – hence, it is important that you follow me Smile

Remember: Participation is always more important! Make sure you reply to the quiz even if it's just a guess!

Until we meet next time,

Be courteous. Drive responsibly.

Tags: #SQLServer, SQL Server, Administration, Development, DBA, T-SQL, #TSQL, Community


Nakul Vachhrajani
4 · 33% · 10564
3
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

10  Comments  

  • If ONLINE option is not specified, when a non-clustered index is rebuilt, a shared table lock occupies the table, preventing all but SELECT operations. When a clustered index is rebuilt, an exclusive table lock occupies the table, preventing any table access to the table.

    Index REORGANIZE doesn’t lock the table and it’s always online operation. This operation puts some temporary locks on the pages it works with, but they are removed as soon as the operation is complete.

    commented on Aug 13 2012 12:39AM
    Olga Medvedeva
    66 · 3% · 843
  • Same as above

    commented on Aug 13 2012 1:22AM
    Nirav Gandhi
    38 · 5% · 1503
  • REBUILD prevents any changes to the underlying table until the operation completes

    No, this is not always true. When ONLINE = ON (Available with Enterprise Edition), "Long-term table locks are not held for the duration of the index operation. During the main phase of the index operation, only an Intent Share (IS) lock is held on the source table. This allows queries or updates to the underlying table and indexes to continue"

    http://msdn.microsoft.com/en-us/library/ms188388.aspx

    From an availability perspective, Reorganization is better than a rebuild, because reorganization is always an ONLINE process, i.e. it acquires only short-term locks whereas REBUILD prevents any changes to the underlying table until the operation completes, i.e. it acquires long-term locks

    This is simply not true. From an availability perspective, if an index is fragmented more than 30%, REBUILD ONLINE = ON is the better solution. It will take less time and fewer server resources than doing a REORGANIZE.

    commented on Aug 13 2012 8:01AM
    Marc Jellinek
    97 · 2% · 545
  • @Marc: The whole question has an assumption that we are dealing with "default" behaviour, i.e. for REBUILDs, ONLINE = OFF; for REORGANIZE, ONLINE is always ON.

    commented on Aug 13 2012 8:40AM
    Nakul Vachhrajani
    4 · 33% · 10564
  • The statement "if an index is fragmented more than 30%, REBUILD is the better solution" stands. You'll have to schedule a maintenance window as the underlying table will be unavailable. If this requires an unacceptable period of time, this is your motivation and cost-justification to move to Enterprise Edition, where REBUILD ONLINE=ON is available.

    When one makes statements that include "better" or "best", it makes sense to qualify that with WHY one is "better" or "best". If I need something that happens quickly, then REBUILD is better than REORGANIZE, given a level of fragmentation.

    If I need something that allows users to continue to submit queries, then REORGANIZE or REBUILD ONLINE = ON is better.

    commented on Aug 13 2012 9:01AM
    Marc Jellinek
    97 · 2% · 545
  • While Rebuilding is index table is in S Lock and Sch-M Lock Pages of the table are in X Lock mode But why we can not access table if table is in S Lock mode , index is created drop and created for rebuild purpose so All pages are created Again and IAM Pages is been put on XMode

    While Reorganize index Table is in IX Lock mode Pages of the table in X Lock Mode here index is not drop and recreated so pages are the same but it places gradually X Mode and Release that page from X Mode as it has been reorganized and then it took other page to reorganize and put it in XMode. so here only Those Pages are locked and release which are moved

    sorry for could not attaching script cause it will take really long to make a script that can be nice

    But one can use sys.dmtranlocks to see what is going on.

    commented on Aug 13 2012 11:32AM
    Gaurang Patel
    237 · 1% · 189
  • As per my knowledge, For clustered as well as non-clustered index, When the Total Fragmentation exceeds 40% or more, it required index Rebuild and when u start this operation an exclusive lock apply on the entire table which prevents any changes to the table until Index rebuild complete as it recreates the entire index. If the Total Fragmentation is less than 40% then Index Reorganize applied. This will apply shared lock to the table. Personally i would go with Index Reorganize as my database is frequently updated.

    commented on Aug 14 2012 12:46PM
    Krishnrajsinh Rana
    136 · 1% · 360
  • @Krish: 30% vs. 40%.... whatever. It's a minor difference.

    Is your database updated constantly throughout the 24 hour day? You have absolutely no maintenance windows at all? And unlimited transaction log space?

    commented on Aug 14 2012 3:44PM
    Marc Jellinek
    97 · 2% · 545
  • Below is the (incomplete) list of locks required to run a REORGANIZE on a Non Clustered index. Apart from the locks listed, there may be other METADATA lock requirement based on the security context of the executing user and features like Auditing and triggers.

    1. IS Lock on the Table
    2. Sch-S Lock on the Clustered Index
    3. Sch-S lock on the NC Index
    4. Sch-S lock on the statistics on th NC Index
    5. IX Lock on the table
    6. S Lock on the table
    7. X lock on the HOBT
    8. U lock on the Extents
    9. X lock on the pages
    10. X lock on the extents
    11. S lock on the keys

    Some these locks locks may be acquired and released several times.

    The REORGANIZE process can be blocked by another process, participate in a blocking chain and block other processes, participate in a deadlock as a victim or winning process.

    The key point about REORGANIZE is that it only acquires "short term" locks and works on a page by page manner. So the chances of other processes getting blocked by the REORGANIZE thread for a longer duration is much less.

    The REBUILD process acquires a Sch-M lock in the beginning and keep it till the end, thus making the table inaccessible for other queries. Teh rEBUILD process also requires all the locks mentioned above pluse few additional METADATA locks.

    Regards

    Roji Thomas

    http://sqlindian.com

    commented on Aug 14 2012 6:08PM
    Roji Thomas
    791 · 0% · 36
  • ya its right.If we not specify ONLINE=ON by default SQL server lock table in the rebuilding index operation.

    While in Reorganize process database is online.

    IF fragmentation is more than 30% we need to rebuild the index otherwise reorganize is better solution.

    I do not have much idea about types of table lock is there good article available on table locks?

    commented on Mar 21 2013 1:41AM
    riks
    988 · 0% · 26

Your Comment


Sign Up or Login to post a comment.

"#0186-SQL Server-Quiz-Locks during Index maintenance (REBUILD/REORGANIZE)? Prize: Two (2) Pluralsight training codes!" rated 5 out of 5 by 3 readers
#0186-SQL Server-Quiz-Locks during Index maintenance (REBUILD/REORGANIZE)? Prize: Two (2) Pluralsight training codes! , 5.0 out of 5 based on 3 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]