<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1619266374223964354</id><updated>2012-01-20T15:37:49.794-08:00</updated><category term='SQL 2k8'/><category term='Indexing'/><category term='Storage Engine'/><category term='SQL Security'/><category term='Announcement'/><category term='Best Practices'/><category term='Performance Monitoring'/><category term='Errors'/><category term='Scripts'/><category term='Links'/><category term='T SQL'/><category term='Administration'/><title type='text'>SQL and SQL only</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>86</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-7739015807072772019</id><published>2012-01-13T00:37:00.000-08:00</published><updated>2012-01-13T01:20:12.229-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><title type='text'>File Group Backups - Intro</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; What is File Group backup? &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Backing up a portion of a database, say a File Group is termed as Filegroup backup.&lt;br /&gt;If you are wondering what are filegroups, then in short a Database's data files can be made of multiple files or groups of files. For more info on File Groups read &lt;a href ="http://msdn.microsoft.com/en-us/library/ms179316.aspx"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; When File Group backups are useful ?&lt;/b&gt;&lt;br /&gt;Assume you have very large database with few hundred GBs or a few Terabytes. The database is divided into multiple filegroups, with recently loaded data in one file group and older data in other filegroups. For example, you have a database which maintains a shop's order/transaction details. Assume that the database is designed to have each year's transaction at one filegroup. Then, instead of backing up the entire database, it would save lot of disk space, if one backup's up the current year's file group alone.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; How to take file group backups ? &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The Screenshot shows how to take file group backup. Fairly straight forward.&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/-XlZua3peVmU/Tw_xswwMHCI/AAAAAAAAAcM/kRIE7ugzX7I/s1600/filegroup%2Bbackup.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 286px;" src="http://1.bp.blogspot.com/-XlZua3peVmU/Tw_xswwMHCI/AAAAAAAAAcM/kRIE7ugzX7I/s400/filegroup%2Bbackup.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5697037804950527010" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; What are the advantages of file group backups ? &lt;/b&gt;&lt;br /&gt;1) Saves lot of space as you backup only a portion of the backup.&lt;br /&gt;2) Can bring the database online partially and at a faster pace. You can restore only your highest priority filegroup first, bring it online while filegroups havent been restored.&lt;br /&gt;3) If a table or particular filegroup is corrupted then One can restore the filegroup seperately.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Requirements &lt;/b&gt;&lt;br /&gt;Any backup strategy is said to work only when one can successfully recover the database. With filegroup backups, there is one basic principle. Each filegroups that is online should be consistent with the rest of the filegroups in the database. Also, the primary filegroup should be restored first for the database to be partially online.&lt;br /&gt;&lt;br /&gt;To explain bit more, assume you have a database 'DB' with filegroups FG1,FG2,FG3. All are read write file groups.FG1 is the primary file group.You can bring the database online partially with either of these &lt;br /&gt;&lt;br /&gt;  * File group backups of FG1 alone &lt;br /&gt;  * File group backups of FG1 + FG2 &lt;br /&gt;  * File group backups of FG1 + FG3 &lt;br /&gt;  * File group backup of FG1 + FG2 + FG3 ( this becomes completely online )&lt;br /&gt;&lt;br /&gt;However one should note that FG2/FG3 backup set should have the same Restoration point as FG1. Restoration point is the time upto which backups where taken for a file/filegroup. &lt;br /&gt;&lt;br /&gt;Assume one has taken full backup of a database at 1 PM and transaction log backups at 2PM and 3PM. After that there were no backups taken for the database. Then the restoration point is termed to be 3PM. In other words, the time upto which you are restoring a backup is termed as restoration point. &lt;br /&gt;&lt;br /&gt;So in our case, one &lt;b&gt; CANT &lt;/b&gt; bring the database partially ( excluding FG1 alone ) online with &lt;br /&gt;&lt;br /&gt;* FG1 file group backup taken on 10th Jan 9 PM&lt;br /&gt;* FG2 file group backup taken on 9th Jan 9 PM&lt;br /&gt;* FG3 file group backup taken on 8th Jan 9 PM &lt;br /&gt;&lt;br /&gt;Attempts to restore with these 3 backups alone will fail as FG3 contains transactions upto 8th Jan night, FG2 upto 9th night and FG1 upto 10th night. &lt;br /&gt;&lt;br /&gt;What &lt;b&gt; CAN &lt;/b&gt; work is&lt;br /&gt;&lt;br /&gt;* FG1 file group backup taken on 10th Jan 9 PM&lt;br /&gt;* FG2 file group backup taken on 9th Jan 9 PM&lt;br /&gt;* FG3 file group backup taken on 8th Jan 9 PM &lt;br /&gt;* &lt;b&gt; Additional T-Log backups from 8th Jan 9 PM to 10th Jan 9 PM. &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;T-Log backups from 8th Jan 9 PM to 10th Jan 9 PM contain all the transactions till &lt;br /&gt;10th Jan 9 PM and upon restoration we can bring FG1,FG2,FG3 to the same restoration point.&lt;br /&gt;&lt;br /&gt;In short the two most important principles for filegroup backups are&lt;br /&gt;&lt;br /&gt;1) Primary Filegroup should be restored first.&lt;br /&gt;2) All the Filegroups should have the same restoration point. &lt;br /&gt;&lt;br /&gt;The table below shows the recovery models and Modes at which filegroup backups are useful. &lt;table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="400" cellpadding="3" cellspacing="3"&gt;&lt;tr&gt;&lt;td&gt;&lt;b&gt;Recover model&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;Read only&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;Strategy&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Full&lt;/td&gt;&lt;br /&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Full FG backups + Differential + T-log backups&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Simple&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;td&gt;Doesn't work&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Full&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Full FG backups for Read write + T-Log backups&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Simple&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;td&gt;Full FG backups&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;/table&gt;&lt;br /&gt;On the upcoming posts, I will be explaining various backup strategies and restoration scenarios in detail.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-7739015807072772019?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/7739015807072772019/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=7739015807072772019&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7739015807072772019'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7739015807072772019'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2012/01/file-group-backups-intro.html' title='File Group Backups - Intro'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-XlZua3peVmU/Tw_xswwMHCI/AAAAAAAAAcM/kRIE7ugzX7I/s72-c/filegroup%2Bbackup.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-3657423585475930277</id><published>2011-11-01T02:47:00.000-07:00</published><updated>2011-11-01T03:17:51.675-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><title type='text'>Log file size after backup and restore</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Quick summary of the post: &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;A restoration of a full database backup retains the log file size before restoration.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Now for the details : &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Consider a large database that you want to move from one server to another. Assume that the log file of the source database is huge. For taking a backup, the size of the log file doesn't matter as a backup operation always backs up only the used pages of database.After restoration, the restored database's log file size is same as the original database, even though the backup file used for restoration &lt;br /&gt;is much smaller. If one has a space constraint in the destination server, then its better to shrink the log before taking the backup of the original &lt;br /&gt;database. &lt;br /&gt;&lt;br /&gt;Let us take the sample database &lt;b&gt; dbadb &lt;/b&gt;. The database size is 107 MB with data file size being 16 MB and log file size being 91 MB. A full backup file size is only 3.2 MB&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Database size &lt;/b&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/-6rBuBi1s_mc/Tq_FAKfW6vI/AAAAAAAAAbc/qILn3R4ecvI/s1600/db_size_before_restore.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 359px;" src="http://4.bp.blogspot.com/-6rBuBi1s_mc/Tq_FAKfW6vI/AAAAAAAAAbc/qILn3R4ecvI/s400/db_size_before_restore.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5669967062489295602" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Full Backup size &lt;/b&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/-WElcBzhjb74/Tq_FYIa0HSI/AAAAAAAAAbo/HkABW81YcM0/s1600/full_backup_size.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 293px; height: 400px;" src="http://2.bp.blogspot.com/-WElcBzhjb74/Tq_FYIa0HSI/AAAAAAAAAbo/HkABW81YcM0/s400/full_backup_size.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5669967474250226978" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;A restore of the backup will create a database again at the original size of 107 MB with data and log file sizes being 16 MB and 91 MB respectively. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Restore of Database &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/-P_X_ZjOIGqM/Tq_FrhFnkiI/AAAAAAAAAb0/bUOlIf-yJ7A/s1600/restore.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 359px;" src="http://3.bp.blogspot.com/-P_X_ZjOIGqM/Tq_FrhFnkiI/AAAAAAAAAb0/bUOlIf-yJ7A/s400/restore.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5669967807289725474" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Database size of restored database &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/-jtEBU403Ghg/Tq_G2ot2WZI/AAAAAAAAAcA/g3PlBhpISHY/s1600/size_after_restore.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 359px;" src="http://2.bp.blogspot.com/-jtEBU403Ghg/Tq_G2ot2WZI/AAAAAAAAAcA/g3PlBhpISHY/s400/size_after_restore.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5669969097827703186" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;So, if your destination server doesn't have enough space, then ensure your log file size is small before taking the full backup.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-3657423585475930277?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/3657423585475930277/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=3657423585475930277&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3657423585475930277'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3657423585475930277'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2011/11/log-file-size-after-backup-and-restore.html' title='Log file size after backup and restore'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-6rBuBi1s_mc/Tq_FAKfW6vI/AAAAAAAAAbc/qILn3R4ecvI/s72-c/db_size_before_restore.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-6511575326117704653</id><published>2011-10-04T01:16:00.000-07:00</published><updated>2011-10-04T01:53:06.605-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><title type='text'>Backup/Restore vs Detach &amp; Attach</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;You want to move a database from one server to another. There are two options to do that.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;1) Detach/Attach: &lt;/b&gt;To Detach the database from source server and copy the Data and log file ( MDF and LDF ) of the database and attach it in the destination server&lt;br /&gt;&lt;br /&gt;&lt;b&gt;2) Backup/Restore: &lt;/b&gt;Perform a SQL Backup of the database and move the&lt;br /&gt;backup file to destination server and Restore the database in destination server.&lt;br /&gt;A simple comparison of both the methods. &lt;br /&gt;&lt;table align="center" border="3"&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;br /&gt;&lt;p align="center"&gt;&lt;b&gt;Detach/Attach&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p align="center"&gt;&lt;b&gt;Backup and Restore&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;td&gt;&lt;p align="center"&gt;Detach/Attach is a offline operation. Source database will be inactive when you perform detach and attach operation&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p align="center"&gt;Source database can be accessed as it is a online operation&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;p align="center"&gt;Detaching and Attaching the database happens instantly irrespective of the size of the database.Time taken in migrating the database is same as the time taken for copying the data and log files from one server to another &lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p align="center"&gt;Backing up a database can take considerabale amount of time ranging from few minutes to many hours depending upon the database. Restore of a database on a average takes about 3 times of backup time. So, total time taken in migrating the database would be backup time + restore time + time taken to move the backup file from one server to another&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;&lt;p align="center"&gt;Sometimes, if the Log file size is huge then one needs to copy large amount of data over the network &lt;/p&gt;&lt;/td&gt;&lt;div align="center"&gt;&lt;/div&gt;&lt;td&gt;&lt;br /&gt;&lt;p align="center"&gt;Backup file contains only used data pages in data file and hence the size of the backup file would not include the size of data log file&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr&gt;&lt;td&gt;&lt;p align="center"&gt;Sometimes, if the Log file size is huge then one needs to copy large amount of data over the network &lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p align="center"&gt;Backup file contains only used data pages in data file and hence the size of the backup file would not include the size of data log file&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;div align="center"&gt;&lt;br /&gt;&lt;/div&gt;&lt;tr&gt;&lt;td&gt;&lt;p align="center"&gt;If the data file is fragmented/ If the Data file has lots of free space with in the file ( allocated but unused ), then copying the data file would mean carrying additional bytes of data though the they are unused.&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p align="center"&gt;Backup copies only used data pages of the data file. So, the size of the backup is always close to the size of the size used with in the data file.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;p align="center"&gt;Detach / Attach is not recorded in any table in msdb database. so one has no record of who detached/atatched who or when was it done, what were the files detached, what size or where the files are stored etc. &lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;p align="center"&gt;Backup and restore operation details are always stored in msdb database tables with information like size,date,location,type of backup / restore. &lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;&lt;p align="center"&gt;Advanced options like mirrored backup/ partial backup/ compressed backup/ backup to tape/ point in time recovery are not available&lt;/p&gt;&lt;/td&gt;&lt;td&gt;&lt;br /&gt;&lt;p align="center"&gt;All advanced options are available &lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br&gt;&lt;br&gt;Detach and attach method is useful when one wants move the database fast, without caring much about the availaiblity of source server.Backup and Restore is certainly a much graceful way to do the same.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-6511575326117704653?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/6511575326117704653/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=6511575326117704653&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/6511575326117704653'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/6511575326117704653'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2011/10/backuprestore-vs-detach-attach.html' title='Backup/Restore vs Detach &amp; Attach'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-5907412553342133981</id><published>2011-08-25T02:17:00.000-07:00</published><updated>2011-08-25T02:18:55.117-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Scripts'/><title type='text'>Reading SQL Error log</title><content type='html'>&lt;p&gt;&lt;br /&gt;SQL Error log can be read from SQL Server's management studio. However, Management studio is too slow and definitely not the greatest way of taking a quick look at SQL Error log. Sp_readerrorlog is definitely a much better command which can help us read a error log must faster way. Also , the  script below Dumps the read error log into a temporary table. Once dumped one can use different kinds of filters as per our needs.&lt;br /&gt;&lt;br /&gt;The script below loads error log into a temporary table, filters for a particular date range, removes error log entries for backup, and searches only for genuine error on the error log.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#error_log_dt&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;logdate&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;DATETIME&lt;/i&gt;&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;processinfo&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;30&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;text_data&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "blue"&gt;MAX&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#error_log_dt&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Sp_readerrorlog&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;#error_log_dt&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;logdate&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;BETWEEN&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'20110815'&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AND&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'20110821'&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AND&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;processinfo&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;!=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'backup'&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AND&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;text_data&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;LIKE&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'%error%'&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DROP&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#error_log_dt&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-5907412553342133981?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/5907412553342133981/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=5907412553342133981&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/5907412553342133981'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/5907412553342133981'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2011/08/reading-sql-error-log.html' title='Reading SQL Error log'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-4877227200175662223</id><published>2011-06-25T20:22:00.000-07:00</published><updated>2011-06-26T18:59:08.304-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='T SQL'/><title type='text'>ALTER TABLE - Adding Column - Column Order Impact</title><content type='html'>&lt;p&gt;&lt;br /&gt;Most of us would have altered a table structre by adding a column to the table. When an ALTER table script is used to add the column to the table, the column is placed on the last of the table. For inserting a column, in the middle of the table one needs to use the SQL Server Management Studio ( SSMS ).ie., Right click on the table and pick design table and then proceed to add a column. This post will deal with the significance of column order while adding a column to the table.&lt;br /&gt;&lt;br /&gt;Consider the table 'sample'. The table contains about 800 rows with a size of 6 MB.  Relatively small by a normal database standards.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/-e-aveoq0BGc/TgaqQEWw-mI/AAAAAAAAAaM/uYVNxtWjasU/s1600/table_size_before.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 311px;" src="http://1.bp.blogspot.com/-e-aveoq0BGc/TgaqQEWw-mI/AAAAAAAAAaM/uYVNxtWjasU/s400/table_size_before.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5622368377842498146" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Let us add column in the middle using SSMS as shown below. Once we click on save button to save the column addition, the operation completes immediateley.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/-ZGFA7cM5Y9o/TgarHy4LdHI/AAAAAAAAAaU/aWJuvTq4K5c/s1600/insert_column.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 320px;" src="http://3.bp.blogspot.com/-ZGFA7cM5Y9o/TgarHy4LdHI/AAAAAAAAAaU/aWJuvTq4K5c/s400/insert_column.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5622369335223481458" /&gt;&lt;/a&gt;&lt;br /&gt;Let me add a few more rows into the table. Now the table contains over 200K rows and the size of the table is 1.6 GB.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/-6xTI1yOzXkc/TgfYmouHzRI/AAAAAAAAAas/p4VvUsl1pRw/s1600/table_size_after.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 312px; height: 400px;" src="http://3.bp.blogspot.com/-6xTI1yOzXkc/TgfYmouHzRI/AAAAAAAAAas/p4VvUsl1pRw/s400/table_size_after.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5622700818072849682" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Now let me add a column to the middle of the table using SSMS as done proviously. Now the operation takes much much longer. Just in case if you face timeout error refer &lt;a href ="http://strictlysql.blogspot.com/2011/06/altering-table-structure-ssms-timeout.html"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Now the operation takes few hours to complete and the new column will be inserted between two columns as shown below.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/-4SgZvYle39c/TgfXp59sTlI/AAAAAAAAAak/o72iq1RG8YE/s1600/inbtw_column_inserted.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 320px;" src="http://1.bp.blogspot.com/-4SgZvYle39c/TgfXp59sTlI/AAAAAAAAAak/o72iq1RG8YE/s400/inbtw_column_inserted.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5622699774729539154" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let us add one more column at the last of the table ( not in between the columns as doen previously). Refer to picture below.&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ALTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sample&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ADD&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;col3_int&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;CHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;5&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/-ekWyG0Kf_7s/TgfWkZgwBhI/AAAAAAAAAac/Bk9cJHjQCCE/s1600/last_column_inserted.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 276px;" src="http://3.bp.blogspot.com/-ekWyG0Kf_7s/TgfWkZgwBhI/AAAAAAAAAac/Bk9cJHjQCCE/s400/last_column_inserted.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5622698580607239698" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Now the operation takes in 0 seconds to complete. The points to note are provided below.&lt;br /&gt;&lt;br /&gt;* &lt;b&gt; When we add a column to the middle of the table using SSMS, when the number of rows are higher, it consumes a longer execution time and resource.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;  However, when the number of rows are lesser it doesnt consume much of time and CPU/IO resource. This aspect is to be handled carefully where a DBA can fall for the trap if overlooked.&lt;br /&gt;&lt;br /&gt;  Assume, DBA is planning to perform a small column addition on the production server. DBA has already tested in staging and it was over in few seconds. DBA assumes that the operation is going to take a few seconds in production and plans accordingly. If the number of rows are higher in production, the DBA can be taken for ride and it can be different ball game all together.&lt;br /&gt;&lt;br /&gt;  So lesson to be learnt is &lt;b&gt; Dont underestimate any table modification prepations  and make sure to check size/# of rows on production before deployment.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;* When the column was added to the end of the table ( without caring about position of the column ) using T-SQL script ( ALTER TABLE coomand ),it completed immediately without consuming much of time and resource, though the number of rows were very high.&lt;br /&gt;&lt;br /&gt;  Lesson learnt is &lt;b&gt; insertion of a column to the middle ( or rather between two other columns ) of the table should not be done, unless there is a strong reason to do so. If there is no strong reason, then always add the column to the end of the table using ALTER TABLE script as they take much much lesser time to execute. &lt;/b&gt;&lt;br /&gt; &lt;br /&gt;* Use scripts instead of SSMS GUI especially while performing table strucutre modifications or DDL operations.&lt;br /&gt;&lt;br /&gt;We will take a much closer look in the next post exploring why such a behavior is observed.&lt;br /&gt;&lt;br /&gt; &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-4877227200175662223?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/4877227200175662223/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=4877227200175662223&amp;isPopup=true' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/4877227200175662223'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/4877227200175662223'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2011/06/alter-table-adding-column-column-order.html' title='ALTER TABLE - Adding Column - Column Order Impact'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/-e-aveoq0BGc/TgaqQEWw-mI/AAAAAAAAAaM/uYVNxtWjasU/s72-c/table_size_before.GIF' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-6048663315635842643</id><published>2011-06-17T19:42:00.000-07:00</published><updated>2011-06-17T22:23:27.565-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Errors'/><title type='text'>Altering Table structure - SSMS - Timeout Expired</title><content type='html'>&lt;p&gt;&lt;br /&gt;Altering a table using SQL Server Management Studio ( SSMS ) can be done by right clicking on the table and by picking the  design table as shown below.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/-ysVDafyvCYI/TfwWl9WQ25I/AAAAAAAAAZk/rI33SKyJroA/s1600/insert_column.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 320px;" src="http://2.bp.blogspot.com/-ysVDafyvCYI/TfwWl9WQ25I/AAAAAAAAAZk/rI33SKyJroA/s400/insert_column.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5619391276430449554" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;While adding a column, especially for a huge table,  then management studio prompts saying changing the data can consume lot of resources and time as shown below.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/-hHKkB138liM/TfwXbOhWyOI/AAAAAAAAAZs/U1S560nelTw/s1600/insert_on_big_table_progress.JPG"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 320px;" src="http://1.bp.blogspot.com/-hHKkB138liM/TfwXbOhWyOI/AAAAAAAAAZs/U1S560nelTw/s400/insert_on_big_table_progress.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5619392191573444834" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;After clicking 'yes', if the alter table takes longer than 30 secs then the alter table fails with the error message &lt;b&gt; 'Time out expired' &lt;/b&gt; as shown below.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/-S2SHC3W8P1o/TfwYkG5PB8I/AAAAAAAAAZ0/cyfZLq_oPIA/s1600/timeout.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 380px; height: 400px;" src="http://2.bp.blogspot.com/-S2SHC3W8P1o/TfwYkG5PB8I/AAAAAAAAAZ0/cyfZLq_oPIA/s400/timeout.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5619393443656566722" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The error can be avoided by changing the default setting in SSMS as shown below. Goto &lt;b&gt; Tools-&gt;Options-&gt;Tables and Database Designer &lt;/b&gt; and set the option &lt;b&gt; Transaction time out after  to  1800 seconds from default 30 seconds &lt;/b&gt;. The default setting is shown below.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/-uLB8FsQRCZc/Tfw0ykomhwI/AAAAAAAAAZ8/DxrMEoaAI8w/s1600/timeout.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 380px; height: 400px;" src="http://3.bp.blogspot.com/-uLB8FsQRCZc/Tfw0ykomhwI/AAAAAAAAAZ8/DxrMEoaAI8w/s400/timeout.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5619424478483613442" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-6048663315635842643?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/6048663315635842643/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=6048663315635842643&amp;isPopup=true' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/6048663315635842643'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/6048663315635842643'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2011/06/altering-table-structure-ssms-timeout.html' title='Altering Table structure - SSMS - Timeout Expired'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-ysVDafyvCYI/TfwWl9WQ25I/AAAAAAAAAZk/rI33SKyJroA/s72-c/insert_column.GIF' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-1932899705234393520</id><published>2011-06-12T02:42:00.000-07:00</published><updated>2011-06-12T02:48:35.243-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Errors'/><title type='text'>Database Owner  - Error Message</title><content type='html'>&lt;p&gt;&lt;br /&gt;Sometimes, a simple task that as a DBA you do day in and day out, &lt;br /&gt;can throw wierd errors. &lt;br /&gt;&lt;br /&gt;You right click on a database and click on Properties to check &lt;br /&gt;the Database Size/ Database path. Suddenly, a error message as shown &lt;br /&gt;below is thrown at you.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/-HAwWc-lbQuI/TfSKdGBoerI/AAAAAAAAAZY/7-flmzgNtVU/s1600/error_msg.JPG"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 137px;" src="http://3.bp.blogspot.com/-HAwWc-lbQuI/TfSKdGBoerI/AAAAAAAAAZY/7-flmzgNtVU/s400/error_msg.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5617266867676150450" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The error message says the problem is with the database owner. When the database owner is removed ( perhaps the login was dropped ) , such a error message is thrown.The problem can be rectified by using the command&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;MASTER&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Sp_changedbowner&lt;/b&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'sa'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;The command sets database owner as dbo and solves the problem. Now we can comfortably view database property from SSMS as usual.&lt;br /&gt;&lt;br /&gt;Please note that sp_changedbowner command is marked for depcreation and users are recommended to use ALTER AUTHORIZATRION command.syntax is provided below.&lt;br /&gt;&lt;br /&gt;&lt;I&gt; ALTER AUTHORIZATION ON DATABASE::dbname TO principal; &lt;/I&gt;&lt;br /&gt;&lt;br /&gt;For more details on AUTHORIZATION refer &lt;a href = "http://msdn.microsoft.com/en-us/library/ms187359.aspx"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-1932899705234393520?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/1932899705234393520/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=1932899705234393520&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/1932899705234393520'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/1932899705234393520'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2011/06/database-owner-error-message.html' title='Database Owner  - Error Message'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/-HAwWc-lbQuI/TfSKdGBoerI/AAAAAAAAAZY/7-flmzgNtVU/s72-c/error_msg.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-7745966347551754108</id><published>2011-06-11T19:55:00.000-07:00</published><updated>2011-06-11T20:10:42.977-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Announcement'/><title type='text'>I am back</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;I am back again!!!. It has been ages since I posted.An important event in my life kept me away from blogging. Now I am happily married to Sharadha. :) Till date, I havent posted a photograph of mine in this blog. Today is certainly a good day to that.&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/-jfx7XSJYD2w/TfQtlouNkgI/AAAAAAAAAZQ/3RWF2F4QMts/s1600/IMG_0910.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 299px; height: 400px;" src="http://2.bp.blogspot.com/-jfx7XSJYD2w/TfQtlouNkgI/AAAAAAAAAZQ/3RWF2F4QMts/s400/IMG_0910.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5617164759847506434" /&gt;&lt;/a&gt;&lt;br /&gt; &lt;br /&gt;So, from now on the blog will be active as it was before and special thanks to all the readers for visiting even when it was not frequently updated. &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-7745966347551754108?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/7745966347551754108/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=7745966347551754108&amp;isPopup=true' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7745966347551754108'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7745966347551754108'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2011/06/i-am-back.html' title='I am back'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-jfx7XSJYD2w/TfQtlouNkgI/AAAAAAAAAZQ/3RWF2F4QMts/s72-c/IMG_0910.GIF' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-547645163417213582</id><published>2011-01-04T17:10:00.000-08:00</published><updated>2011-01-04T17:29:37.478-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Scripts'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><title type='text'>Finding last database growth date and time</title><content type='html'>&lt;p&gt;&lt;br /&gt;A file growth operation on a database server is a extremely expensive operation. Many Performance problems correlate to a Data or log file growth event on the database. As a result its extremley important for a DBA, to have a way to check the time of Data or Log file growth occured earlier. A good way of finding it will be thro SQL default trace. Refer to the following query&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@path&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;NVARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;1000&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@path&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Substring&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "blue"&gt;PATH&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Len&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "blue"&gt;PATH&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;-&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Charindex&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "red"&gt;'\'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Reverse&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "blue"&gt;PATH&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "red"&gt;'\log.trc'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;traces&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;databasename&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;e&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;name&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;eventname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cat&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;name&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[CategoryName]&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;starttime&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;e&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;category_id&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;loginname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;loginsid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;hostname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;applicationname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;servername&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;textdata&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;objectname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;eventclass&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;eventsubclass&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;::fn_trace_gettable&lt;/b&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "#8000FF"&gt;@path&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;0&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;INNER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;JOIN&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;trace_events&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;e&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;eventclass&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;trace_event_id&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;INNER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;JOIN&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;trace_categories&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;cat&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;e&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;category_id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;cat&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;category_id&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;e&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;name&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;IN&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'Data&amp;nbsp;File&amp;nbsp;Auto&amp;nbsp;Grow'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'Log&amp;nbsp;File&amp;nbsp;Auto&amp;nbsp;Grow'&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ORDER&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;starttime&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;DESC&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_TcYicum3HIs/TSPIEXG6p1I/AAAAAAAAAWM/lM3hljrf72I/s1600/file_growth.JPG"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 377px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/TSPIEXG6p1I/AAAAAAAAAWM/lM3hljrf72I/s400/file_growth.JPG" border="0" alt=""id="BLOGGER_PHOTO_ID_5558506342353381202" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The script provided above will list the databases that grew recently along with their time of growth. The script pulls these details from SQL Server''s default trace. Default trace is a light weight trace running at the background by default capturing many important events. Few of them are listed below.&lt;br /&gt;&lt;br /&gt;Data File Auto Grow&lt;br /&gt;Data File Auto Shrink&lt;br /&gt;Database Mirroring State Change&lt;br /&gt;ErrorLog&lt;br /&gt;Full text crawl related events&lt;br /&gt;Hash Warning&lt;br /&gt;Log File Auto Grow&lt;br /&gt;Log File Auto Shrink&lt;br /&gt;Missing Column Statistics&lt;br /&gt;Missing Join Predicate&lt;br /&gt;Object:Altered&lt;br /&gt;Object:Created&lt;br /&gt;Object:Deleted&lt;br /&gt;Plan Guide Unsuccessful&lt;br /&gt;Server Memory Change&lt;br /&gt;Sort Warnings&lt;br /&gt;&lt;br /&gt;SQL default trace can write upto 5 files with each file with a maximum size 0f 20 MB.&lt;br /&gt;So maximum log of 100 MB will be generated. So, the amount of time SQL Trace retains the log details depends on the amount of activity on the server.One can change the same script to view other events and get other useful info like 'who altered that object?' , 'Who granted the rights to a particular table' and so on. &lt;br /&gt;&lt;br /&gt;For more details on default trace refer &lt;a href = "http://www.eraofdata.com/blog/tag/sql-profiler/"&gt;&lt;b&gt;here&lt;/b&gt;&lt;/a&gt; and &lt;a href = "http://www.sql-server-performance.com/articles/dba/default_trace_intro_p1.aspx/"&gt;&lt;b&gt;here&lt;/b&gt;&lt;/a&gt; and &lt;a href = "http://www.sqlservercentral.com/articles/SQL+Server+2005/64547/"&gt;&lt;b&gt;here&lt;/b&gt;&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-547645163417213582?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/547645163417213582/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=547645163417213582&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/547645163417213582'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/547645163417213582'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2011/01/finding-last-database-growth-date-and.html' title='Finding last database growth date and time'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_TcYicum3HIs/TSPIEXG6p1I/AAAAAAAAAWM/lM3hljrf72I/s72-c/file_growth.JPG' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-2758848775165204433</id><published>2010-12-24T01:30:00.000-08:00</published><updated>2010-12-24T01:44:07.348-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><title type='text'>Server Side trace</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;Most of us are aware of tracing queries using SQL profiler. But an effective way of performing tracing is using a server side trace.&lt;br /&gt;&lt;br /&gt;Whats special about server side trace?&lt;br /&gt;&lt;br /&gt;1) Server Side trace uses much less resources than SQL Profiler.&lt;br /&gt;&lt;br /&gt;2) Server side trace runs within the server which means the dependency on client tool is removed.In other words, when you run a profiler from a client machine, once you close the profiler, the SQL trace on the server stops. for ex: if the DBA is running a trace on production server from his desktop, then if the DBA's machine is restarted then the trace automatically stops. With a Server side trace as long as server is up the trace will be running and it doesnt depend on any client machine or tool.&lt;br /&gt;&lt;br /&gt;3) SQL Profiler tool, while running, consumes lots of space on the C: drive ( or on the drive executables are installed ) by writing temporary files to C:\...\..\ temp folder. Temporoary files are cleared only by stopping the profiler, which would mean stopping the trace. There are enough articles on the net for Server Side traces. Please refer &lt;a href = "http://www.mssqltips.com/tip.asp?tip=1035"&gt;&lt;b&gt;here&lt;/b&gt;&lt;/a&gt; and &lt;a href = "http://sqlserverpedia.com/wiki/The_Server-side_Trace:_What,_Why,_and_How"&gt;&lt;b&gt;here&lt;/b&gt;&lt;/a&gt;. These links provide a fairly comprehensive expalantion on server side traces.&lt;br /&gt;&lt;br /&gt;In short they say&lt;br /&gt;&lt;br /&gt;1) configure the trace using profiler&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_TcYicum3HIs/TRRouyreM4I/AAAAAAAAAV0/3DQqpuhww3c/s1600/profiler_before.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 251px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/TRRouyreM4I/AAAAAAAAAV0/3DQqpuhww3c/s400/profiler_before.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5554179393542042498" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;2) Use the File and Export option on SQL Profiler to export the script of the trace.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_TcYicum3HIs/TRRpAa9U7BI/AAAAAAAAAV8/iryJwa3-1eA/s1600/profiler.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 256px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/TRRpAa9U7BI/AAAAAAAAAV8/iryJwa3-1eA/s400/profiler.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5554179696412126226" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;3) Make the change on &lt;b&gt; sp_trace_create&lt;/b&gt; parameter to 2 so that files roll over after the first trace file is filled up. If not the trace stops after the first trc file is full.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_TcYicum3HIs/TRRppHyMzWI/AAAAAAAAAWE/Gz3WoQvjiz4/s1600/trace.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 310px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/TRRppHyMzWI/AAAAAAAAAWE/Gz3WoQvjiz4/s400/trace.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5554180395639819618" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;4) Execute it from SSMS to get the server side trace started.&lt;br /&gt;&lt;br /&gt;5) Use the following functions to administer the trace&lt;br /&gt;&lt;br /&gt;&lt;b&gt; 1) fn_trace_getinfo(default) or select * from sys.traces &lt;/b&gt; - to check trace status&lt;br /&gt;&lt;b&gt; 2) sp_trace_setstatus &lt;/b&gt;  - To start, stop and close a thread&lt;br /&gt;&lt;b&gt; 3) fn_trace_gettable &lt;/b&gt; - To get the results of the trace files into a table.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-2758848775165204433?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/2758848775165204433/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=2758848775165204433&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/2758848775165204433'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/2758848775165204433'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/12/server-side-trace.html' title='Server Side trace'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_TcYicum3HIs/TRRouyreM4I/AAAAAAAAAV0/3DQqpuhww3c/s72-c/profiler_before.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-7898044546698492069</id><published>2010-12-08T18:17:00.000-08:00</published><updated>2010-12-08T18:19:56.949-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><category scheme='http://www.blogger.com/atom/ns#' term='Links'/><title type='text'>Perfmon counters list - Quest poster</title><content type='html'>&lt;p&gt; &lt;br /&gt;&lt;br /&gt;Stumbled upon &lt;a href = "http://www.quest.com/backstage/images/promotions/SQLServer-Perfmonance-Poster.pdf" &gt;this&lt;/a&gt; PDF from quest which lists the important Perfmon counters and their acceptable values. A must print and stick poster which can be very very handy indeed. Saves so much of time and effort involved in reading pages of documents to know the important counters and correct value for each of them. Superb stuff. Thanks Quest !!! &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-7898044546698492069?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/7898044546698492069/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=7898044546698492069&amp;isPopup=true' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7898044546698492069'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7898044546698492069'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/12/perfmon-counters-list-quest-poster.html' title='Perfmon counters list - Quest poster'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-3133920666740623785</id><published>2010-11-29T02:39:00.000-08:00</published><updated>2010-11-29T03:02:46.701-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL 2k8'/><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Storage Engine'/><title type='text'>Lock escalation : SQL Server 2008</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;Lock escalation is a event which occurs when SQL Server decides to upgrade a lock at a lower level hierarchy to a lock to a table level lock., In other words, when a particular query obtains a large number of row level locks/ page level locks, SQL Server decides that instead of creating and granting number of row level/page level locks, it is effective to grant a single table level lock. Or to be precise,&lt;br /&gt;SQL Server upgrades the row/page level locks to table level locks. The above process is termed as lock escalation.&lt;br /&gt;&lt;br /&gt;Lock escalation is good, as it reduces the overhead in maintaining a large number of smaller level locks. A lock structure is about occupies about 100 bytes of memory and too many locks can cause a memory pressure.Similarly applying,granting , and releasing locks for each row or page is a resource consuming processes which can be reduced by lock escalation. However, Lock escalation also reduces concurrency. ie, If a query causes lock escalation, then the query obtains a full table level lock, and another query attempting to access the table will have to wait till the first query releases the lock.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; How SQL Server decides when to escalate lock ? &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; * When a query consumes more than 5000 locks per index / heap. &lt;/b&gt;&lt;br /&gt;&lt;b&gt; * When the lock monitor consumes more than 40% of the static memory or non AWE alloted memory. &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;So Let us quickly see lock escalation in action. Consider the following query &lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;ROWCOUNT&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;4990&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TRAN&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;UPDATE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;orders&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;order_description&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;order_description&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'&amp;nbsp;'&lt;/font&gt;&lt;br /&gt;&lt;font color = "green"&gt;&lt;i&gt;&amp;#45;&amp;#45;Rollback&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TcYicum3HIs/TPOD1am5fQI/AAAAAAAAAVE/Sd5DjYW4NGQ/s1600/lock_escalation_1st.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 277px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/TPOD1am5fQI/AAAAAAAAAVE/Sd5DjYW4NGQ/s400/lock_escalation_1st.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5544920519921990914" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The orders table has a clustered index. Row level locks will be taken on the index keys. &lt;b&gt; SET ROWCOUNT &lt;/b&gt; ensures that only 4990 rows are updated by the query. I am leaving the transaction open ( without committing or rolling back ) , so that we can see the number of locks held by the query.&lt;br /&gt;&lt;br /&gt;Fire the following query to check the locks held by the above script. The query lists the count of locks for each lock type and object. Note that the session id for the above script on my machine was 53. So filtering by the same. &lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;COUNT&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;request_mode&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[resource_associated_entity_id]&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_tran_locks&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;resource_type&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;object_type&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Db_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;sysprocesses&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbid&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbname&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_tran_locks&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;sysprocesses&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;OUTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;APPLY&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Fn_get_sql&lt;/b&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;sql_handle&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;53&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AND&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_tran_locks&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;request_session_id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;53&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AND&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_tran_locks&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;resource_type&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;IN&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'page'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'key'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'object'&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AND&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Db_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;sysprocesses&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbid&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'dbadb'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GROUP&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[resource_associated_entity_id]&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;request_mode&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_tran_locks&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;resource_type&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Db_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;sysprocesses&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbid&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_TcYicum3HIs/TPOEqONhmQI/AAAAAAAAAVU/8iGSeOiteWQ/s1600/row_level_locks.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 283px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/TPOEqONhmQI/AAAAAAAAAVU/8iGSeOiteWQ/s400/row_level_locks.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5544921427127408898" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;As one may notice, we can find 4990 key locks / row level locks. Let us rollback transaction and modify the script to use 5000 or more locks.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;ROWCOUNT&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;5000&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TRAN&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;UPDATE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;orders&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;order_description&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;order_description&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'&amp;nbsp;'&lt;/font&gt;&lt;br /&gt;&lt;font color = "green"&gt;&lt;i&gt;&amp;#45;&amp;#45;Rollback&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_TcYicum3HIs/TPOGiWFkmII/AAAAAAAAAVc/SeJuJASA8VM/s1600/table_level_lock.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 245px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/TPOGiWFkmII/AAAAAAAAAVc/SeJuJASA8VM/s400/table_level_lock.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5544923490825836674" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Now let us fire the same query on sys.dm_tran_locks. we obtain a single exclusive lock on the table/object. There are no key or row level locks as SQL Server as per its rule has escalated the row level locks to a table level lock.&lt;br /&gt;&lt;br /&gt;SQL Server 2005 had a server wide setting to disable lock escalations. On SQL Server 2005, when the trace flag 1211/1224 are set, no query is allowed to escalate locks on the entire server. Ideally, we would like to have it as a object/table level setting which was provided by SQL Server 2008. SQL Server 2008 allows one to disable lock escalations at table/ partition levels. &lt;br /&gt;&lt;br /&gt;Consider the following command in SQL 2k8&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ALTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;orders&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;CONSTRAINT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(LOCK_ESCALATION = DISABLE )&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;The ALTER TABLE command's LOCK_ESCALATION property accepts three values.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; * Disable &lt;/b&gt; -&gt; Disables lock escalation ( Except a few exceptions . Refer Books online for details ) &lt;br /&gt;&lt;b&gt; * Table &lt;/b&gt; -&gt; Allows SQL Server to escalate to table level. That is the default setting.&lt;br /&gt;&lt;b&gt; * Auto &lt;/b&gt; -&gt; Escalation will be partition level if the table is partitioned. Else escalation is always up to table level.&lt;br /&gt;&lt;br /&gt;Let us rollback the open transaction created earlier and run the ALTER TABLE command posted above to disable lock escalations. Now let us run the same script to update 5000 records again and see if lock escalation has actually occurred.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_TcYicum3HIs/TPOH7BwplnI/AAAAAAAAAVk/52LEWR42g3k/s1600/no_escalaltion.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 273px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/TPOH7BwplnI/AAAAAAAAAVk/52LEWR42g3k/s400/no_escalaltion.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5544925014377731698" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;As you may now notice, for the same 5000 rows, there is no lock escalation occuring this time as we have disabled it using the ALTER TABLE command. The picture shows 5000 key/row locks which is not possible at the default setting of lock escalation.&lt;br /&gt;&lt;br /&gt;The intention behind this post was to introduce lock escalation, show how it works and also explain the new option provided to change lock escalation setting SQL Server 2008. Upcoming posts, we will dive deeper into the topic and understand when and under what circumstances can we play with lock escalation setting.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-3133920666740623785?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/3133920666740623785/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=3133920666740623785&amp;isPopup=true' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3133920666740623785'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3133920666740623785'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/11/lock-escalation-sql-server-2008.html' title='Lock escalation : SQL Server 2008'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_TcYicum3HIs/TPOD1am5fQI/AAAAAAAAAVE/Sd5DjYW4NGQ/s72-c/lock_escalation_1st.GIF' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-3769820749010021365</id><published>2010-11-16T03:22:00.000-08:00</published><updated>2010-11-16T03:45:32.235-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL 2k8'/><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><title type='text'>Backup log Truncate_Only in SQL Server 2008</title><content type='html'>&lt;p&gt; &lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;BACKUP&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;LOG&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;&lt;/font&gt;&lt;font color = "maroon"&gt;db_name&lt;/font&gt;&lt;font color = "silver"&gt;&amp;gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;WITH&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;truncate_only&lt;/font&gt; &lt;/font&gt; command, used for clearing the log file, is deprecated in SQL Server 2008. So this post will explain option available in SQL Server 2008 for truncating the log.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Step 1:  Change the recovery model to Simple &lt;/b&gt; &lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;USE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[master]&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ALTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;DATABASE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbadb]&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;recovery&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;simple&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;WITH&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;no_wait&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;b&gt; Step 2: Issue a checkpoint &lt;/b&gt; &lt;br /&gt;One can issue a checkpoint using the following command.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CHECKPOINT&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;Checkpoint process writes all the dirty pages in the memory to disk. On a simple recovery mode, the checkpoint process clears the inactive portion of the transaction log.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Step 3: Shrink the log file &lt;/b&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;USE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbadb&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;shrinkfile&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;2&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;2&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;truncateonly&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;Shrinking the log file with a truncateonly option clears the unused space at the end of the log file. First parameter of the Shrinkfile takes the filed id within the database. Mostly the fileid of the log file is 2. You may verify the same by firing a query on sysfiles.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Step 4: Change the recovery model back to full/bulk logged &lt;/b&gt;&lt;br /&gt;Change the recovery model to the recovery model originally ( full/bulk logged ) used by the database.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;USE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[master]&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ALTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;DATABASE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbadb]&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;recovery&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;FULL&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;WITH&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;no_wait&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;After these steps the log file size should have reduced.&lt;br /&gt;&lt;br /&gt;The intention behind this post is not to encourage truncating the log files. Use the method explained, only when you are running short of disk space because of a log file growth. Note that, Just like truncating log files, Changing  the recovery model also disturbs the log chain. After clearing the log using the above method, you need to either run a full backup/Differential backup to keep your log chain intact for any recovery.&lt;br /&gt;&lt;br /&gt;Just a quick demo to show that the log chain breaks if you change the recovery model.&lt;br /&gt;&lt;br /&gt;The database whose log file we will be clearing is dbadb. Log file size 643 MB as shown below.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_TcYicum3HIs/TOJsLzKljII/AAAAAAAAAUY/2Su54cFqVYk/s1600/log_size_before.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 331px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/TOJsLzKljII/AAAAAAAAAUY/2Su54cFqVYk/s400/log_size_before.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5540109441588104322" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;After executing the scripts mentioned above, the log file size is 2 MB as shown below.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TcYicum3HIs/TOJscRP2nNI/AAAAAAAAAUg/CAps9sRn5gI/s1600/log_size_after.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 327px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/TOJscRP2nNI/AAAAAAAAAUg/CAps9sRn5gI/s400/log_size_after.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5540109724541164754" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The log chain breaks after changing the recovery model. When log chain breaks, subsequent transaction log backups start failing as shown below.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_TcYicum3HIs/TOJstf-tlfI/AAAAAAAAAUo/l-WE5eVTKjs/s1600/backup_error.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 350px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/TOJstf-tlfI/AAAAAAAAAUo/l-WE5eVTKjs/s400/backup_error.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5540110020553577970" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Transaction log backups will be successful only after the execution of full or differential backup.&lt;br /&gt;&lt;br /&gt;PS: Pardon me for a SQL 2k8 post, when the whole world is going crazy about&lt;br /&gt;&lt;a href = "http://www.microsoft.com/downloads/en/details.aspx?FamilyID=6a04f16f-f6be-4f92-9c92-f7e5677d91f9&amp;displaylang=en"&gt;SQL Denali&lt;/a&gt; :) &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-3769820749010021365?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/3769820749010021365/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=3769820749010021365&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3769820749010021365'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3769820749010021365'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/11/backup-log-truncateonly-in-sql-server.html' title='Backup log Truncate_Only in SQL Server 2008'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_TcYicum3HIs/TOJsLzKljII/AAAAAAAAAUY/2Su54cFqVYk/s72-c/log_size_before.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-7014356252924028013</id><published>2010-11-09T15:57:00.000-08:00</published><updated>2010-11-09T16:23:48.780-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Scripts'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><title type='text'>Finding CPU Pressure - dm_os_schedulers DMV</title><content type='html'>&lt;p&gt;&lt;br /&gt;The last &lt;a href ="http://strictlysql.blogspot.com/2010/10/finding-cpu-pressure-using-waitstats.html"&gt;post&lt;/a&gt; dealt with checking CPU pressure using wait_stats DMV. But, to get a complete picture of CPU Pressure, the script provided in the previous post alone wouldn't suffice. We need the help of additional DMV&lt;br /&gt;&lt;b&gt; sys.dm_os_schedulers.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Why we need the help of sys.dm_os_schedulers? &lt;/b&gt;&lt;br /&gt;As already mentioned, Waitstats DMV captures waiting time for a group of wait types. WaitStats DMV works in the following way. WaitStats checks whether any processes waits at any of the defined wait types. If yes, then WaitStats tracks resource wait time experienced by the process. After the resource wait is over, waitstats tracks the signal wait/CPU wait. Once, both the waits are completed, the waitstats reflect in the DMV.&lt;br /&gt;&lt;br /&gt;There have been scenarios where a process doesn't experience any resource wait and just has a CPU wait/Signal wait. In such a case, the waiting time misses the wait stats, and wait stats doesn't reflect the CPU pressure experienced at all. In other words, if a process doesn't wait for any resource and directly gets &lt;br /&gt;into the CPU queue and spends long time only in runnable state, then &lt;br /&gt;sys.dm_os_wait_stats doesn't reflect it. The Same scenario is explained in detail by MVP Linchi Shea over &lt;a href = "http://sqlblog.com/blogs/linchi_shea/archive/2006/12/31/cpu-pressure-is-huge-but-where-is-the-wait.aspx"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;In such a scenario, one can use &lt;b&gt; sys.dm_os_schedulers &lt;/b&gt; to detect CPU pressure.&lt;br /&gt;Consider the following query.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;scheduler_id&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cpu_id&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;current_tasks_count&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;runnable_tasks_count&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;current_workers_count&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;active_workers_count&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;work_queue_count&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_os_schedulers&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;scheduler_id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;255&lt;/font&gt;&lt;font color = "silver"&gt;;&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;Simple and straight forward query on sys.dm_os_schedulers DMV. Returns one row per CPU. Scheduler_id &lt; 255 filters out schedulers used by system processes. Detailed explanation on the DMV can be obtained from &lt;a href = "http://msdn.microsoft.com/en-us/library/ms177526.aspx"&gt;here&lt;/a&gt;. Most important column is &lt;b&gt; runnable_tasks_count &lt;/b&gt; which indicates the number of tasks that are waiting&lt;br /&gt;on the runnable queue. Once a process moves out of the runnable state into running state, the value of the column reduces by one. So, the DMV always indicates the current state of the server and the runnable queue.dm_os_schedulers doesnt hold historical data since last restart unlike waitstats dmv. So, this needs to &lt;br /&gt;polled lot more frequently, perhaps every 5 minutes if necessary. Any non zero value noted on runnable_tasks_count is a indication of CPU pressure and requires close monitoring.&lt;br /&gt;&lt;br /&gt;To sum it up, CPU monitoring for SQL Server would include three parts.&lt;br /&gt;&lt;br /&gt; &lt;b&gt; * Directly checking CPU using sys.os_ring_buffers dmv as explained &lt;a href  ="http://strictlysql.blogspot.com/2010/06/finding-cpu-utilization-in-sql-server.html"&gt;here&lt;/a&gt;.&lt;br /&gt;     * Checking waitstats dmv as explained &lt;a href ="http://strictlysql.blogspot.com/2010/10/finding-cpu-pressure-using-waitstats.html"&gt;here&lt;/a&gt;&lt;br /&gt;     * checking sys.os_schedulers as explained in this post.&lt;br /&gt; &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-7014356252924028013?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/7014356252924028013/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=7014356252924028013&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7014356252924028013'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7014356252924028013'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/11/cpu-pressure-dmosschedulers-dmv.html' title='Finding CPU Pressure - dm_os_schedulers DMV'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-7991488082457505564</id><published>2010-10-31T02:25:00.000-07:00</published><updated>2010-10-31T04:34:52.822-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Scripts'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><title type='text'>Finding CPU Pressure using wait_stats DMV</title><content type='html'>&lt;p&gt;&lt;br /&gt;CPU Pressure forms vital part in performance monitoring and &lt;b&gt; sys.dm_os_wait_stats &lt;/b&gt; is a good place to check. Some background info before we look at the DMV.&lt;br /&gt;&lt;br /&gt;Waiting time for a SQL Process is divided into two. They are Resource waits and signal waits( CPU waits ). When a process/query is submitted to SQL Server,  SQL Server checks whether all resources required for the process are available.Resources would include things like IO, Network, Memory, Locks, Latch waits etc. For Ex: If a process is waiting for another process to release its lock, then it becomes a resource wait because of locks. If a process  wants to write something to a disk but its has to wait for some other IO activity to complete then its a resource wait because of IO. So all these waiting time gets categorized as resource waits..&lt;br /&gt;&lt;br /&gt;After a process grabs all the resources it requires, the process gets scheduled to run/executed by the Processor/ CPU. So, it enters the CPU queue. If there are already many processes executing, then it has to wait to get the CPU. The time spent waiting for the CPU is termed as signal waits. &lt;br /&gt;&lt;br /&gt;&lt;b&gt; sys.dm_os_wait_stats &lt;/b&gt; DMV provides the cumulative sum of the waiting time experienced by all processes at each wait type. &lt;b&gt; dm_os_wait_stats DMV &lt;/b&gt; is a SQL Server 2005/2008 equivalent of DBCC SQLPERF(waitstats). A lot more accurate explanation about the DMV is provided &lt;a href = "http://technet.microsoft.com/en-us/library/ms179984.aspx"&gt; here &lt;/a&gt;. dm_os_wait_stats maintains these stats since last restart of the server or since the last execution  &lt;b&gt; DBCC SQLPERF ('sys.dm_os_wait_stats', CLEAR); &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; sys.dm_os_wait_stats &lt;/b&gt; provides about 485 wait types/wait events and waiting time spent at each wait type. Two interesting columns of this DMV are &lt;br /&gt;   &lt;b&gt; &lt;br /&gt;    * wait_time_ms&lt;br /&gt;    * signal_wait_time_ms   &lt;br /&gt;   &lt;/b&gt;&lt;br /&gt;&lt;b&gt; Wait_time_ms &lt;/b&gt; gives the total time spent at each wait type ( Resource + cpu wait ). &lt;b&gt; signal_wait_time_ms &lt;/b&gt; specifies the time spent on waiting for CPU. &lt;b&gt; Resource waittime &lt;/b&gt; can be found by &lt;b&gt; wait_time_ms - signal_wait_time_ms&lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;SUM&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;signal_wait_time_ms&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;100&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;/&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;SUM&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;wait_time_ms&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cpu_pressure_percentage&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;SUM&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;signal_wait_time_ms&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;cpu_wait&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;SUM&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;wait_time_ms&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;-&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;signal_wait_time_ms&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;resource_wait&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;SUM&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;wait_time_ms&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;total_wait_time&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_os_wait_stats&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_TcYicum3HIs/TM07Pi8T0FI/AAAAAAAAAUA/cKnCztylUmw/s1600/wait_stats.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 206px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/TM07Pi8T0FI/AAAAAAAAAUA/cKnCztylUmw/s400/wait_stats.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5534144655372046418" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Consider the query above. &lt;b&gt; cpu_pressure_percentage &lt;/b&gt; provides percentage of time spent waiting for CPU, by finding the ratio of signal_time and total wait time. Microsoft recommends this value to be &lt;b&gt; less than 25% &lt;/b&gt; for a normal system. Value any higher would indicate a system suffering likely from a CPU stress.&lt;br /&gt;Most common causes for CPU stress/high signal waits can be&lt;br /&gt;&lt;br /&gt;               * Poor and ineffective query plans / missing indexes / table scans&lt;br /&gt;               * Too many Plan Recompliations&lt;br /&gt;               * Slow CPU and requires a upgrade&lt;br /&gt;               &lt;br /&gt; As a production DBA, its  a good idea to collect the resultset dm_os_wait_stats DMV once  few hours or twice or thrice a day and perhaps analyze the results to notice any surge in wait time.  This can be one proactive approach to detect potential major problems.&lt;br /&gt; &lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-7991488082457505564?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/7991488082457505564/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=7991488082457505564&amp;isPopup=true' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7991488082457505564'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7991488082457505564'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/10/finding-cpu-pressure-using-waitstats.html' title='Finding CPU Pressure using wait_stats DMV'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_TcYicum3HIs/TM07Pi8T0FI/AAAAAAAAAUA/cKnCztylUmw/s72-c/wait_stats.GIF' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-2715024192996657326</id><published>2010-10-23T21:34:00.000-07:00</published><updated>2010-10-23T21:40:54.089-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Scripts'/><title type='text'>Backup path</title><content type='html'>&lt;p&gt;&lt;br /&gt;Sometimes after taking a backup, one can forget the path where the backup was taken. &lt;br /&gt;The simple script below provides the list of backups taken on the server, backup type ( log - l, Full -D..),backup start date and time, lsn numbers and the path of the backup. Simple and handy.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;database_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;TYPE&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;backup_start_date&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;first_lsn&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;last_lsn&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;backupmediafamily&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;physical_device_name&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;msdb&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;backupset&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;msdb&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;backupmediafamily&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;backupset&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;media_set_id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;backupmediafamily&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;media_set_id&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ORDER&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;backup_start_date&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;DESC&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TcYicum3HIs/TMO4l-SjuaI/AAAAAAAAAT0/ldB9Gwl7HnQ/s1600/backup.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 79px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/TMO4l-SjuaI/AAAAAAAAAT0/ldB9Gwl7HnQ/s400/backup.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5531467729856608674" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-2715024192996657326?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/2715024192996657326/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=2715024192996657326&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/2715024192996657326'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/2715024192996657326'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/10/backup-path.html' title='Backup path'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_TcYicum3HIs/TMO4l-SjuaI/AAAAAAAAAT0/ldB9Gwl7HnQ/s72-c/backup.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-4560368769531242910</id><published>2010-10-19T02:38:00.000-07:00</published><updated>2010-10-19T03:10:09.277-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Scripts'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><title type='text'>Currently executing query - wait status</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;Recently, I had a developer who came to my desk with a request.&lt;br /&gt;&lt;br /&gt;&lt;i&gt; ' I started a execution of a script from a program. the program has been running for a long time. I don't know whether its executing or hanging. Can you check from database whether the script is running or hanging  and if hanging at which statement it is hanging?. If possible please tell me why its hanging..' &lt;/i&gt;&lt;br /&gt;&lt;br /&gt;Earlier in my blog, I posted the following script to check the currently running query.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;TEXT&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[query]&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Db_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;sysprocesses&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbid&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cpu&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;memusage&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;status&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;loginame&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;hostname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;lastwaittype&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;last_batch&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cmd&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;program_name&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;sysprocesses&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;OUTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;APPLY&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Fn_get_sql&lt;/b&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;sql_handle&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ORDER&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;cpu&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;DESC&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;The above query would give the stored procedure's name or starting statements of the script/batch and not the exact statement within the stored procedure or batch that is currently executing. To find that one should use &lt;b&gt; sys.dm_exec_requests &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;session_id&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;request_id&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Db_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;database_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;start_time&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;status&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;command&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Substring&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;txt&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "blue"&gt;TEXT&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;statement_start_offset&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;/&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;2&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;CASE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;statement_end_offset&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;WHEN&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;-&lt;/font&gt;&lt;font color = "black"&gt;1&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;THEN&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Datalength&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;txt&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "blue"&gt;TEXT&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;ELSE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;statement_end_offset&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "silver"&gt;-&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;statement_start_offset&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;/&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;2&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;statement_text&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;wait_type&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;wait_time&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;blocking_session_id&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;percent_complete&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cpu_time&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;reads&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;writes&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;logical_reads&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;row_count&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_exec_requests&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;CROSS&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;APPLY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Dm_exec_sql_text&lt;/b&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;[sql_handle]&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;txt&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;session_id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;&amp;gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;@@SPID&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AND&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;session_id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;50&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;The query above gives the currently executing statement within a batch. sys.dm_exec_requests provides the columns  statement_start_offset ,statement_end_offset which specify the starting and ending positions of the currently executing query within a batch, in bytes. As 'text' column returned by  dm_exec_sql_text is of nvarchar datatype, statement_start_offset/2 is required to get to the starting postion of the query. statement_end_offset returns -1 to indicate the end of the batch.So, statement_text column of the query answers the first part of the developer's request.  &lt;br /&gt;&lt;br /&gt;Now, for the next part , ie finding the  &lt;b&gt; status of the query &lt;/b&gt; ie.. running/hanging/sleeping etc &lt;br /&gt;&lt;br /&gt;A user request can take the following major states.&lt;br /&gt;&lt;br /&gt; &lt;b&gt; &gt; Sleeping : &lt;/b&gt; Query completed execution and has nothing to do. Connection   &lt;br /&gt;                       is still not closed.&lt;br /&gt; &lt;b&gt; &gt; Running : &lt;/b&gt;  Currently running/executing.&lt;br /&gt; &lt;b&gt; &gt; Runnable: &lt;/b&gt;  Can run but waiting for a processor. Implies waiting in &lt;br /&gt;                       processor queue.&lt;br /&gt; &lt;b&gt; &gt; Suspended:&lt;/b&gt;  Waiting for some resource or event. Wait may be because of &lt;br /&gt;                       locks, IO completion, Latch or Memory wait etc.&lt;br /&gt;&lt;br /&gt;Sleeping requests are not shown in sys.dm_exec_requests. Runnable/Suspended state requests are the ones we need to watch out for. Runnable/Suspended states are not abnormal, as long as they dont stay at the same state for a long time.&lt;br /&gt;&lt;br /&gt;Now for the third part..' &lt;b&gt; Why it is hanging..' &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;The &lt;b&gt; wait_type &lt;/b&gt; column on the resultset will return the reason why a request is waiting. To know the meaning of each wait type refer &lt;a href = "http://technet.microsoft.com/en-us/library/ms179984.aspx"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Popular few wait types are given below&lt;br /&gt;&lt;br /&gt;ASYNC_IO_COMPLETION - Waiting for IO&lt;br /&gt;CXPACKET - Wait beacuse of Parallelism.&lt;br /&gt;LOGBUFFER - Waiting for the logbuffer to release some space in the memory.&lt;br /&gt;LCK_M% - Waiting for lock to be released. Refer to Blocking_Session_id for finding the blocking process.&lt;br /&gt;&lt;br /&gt;Other interesting columns returned by dm_exec_requests are wait_time returning time spent waiting in ms, percent_complete indicating progress of the query,&lt;br /&gt;cpu_time,reads,writes,logical_reads and row_count. percent_complete mostly indicates the progress for select queries,backup process but not for DML operations.&lt;br /&gt;&lt;br /&gt;So, a quick execution of the script and checking the wait state details can clearly&lt;br /&gt;show the current progress/ status of the request.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-4560368769531242910?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/4560368769531242910/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=4560368769531242910&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/4560368769531242910'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/4560368769531242910'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/10/currently-executing-query-wait-status.html' title='Currently executing query - wait status'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-6865473061185914975</id><published>2010-09-27T02:22:00.000-07:00</published><updated>2010-09-27T03:09:38.885-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Indexing'/><category scheme='http://www.blogger.com/atom/ns#' term='Storage Engine'/><title type='text'>SQL Server never stores the same column twice</title><content type='html'>&lt;p&gt;&lt;br /&gt;Consider the following table.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;tbl&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;col1_pky&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;PRIMARY&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;KEY&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;col2_nc_index&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INDEX&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;nc_col2_col1&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;tbl&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;col2_nc_index&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;col1_pky&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;I have created a table dbo.tbl with the column &lt;b&gt; col1_pky &lt;/b&gt; as primary key.&lt;br /&gt;A composite Non Clustered index is created on &lt;b&gt; col2_nc_index,col1_pky &lt;/b&gt; columns.As by definition every non clustered index contains the clustered index key.&lt;br /&gt;So going by definition, the non-clustered index should contain the following&lt;br /&gt;       &lt;br /&gt;       &lt;b&gt; *  col2_nc_index,col1_pky &lt;/b&gt;- Index definition &lt;br /&gt;       &lt;b&gt; *  col1_pky &lt;/b&gt; - Clustered Index key &lt;br /&gt;&lt;br /&gt;&lt;b&gt; col1_pky &lt;/b&gt;is supposed to repeat twice as its a part of the nonclustered index and also clustered index key. But, SQL Server avoids the same by not storing the same&lt;br /&gt;column twice.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "green"&gt;&lt;i&gt;/*&amp;nbsp;Data&amp;nbsp;Generation&amp;nbsp;Script&amp;nbsp;*/&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@col1&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@col2&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@col1&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@col2&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;10000&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHILE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@col1&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;10000&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;tbl&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@col1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@col2&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@col1&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@col1&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@col2&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@col2&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;-&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;As usual let us use &lt;b&gt; DBCC IND / PAGE &lt;/b&gt; to check the same.&lt;br /&gt;To check the root page&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;ind&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'dbadb'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'tbl'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;-&lt;/font&gt;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TcYicum3HIs/TKBo1Lb5BRI/AAAAAAAAATQ/vEabBfNNwHA/s1600/root.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 229px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/TKBo1Lb5BRI/AAAAAAAAATQ/vEabBfNNwHA/s400/root.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5521528405968356626" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Root page is found as  18684 &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Let us take a look at the contents of root page using DBCC PAGE&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;traceon&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;3604&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;page&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;dbadb&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;18684&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;3&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_TcYicum3HIs/TKBpTbfBbpI/AAAAAAAAATY/pOY6qMV6T2A/s1600/root_level_columns.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 223px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/TKBpTbfBbpI/AAAAAAAAATY/pOY6qMV6T2A/s400/root_level_columns.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5521528925672533650" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Note that &lt;b&gt; col1_pky appears only once and doesnt appear twice. &lt;/b&gt; To confirm the same let us check the contents of a leaf level page.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;page&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;dbadb&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;19044&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;3&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_TcYicum3HIs/TKBpz1p5mdI/AAAAAAAAATg/2pPsfdolz_Q/s1600/root_level_columns.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 223px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/TKBpz1p5mdI/AAAAAAAAATg/2pPsfdolz_Q/s400/root_level_columns.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5521529482453293522" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Again col1_pky is present only once. So, &lt;b&gt; SQL Server always stores a column only once in a table.&lt;/b&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-6865473061185914975?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/6865473061185914975/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=6865473061185914975&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/6865473061185914975'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/6865473061185914975'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/09/sql-server-never-stores-same-column.html' title='SQL Server never stores the same column twice'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_TcYicum3HIs/TKBo1Lb5BRI/AAAAAAAAATQ/vEabBfNNwHA/s72-c/root.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-1539918293247730779</id><published>2010-09-12T19:06:00.000-07:00</published><updated>2010-09-12T19:07:54.799-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='SQL Security'/><title type='text'>Avoiding Explicit Permission - Execute as clause</title><content type='html'>&lt;p&gt;&lt;br /&gt;Providing direct access on tables for application user accounts is always a security concern. But, there are some scenarios which forces us to do the same. I previously discussed it in detail over &lt;a href="http://strictlysql.blogspot.com/2010/08/stored-procedures-explicit-permissions.html"&gt;here&lt;/a&gt;. SQL Server 2008/2005 provides a few options to avoid providing direct rights, even when the stored procedure involves dynamic sql, DDL, cross database reference etc. This post will explore one such option.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Execute AS option: &lt;/b&gt;&lt;br /&gt;While creating stored procedures, we can specify EXECUTE AS clause to run the stored procedure under a different account. For example consider the following&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;PROCEDURE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Rights_check&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;AS&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@txt&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;NVARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;100&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@txt&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'Select&amp;nbsp;*&amp;nbsp;from&amp;nbsp;databaselog'&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Sp_executesql&lt;/b&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@txt&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;Let us grant rights to account test1, which doesn't have direct access to 'databaselog' table.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GRANT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;rights_check&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;test1&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;Executing the 'dbo.rights_check' procedure from 'test1' account fails as test1 doesn't have direct access on 'databaselog' table.Using 'Execute As' clause can allow test1 to execute 'dbo.rights_check' procedure successfully without directly providing rights on the 'databaselog' table.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ALTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;PROCEDURE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Rights_check&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WITH&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;EXECUTE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;owner&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;AS&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@txt&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;NVARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;100&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@txt&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'Select&amp;nbsp;*&amp;nbsp;from&amp;nbsp;databaselog'&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Sp_executesql&lt;/b&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@txt&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;I have just added the  EXECUTE as OWNER clause on top which ensures that the stored procedure is executed in the context of owner of the stored procedure. Execution of the stored procedure using test1 is successful as expected.&lt;br /&gt;&lt;br /&gt;The reason is, adding EXECUTE as OWNER allows any account which has execution rights on the stored procedure to execute under the security context of the owner of the stored procedure. This ensures that the executing account is allowed to obtain the special permissions only within the stored procedure and not outside the scope of it.&lt;br /&gt;&lt;br /&gt;EXECUTE AS clause provides additional options like EXECUTE as 'user_name'.   EXECUTE as 'user_name' allows one to execute under the context of particular user account instead of owner of the object.More details on the same can be read &lt;a href = "http://msdn.microsoft.com/en-us/library/ms188354.aspx"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;EXECUTE AS clause effectively solves the security problems that arise when DDL statements, dynamic sql, cross database references are used.Digital Signatures, introduced in SQL Server 2005 also helps in addressing similar security problems which we will cover in the next post.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-1539918293247730779?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/1539918293247730779/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=1539918293247730779&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/1539918293247730779'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/1539918293247730779'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/09/avoiding-explicit-permission-execute-as.html' title='Avoiding Explicit Permission - Execute as clause'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-9061774070154923374</id><published>2010-09-06T21:06:00.000-07:00</published><updated>2010-09-06T21:10:03.468-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Indexing'/><category scheme='http://www.blogger.com/atom/ns#' term='Links'/><category scheme='http://www.blogger.com/atom/ns#' term='Storage Engine'/><title type='text'>DBCC IND/ PAGE - Unique Non Clustered index structure</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;I am back after a short break. Few personal and official commitments have kept me away from posting. Apologies.From now on I will be posting at usual pace.&lt;br /&gt;&lt;br /&gt;Continuing from my series of posts on DBCC IND, DBCC PAGE, this post will deal with the structure of Unique Non clustered index.Refer to the following links for previous posts on the same topic&lt;br /&gt;&lt;br /&gt; &lt;b&gt; 1. DBCC IND/PAGE intro -&lt;/b&gt; Refer &lt;a href = "http://strictlysql.blogspot.com/2010/08/dbcc-ind-dbcc-page-intro.html"&gt;here&lt;/a&gt; &lt;br /&gt; &lt;b&gt; 2. DBCC IND/ PAGE  -&lt;/b&gt; Non Clustered Index structure on a table with Unique/Non unique Clustered index - Refer &lt;a href="http://strictlysql.blogspot.com/2010/08/dbcc-ind-page-non-clustered-index.html"&gt;here&lt;/a&gt; &lt;br /&gt;&lt;br /&gt;Structure of a Unique Non clustered index is different from ordinary ( Non unique ) non clustered index.  The difference is that the Clustered index columns,&lt;br /&gt;which are normally a part of Non clustered index, are not present in the Non leaf nodes when the Non clustered index is unique.When a Non clustered index is&lt;br /&gt;unique, the clustered index columns are stored only in the leaves of the Non clustered index.&lt;br /&gt;&lt;br /&gt;A detailed explanation on the same is given by the legendary Kalen Deanley :) Who else in the planet can explain internals better than her? &lt;br /&gt;So, for more details on the topic, refer to her article &lt;a href ="http://sqlblog.com/blogs/kalen_delaney/archive/2008/03/16/nonclustered-index-keys.aspx"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-9061774070154923374?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/9061774070154923374/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=9061774070154923374&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/9061774070154923374'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/9061774070154923374'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/09/dbcc-ind-page-unique-non-clustered.html' title='DBCC IND/ PAGE - Unique Non Clustered index structure'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-1441377133277125747</id><published>2010-08-17T07:06:00.000-07:00</published><updated>2010-08-17T07:51:56.383-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='SQL Security'/><title type='text'>Stored Procedures , Explicit Permissions and Security concerns</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;Its always a good practice to have your code wrapped in stored procedures. &lt;br /&gt;One of the reasons is Security. To explain a bit more, when one grants execution rights on a stored procedure to a user, the user gets the rights to perform all the operations ( Select/Insert/update/delete ) within the stored procedure.However, the same user cannot perform the operations outside the context of stored procedure. &lt;br /&gt;&lt;br /&gt;&lt;b&gt; Ex: &lt;/b&gt; &lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GRANT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;usp_stored_proc&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;user1&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;Assume that the stored proc dbo.usp_stored_proc performs Select and update on table1, then user1 can perform these operations only while executing dbo.usp_stored_proc and not directly. In other words, user1 cannot bypass the stored procedure and directly perform a select/update on table1.&lt;br /&gt;&lt;br /&gt;But there are a few operations, where one needs to explictly grant permission to a object inside a stored procedure.In other words, its not enough if we just grant EXEC rights on the stored procedure. Let me list down such scenarios.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; 1) Using Dynamic sql queries using sp_executesql / EXEC : &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;       If your stored procedure is using Dynamic SQL using sp_executesql then the &lt;br /&gt;       objects accessed in the dynamic sql require explicit permissions. &lt;br /&gt;&lt;br /&gt;       &lt;i&gt; For Example &lt;/i&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;PROCEDURE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Usp_stored_proc&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;AS&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dsql&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;NVARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;100&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dsql&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'&amp;nbsp;Select&amp;nbsp;*&amp;nbsp;from&amp;nbsp;table1&amp;nbsp;'&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Sp_executesql&lt;/b&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dsql&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;    For the above stored procedure, its not enough if we grant execution rights to &lt;br /&gt;dbo.usp_stored_proc. In addition,one needs to grant select rights on 'table1' for the user executing stored procedure. Explicit grant is required because dynamic sql are always treated as separate batch outside the scope of the stored procedure.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; 2) Cross database reference &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;    If you are accessing a table on another database, then one needs to explicitly grant rights.&lt;br /&gt; &lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;PROCEDURE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Usp_stored_proc&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;AS&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;empid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;salary&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;increment&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;name&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;hr_database&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;salary&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;username&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'clerk'&lt;/font&gt;&lt;font color = "silver"&gt;;&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "green"&gt;&lt;i&gt;&amp;nbsp;.......&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;...........&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Some&amp;nbsp;code&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;..........&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;...........&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;  In the above stored procedure, the salary from database 'HR_database' is accessed. The user who calls the stored procedure should have rights on the HR_database.dbo.salary for the stored procedure to execute successfully. &lt;br /&gt;&lt;br /&gt;    Please note that the above scenario is true, when cross database ownership chaining is not enabled.If cross database ownership chaining is enabled, and if both the objects(dbo.usp_stored_proc and HR_database.dbo.salary;) belong to the same owner, then explicit permissions need not be granted.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; 3) While using linked servers &lt;/b&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;PROCEDURE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Usp_stored_proc&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;AS&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;linkedserver&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "blue"&gt;DATABASE&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;table2&lt;/font&gt;&lt;font color = "silver"&gt;;&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;  On the above stored procedure, linked server is used to refer to a table in a remote server.In such a case, the login that maps the user ( executing the stored procedure ) to the remote server should have select rights on table2. For understanding login mappings on linked server refer &lt;a href ="http://www.databasejournal.com/features/mssql/article.php/3116011/Linked-Severs-on-MS-SQL-Part-3.htm"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; 4) Using DDL statements &lt;/b&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;PROCEDURE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Usp_stored_proc&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;AS&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;TRUNCATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;t1&lt;/font&gt;&lt;font color = "silver"&gt;;&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;   &lt;br /&gt;     If the stored procedure contains DDL statement like truncate,alter table, Create index then appropriate rights should be granted to caller of the stored procedure.Meagre execution rights on the stored procedure wouldnt suffice.&lt;br /&gt;&lt;br /&gt;On all the four scenarios listed above, ideally one would want the caller of the stored procedure to use these extra permissions only while executing the stored procedure. At the rest of the time, we wouldnt want the caller to gain direct access on the table.But by granting the rights explicitly, the caller of the stored procedure gets additional rights to perform the above mentioned operations without executing the stored procedure. ie, anyone can use the calling account and connect to the database and perform a Select on a entire salary table on HR_database or truncate a table t1, without executing stored procedure. Obviously, this can be a serious security concern.&lt;br /&gt;&lt;br /&gt;To prevent the same, there are a few excellent options in SQL Server 2005, SQL Server 2008 which will be discussed in the next post.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-1441377133277125747?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/1441377133277125747/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=1441377133277125747&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/1441377133277125747'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/1441377133277125747'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/08/stored-procedures-explicit-permissions.html' title='Stored Procedures , Explicit Permissions and Security concerns'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-5815670719125577920</id><published>2010-08-09T07:15:00.000-07:00</published><updated>2010-08-09T08:38:50.336-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Indexing'/><category scheme='http://www.blogger.com/atom/ns#' term='Storage Engine'/><title type='text'>DBCC IND/ PAGE - Non Clustered Index structure on a table with Unique/Non unique Clustered index</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;Continuing from the &lt;a href = "http://strictlysql.blogspot.com/2010/08/dbcc-ind-dbcc-page-intro.html"&gt;last post&lt;/a&gt;, let us analyze the structure of a non clustered index using DBCC IND/PAGE commands, when we have a clustered index on the table. To be specific, we will see the difference in structure of a Non Clustered index when we have unique clustered or non unique clustered index.&lt;br /&gt;&lt;br /&gt;As already written earlier &lt;a href = "http://strictlysql.blogspot.com/2009/11/leaf-of-non-clustered-index.html"&gt;here&lt;/a&gt;, a Non clustered index will store clustered index key in its index.However, there is a small change in Non clustered index when the clustered index is defined as Non unique.When one searches using the Non clustered index , the clustered index key helps in reaching the actual row in the table. But, when the clustered index is not unique SQL Server adds a additional &lt;b&gt; Unique identifier &lt;/b&gt; column along with the clustered index key on the non clustered index. We will see the same using DBCC IND and DBCC PAGE commands.&lt;br /&gt;&lt;br /&gt;Table structure is provided below. Students table has 2 columns namely 'student_name','sid'.'sid' has a UNIQUE clustered index. student_name has a non clustered index. &lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[students]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[student_name]&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[VARCHAR]&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[sid]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[PRIMARY]&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;UNIQUE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;CLUSTERED&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INDEX&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[CIX_students_id]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[Students]&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[sid]&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ASC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NONCLUSTERED&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INDEX&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[IX_students_name]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[Students]&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[student_name]&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ASC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;I have loaded about 100,001 rows on the table. Let us see the structure on Non clustered index [IX_students_name] using DBCC IND/PAGE command.&lt;br /&gt;&lt;br /&gt; Following steps are involved in reading the structure of the index.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; 1)  Finding the root page of the index. &lt;/b&gt;&lt;br /&gt;        &lt;br /&gt;Execute the following command.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;ind&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbadb&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;students&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;2&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;The third parameter is the ID of Non Clustered index [IX_students_name] which is obtained from sysindexes table.So we see only the pages of [IX_students_name] in our result set.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_TcYicum3HIs/TGAUn5f3jJI/AAAAAAAAAR4/NmLp4fayAno/s1600/unique_cluster_root.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 221px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/TGAUn5f3jJI/AAAAAAAAAR4/NmLp4fayAno/s400/unique_cluster_root.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5503421420328684690" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The command will show all the pages in the index. To identify the root of the index , identify the row with the highest Indexlevel. Indexlevel 0 refers to leaf pages. In Students table, the highest Indexlevel is noted as 1( which means there are only two levels on the index ie root and leaves ) and the page number is 17080.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; 2) Checking the contents of the root page &lt;/b&gt;&lt;br /&gt;For checking contents of the root page execute the following command.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;traceon&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;3604&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;page&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;dbadb&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;17080&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;3&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_TcYicum3HIs/TGAdthAQ-fI/AAAAAAAAASo/sJgKNxhYPQI/s1600/non_leaf_unique_cluster.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 227px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/TGAdthAQ-fI/AAAAAAAAASo/sJgKNxhYPQI/s400/non_leaf_unique_cluster.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5503431412437547506" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;You would see the non clustered key and clustered index key ( sid ) column on the root page of [IX_students_name].But there wont be any Unique Identifier as the Clustered index is unique.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Non clustered index structure with a non unique clustered index: &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Now let us check the structure of the Non clustered index when we have Non Unique clustered index.For that let us alter our clustered index to non unique cluster using the following command.I have just removed the UNIQUE keyword from the earlier script and recreated the clustered index using DROP_EXISTING = ON option.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;CLUSTERED&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INDEX&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[CIX_students_id]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[Students]&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[sid]&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ASC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;WITH&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;drop_existing&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[PRIMARY]&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;After executing the above command, the clustered index is non unique.&lt;br /&gt;Now let us again analyze the structure of Non clustered index.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; 1) Root of the index &lt;/b&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;ind&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbadb&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;students&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;2&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_TcYicum3HIs/TGAWjT36etI/AAAAAAAAASA/tPkqND4cGwM/s1600/non_unique_cluster_root_ix.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 210px;" src="http://2.bp.blogspot.com/_TcYicum3HIs/TGAWjT36etI/AAAAAAAAASA/tPkqND4cGwM/s400/non_unique_cluster_root_ix.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5503423540532771538" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Root page number is noted as &lt;b&gt; 15992 &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; 2) Contents of the root page &lt;/b&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;traceon&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;3604&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;page&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;dbadb&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;15992&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;3&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TcYicum3HIs/TGAXVsUD4CI/AAAAAAAAASI/rF8S_uUBePU/s1600/non_unique_cluster_unquiefier.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 262px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/TGAXVsUD4CI/AAAAAAAAASI/rF8S_uUBePU/s400/non_unique_cluster_unquiefier.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5503424406086737954" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;b&gt; Picture of index page with Uniquifier &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;You would notice that a new column called &lt;b&gt; 'Uniquifier' &lt;/b&gt; is added to the non clustered index's leaf page as the clustered index is not unique anymore. Uniquifier has NULL values as there are no duplicate rows in the table.&lt;br /&gt;&lt;br /&gt;Let us introduce a duplicate value in the table using the following script.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;students&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;100000&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "green"&gt;&lt;i&gt;/*&amp;nbsp;just&amp;nbsp;to&amp;nbsp;confirm&amp;nbsp;that&amp;nbsp;i&amp;nbsp;have&amp;nbsp;only&amp;nbsp;one&amp;nbsp;row.&amp;nbsp;*/&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;students&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;students&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;100000&lt;/font&gt;&lt;br /&gt;&lt;font color = "green"&gt;&lt;i&gt;/*&amp;nbsp;Manually&amp;nbsp;inserting&amp;nbsp;a&amp;nbsp;duplicate&amp;nbsp;*/&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;students&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;100000&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "green"&gt;&lt;i&gt;/*&amp;nbsp;Verifying&amp;nbsp;that&amp;nbsp;we&amp;nbsp;have&amp;nbsp;a&amp;nbsp;duplicate&amp;nbsp;*/&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_TcYicum3HIs/TGAYKxc9qwI/AAAAAAAAASQ/m01UI36FMEI/s1600/duplicate.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 253px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/TGAYKxc9qwI/AAAAAAAAASQ/m01UI36FMEI/s400/duplicate.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5503425317999323906" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Note that the row has a value E-100000 on student_name column&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Locating a value with in the index using DBCC IND/ DBCC PAGE: &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;To see how uniquifier is used in Non clustered index ,we need to find the duplicate row on the Non Clustered index with the value 'E-100000'. To do the same let us follow the following steps.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; 1) Finding the root page: &lt;/b&gt;&lt;br /&gt;We already know that root page id of [IX_students_name] is 15992&lt;br /&gt;&lt;br /&gt;&lt;b&gt; 2) Identify the page in which E-100000 is residing &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Fire the same DBCC page command used earlier&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;traceon&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;3604&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;page&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;dbadb&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;15992&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;3&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;The resultset of DBCC PAGE is quite similar to a telephone directory index page where key column(s) ( student_name ) is the name of the person we are searching for and ChildPageid column is the Pagenumber in the directory.&lt;br /&gt;&lt;br /&gt;For example,observe the Picture of index page with Uniqufier posted &lt;a href ="http://4.bp.blogspot.com/_TcYicum3HIs/TGAXVsUD4CI/AAAAAAAAASI/rF8S_uUBePU/s1600/non_unique_cluster_unquiefier.GIF"&gt;above&lt;/a&gt;.The value A-19032 on the student_name column ( 2nd row ) indicates that page number 15929 ( child page id ) contains rows starting from A-19032 and just before 'A-28080'. So to find E_10000 we should scroll to the row that exceeds (alphabetically) E-10000 by just and then goto the previous row and pick up the corresponding childpageid.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_TcYicum3HIs/TGAZDFHQICI/AAAAAAAAASY/_TJbAz6kYCs/s1600/finding_e_10000.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 298px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/TGAZDFHQICI/AAAAAAAAASY/_TJbAz6kYCs/s400/finding_e_10000.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5503426285349642274" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Observe the first row that alphabetically exceeds E-100000.&lt;br /&gt;Refer to the row before that which is likely to contain E-100000.ChildPageID gives the page number of the page linked to row. The Childpageid on the immediate previous row exceeding E-100000 is 8605. So Page number 8605 should contain E-100000.So, execute the following command.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;page&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;dbadb&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;8605&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;3&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_TcYicum3HIs/TGAZzIFIW8I/AAAAAAAAASg/i0XtEJHPIIo/s1600/e_10000.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 278px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/TGAZzIFIW8I/AAAAAAAAASg/i0XtEJHPIIo/s400/e_10000.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5503427110779771842" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Notice two rows with 'E-100000' which reflect the duplicate row we inserted earlier.&lt;br /&gt;Note the uniquiefier column for rows containing E-100000. They have values of 0,1 which will be used to identify the correct row in the clustered index.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; So, the conclusion of this long post is if you have a Non unique clustered index,&lt;br /&gt;then additional Uniquifier column will be added in the Non clustered index to locate the correct row. &lt;/b&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-5815670719125577920?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/5815670719125577920/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=5815670719125577920&amp;isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/5815670719125577920'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/5815670719125577920'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/08/dbcc-ind-page-non-clustered-index.html' title='DBCC IND/ PAGE - Non Clustered Index structure on a table with Unique/Non unique Clustered index'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_TcYicum3HIs/TGAUn5f3jJI/AAAAAAAAAR4/NmLp4fayAno/s72-c/unique_cluster_root.GIF' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-2758632855813428122</id><published>2010-08-02T08:09:00.000-07:00</published><updated>2010-08-02T08:52:44.812-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Indexing'/><category scheme='http://www.blogger.com/atom/ns#' term='Storage Engine'/><title type='text'>DBCC IND, DBCC PAGE - Intro</title><content type='html'>This post is a gentle intro into couple of useful undocumented commands which I will be using in the next couple of posts. &lt;br /&gt;&lt;br /&gt;&lt;b&gt; DBCC IND &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;DBCC IND command provides the list of pages used by the table or index. The command provides the page numbers used by the table along with previous page number,next page number. The command takes three parameters.&lt;br /&gt;&lt;br /&gt;Syntax is provided below.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;ind&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;&lt;/font&gt;&lt;font color = "maroon"&gt;database_name&lt;/font&gt;&lt;font color = "silver"&gt;&amp;gt;&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;&lt;/font&gt;&lt;font color = "maroon"&gt;table_name&lt;/font&gt;&lt;font color = "silver"&gt;&amp;gt;&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;non clustered&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;index_id*&lt;/font&gt;&lt;font color = "silver"&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;The third parameter can either be a Non Clustered index id ( provided by sys.indexes ) or 1,0,-1,-2. -1 provides complete information about all type of pages( in row data,row over flow data,IAM,all indexes ) associated with the table. The list of columns returned are provided below.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; IndexID: &lt;/b&gt; Provides id of the index. 0 - for heap, 1 - clustered index.,Non &lt;br /&gt;clustered ids &gt; 2 .&lt;br /&gt;&lt;b&gt;PagePID : &lt;/b&gt; Page number&lt;br /&gt;&lt;b&gt;IAMFID : &lt;/b&gt; Fileid of the file containing the page ( refer sysfiles ) &lt;br /&gt;&lt;b&gt;ObjectID : &lt;/b&gt; Objectid of the table used.&lt;br /&gt;&lt;b&gt;Iam_chain_type:&lt;/b&gt; Type of data stored ( in row data,row overflow etc )&lt;br /&gt;&lt;b&gt;PageType :&lt;/b&gt; 1 refers to Data page, 2 -&gt; Index page,3 and 4 -&gt; text pages&lt;br /&gt;&lt;b&gt;Indexlevel:&lt;/b&gt; 0 - refers to leaf. Highest value refers to root of an index.&lt;br /&gt;&lt;b&gt;NextPagePID,PrevPagePID :&lt;/b&gt; refers to next and previous page numbers.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Example: &lt;/b&gt;&lt;br /&gt;The command provides the pages used by table named Bigtable in database dbadb.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;ind&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;dbadb&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;bigtable&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;-&lt;/font&gt;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_TcYicum3HIs/TFbhdBy8gXI/AAAAAAAAARg/CNWhRu14I2s/s1600/ind.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 276px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/TFbhdBy8gXI/AAAAAAAAARg/CNWhRu14I2s/s400/ind.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5500831883694211442" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; DBCC PAGE: &lt;/b&gt;&lt;br /&gt;Next undocumented command we would be seeing is DBCC PAGE:&lt;br /&gt;DBCC PAGE takes the page number as the input parameter and displays the content of the page.Its almost like opening database page with your hands and viewing the contents of the page.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Syntax: &lt;/b&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;page&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "silver"&gt;&amp;lt;&lt;/font&gt;&lt;font color = "maroon"&gt;database_name&lt;/font&gt;&lt;font color = "silver"&gt;&amp;gt;&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;&lt;/font&gt;&lt;font color = "maroon"&gt;fileid&lt;/font&gt;&lt;font color = "silver"&gt;&amp;gt;&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;&lt;/font&gt;&lt;font color = "maroon"&gt;pagenumber&lt;/font&gt;&lt;font color = "silver"&gt;&amp;gt;&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;&lt;/font&gt;&lt;font color = "maroon"&gt;viewing_type&lt;/font&gt;&lt;font color = "silver"&gt;&amp;gt;&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;DBCC PAGE takes 4 parameters. They are database_name, fileid, pagenumber, viewing_type.Viewing_type parameter when passed a value 3 and displays the results in tabular format.If you are viewing a data page then the results are always in text format. For Index pages, when we pass the value 3 as parameter we get the results in a tabular format.DBCC PAGE command requires the trace flag 3604 to be turned on before its execution.&lt;br /&gt;&lt;br /&gt;A sample call is when a Index page is viewed is provided below:&lt;br /&gt;Note that the page number picked (9069) is a page from clustered index of the table&lt;br /&gt;'Bigtable'. 'Bigtable' has a  clustered index on a column named 'id' .&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;traceon&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;3604&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;page&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;dbadb&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;8176&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;3&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_TcYicum3HIs/TFbirQ-joQI/AAAAAAAAARo/IW7EBPCPlqc/s1600/page.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 382px; height: 400px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/TFbirQ-joQI/AAAAAAAAARo/IW7EBPCPlqc/s400/page.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5500833227799240962" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Useful columns returned are provided below:&lt;br /&gt;&lt;b&gt; Level &lt;/b&gt; : Index level&lt;br /&gt;&lt;b&gt;id(Key)&lt;/b&gt; : Actual column value on the index. The indexed column name suffixed with '(key)' becomes a part of a result set. If your index has 4 columns then 4 columns with the suffix '(key)' will be a part of your result set. In the above example the data/values on column 'id' present in the page 8176 are displayed. &lt;br /&gt;&lt;b&gt;ChildPageid&lt;/b&gt;: Pageid of the child page.&lt;br /&gt;&lt;br /&gt;A sample call when a data page number is passed is shown below:&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;traceon&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;3604&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;page&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;dbadb&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;9069&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;3&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_TcYicum3HIs/TFbkm-ixoII/AAAAAAAAARw/aCV2ID2xU4E/s1600/page_text.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 309px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/TFbkm-ixoII/AAAAAAAAARw/aCV2ID2xU4E/s400/page_text.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5500835353154658434" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;Bit cryptic to read the text format results. But anyways we will using it less compared to index page results.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; What we intend to do with these two commands ? &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;These two commands help us understand index structures, they way pages have been allocated and linked in a much better way. DBCC IND and PAGE are the two commands with which we can really get our hands dirty while trying to understand index structures. In the next couple of posts, I will analyze index structures using these commands and provide some interesting inferences on how index structures are arranged internally.&lt;br /&gt;&lt;br /&gt;References : As usual Kalen Deanley - SQL Server Internals :) &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-2758632855813428122?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/2758632855813428122/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=2758632855813428122&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/2758632855813428122'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/2758632855813428122'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/08/dbcc-ind-dbcc-page-intro.html' title='DBCC IND, DBCC PAGE - Intro'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_TcYicum3HIs/TFbhdBy8gXI/AAAAAAAAARg/CNWhRu14I2s/s72-c/ind.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-3321808864698817282</id><published>2010-07-25T18:57:00.000-07:00</published><updated>2010-07-25T23:16:52.303-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Scripts'/><category scheme='http://www.blogger.com/atom/ns#' term='Indexing'/><title type='text'>Disabling Primary Key/ Clustered index</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;Most of us are aware that indexes and constraints can be disabled. This post will concentrate on what happens when a Clustered index or Primary key is disabled.&lt;br /&gt;&lt;br /&gt;When a Clustered index is disabled , irrespective of whether it is a part of primary key or not, the entire table is placed offline. Placed offline essentially means that one cant issue any select/insert/update/delete on the table and also all the other indexes associated with the table are disabled. When a clustered primary key is disabled, in addition to the impact happening because of disabling clustered index, the foreign key constraints on the tables that are referencing the disabled primary key are disabled as well.But the most important part is when you enable back your primary key, &lt;b&gt; the foreign key constraints still remain disabled. &lt;/b&gt; We will see a quick example.&lt;br /&gt;&lt;br /&gt;I have a student table with sid column as clustered primary key. &lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[Student]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[sid]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[student_name]&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[VARCHAR]&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;CONSTRAINT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[PK_Student]&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;PRIMARY&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;KEY&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;CLUSTERED&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[sid]&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ASC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INDEX&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;ix_student_name&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[Student]&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;[student_name]&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;Student_marks table references student table. Script for the same is provided below.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[student_marks]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[sid]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[marks]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[subject_name]&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[VARCHAR]&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;COLLATE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sql_latin1_general_cp1_ci_as&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[subject_id]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ALTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[student_marks]&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ADD&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;CONSTRAINT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[FK_student_marks_Student]&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FOREIGN&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;KEY&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;[sid]&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;REFERENCES&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[Student]&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;[sid]&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;Let us disable the primary key on the student table. It can be done by &lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ALTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INDEX&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;pk_student&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;student&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;DISABLE&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;I&gt; Warning: Foreign key 'FK_student_marks_Student' on table 'student_marks' referencing table 'Student' was disabled as a result of disabling the index 'PK_Student'.&lt;br /&gt;Warning: Index 'IX_student_name' on table 'Student' was disabled as a result of disabling the clustered index on the table.&lt;br /&gt;&lt;/I&gt;&lt;br /&gt;&lt;br /&gt;We obtain a warning message saying that the Non Clustered Index on student table and the foreign key constraint on student_marks table are disabled.The same can be confirmed by querying sys.foreign_keys tabled. The is_disabled column with a value one indicates that the constraint is disabled.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Object_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;parent_object_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;table_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Object_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;referenced_object_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;referenced_pky_tbl_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;is_disabled&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;name&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;constraint_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;type_desc&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;foreign_keys&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Object_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;referenced_object_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;LIKE&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'student'&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_TcYicum3HIs/TEz6ALL0F1I/AAAAAAAAARI/Wr75oc01y4s/s1600/disbaled_contraint.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 238px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/TEz6ALL0F1I/AAAAAAAAARI/Wr75oc01y4s/s400/disbaled_contraint.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5498044126021818194" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Now let me enable the Primary key constraint back on student table.&lt;br /&gt;The command used is provided below.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ALTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INDEX&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ALL&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;student&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;rebuild&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;Primary key constraint is enabled by using the Index Rebuild command. One can rebuild just the Clustered index to enable the primary key but that would not enable the other non clustered indexes on the table. ALTER INDEX ALL enables all the indexes that are present in the table and hence it is the recommended command if you have disabled your clustered primary key.&lt;br /&gt;&lt;br /&gt;To prove that foreign key is not enabled,let me insert a row into student_marks table which violates the constraint.I execute the following script which effectively proves the same.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;student&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;-&lt;/font&gt;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;font color = "green"&gt;&lt;i&gt;/*--&amp;nbsp;To&amp;nbsp;ensure&amp;nbsp;that&amp;nbsp;no&amp;nbsp;row&amp;nbsp;on&amp;nbsp;student&amp;nbsp;has&amp;nbsp;sid&amp;nbsp;=&amp;nbsp;-1&amp;nbsp;*/&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;student_marks&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;subject_id&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;subject_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;marks&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;-&lt;/font&gt;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "red"&gt;'Tamil'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;60&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;student_marks&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;-&lt;/font&gt;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;font color = "green"&gt;&lt;i&gt;/*&amp;nbsp;to&amp;nbsp;confirm&amp;nbsp;that&amp;nbsp;insert&amp;nbsp;was&amp;nbsp;successful&amp;nbsp;*/&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Object_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;parent_object_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;table_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Object_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;referenced_object_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;referenced_pky_tbl_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;is_disabled&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;name&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;constraint_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;type_desc&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;foreign_keys&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Object_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;referenced_object_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;LIKE&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'student'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TcYicum3HIs/TE0FTuI6ejI/AAAAAAAAARQ/FmjZDoB-Y7c/s1600/disbaled_constraint_after_insert.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 333px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/TE0FTuI6ejI/AAAAAAAAARQ/FmjZDoB-Y7c/s400/disbaled_constraint_after_insert.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5498056556450314802" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;To enable the constraint first delete the invalid row and then use Check constraint command.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DELETE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;student_marks&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;-&lt;/font&gt;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ALTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[student_marks]&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;WITH CHECK CHECK CONSTRAINT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;CONSTRAINT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[FK_student_marks_Student]&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;So, the conclusion of this post is &lt;b&gt; always remember to enable the foreign key constraints after you have disabled and enabled your primary key.&lt;/b&gt; Let me finish the post with a quick script to generate a script for enabling foreign key &lt;br /&gt;constraints.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@primary_key_tbl_name&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;500&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@primary_key_tbl_name&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'student'&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'ALTER&amp;nbsp;TABLE&amp;nbsp;['&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;schemas&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;name&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'].['&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Object_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;parent_object_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "red"&gt;']&amp;nbsp;WITH&amp;nbsp;CHECK&amp;nbsp;CHECK&amp;nbsp;CONSTRAINT&amp;nbsp;['&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;foreign_keys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;name&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;']'&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;enable_fky&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;foreign_keys&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;schemas&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Object_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;referenced_object_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;LIKE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@primary_key_tbl_name&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AND&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;is_disabled&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AND&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TYPE&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'F'&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AND&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;schemas&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;schema_id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;foreign_keys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;schema_id&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;Set the value of @primary_key_tbl_name to the table whose primary key you have disabled. Executed the script after you have disabled and enabled the primary key. The Script pulls the related foriegn keys to the table and generates the enable script for the same.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-3321808864698817282?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/3321808864698817282/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=3321808864698817282&amp;isPopup=true' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3321808864698817282'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3321808864698817282'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/07/disabling-primary-key-clustered-index.html' title='Disabling Primary Key/ Clustered index'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_TcYicum3HIs/TEz6ALL0F1I/AAAAAAAAARI/Wr75oc01y4s/s72-c/disbaled_contraint.GIF' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-8092719690016415861</id><published>2010-07-19T02:09:00.000-07:00</published><updated>2010-07-19T02:32:47.384-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Best Practices'/><title type='text'>Number of Drives,Location and Raid levels</title><content type='html'>&lt;p&gt;&lt;br /&gt;When you are setting up your new server, One of the important tasks you have its to pick the right number of disks for correct  purposes at the correct RAID level. This article briefly deals with the same.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;SQL Server Data files: &lt;/b&gt; One needs atleast one dedicated drive for Data files. Ideally, for best performance one needs the disk to be have it Raid 10 as it involves random reads and random writes.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; SQL Server Log files: &lt;/b&gt; Log files have more sequential reads and writes. To avoid any IO contention, one needs a dedicated drive at Raid 1.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; SQL Server Backups: &lt;/b&gt; Backups are always to be kept on a drive different from data and log files so that they don't become a single point of failure. ie., When your data/ log file drive gets corrupted then your backups are also lost , if placed on the same drive. As Backups involve high writes RAID 1 / RAID 10 is recommended.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Location of Operating System: &lt;/b&gt; Operating system is ideally to be placed in C:\ drive and should always be kept away from data file drive, log file drive and backup drive.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Location of SQL Server executables: &lt;/b&gt; SQL Server's executables are similar &lt;br /&gt;to operating system files and hence we are better off keeping them away from Data and log files.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Tempdb location: &lt;/b&gt; In general, tempdb involves heavy writes and ideally Raid 1 or Raid 10 is recommended.It is a luxury if you can afford a separate disk for tempdb. If your application heavily uses tempdb then you are strongly recommended to do so.&lt;br /&gt;&lt;br /&gt;So, Ideally one atleast needs 3 dedicated disks for SQL Server with one each for Data file , Log files, Backups.Note that this doesn't include C drive required for Operating system + SQL Server's executable.Out of the 3 dedicated SQL Server, one is expected to be on RAID 10. I realize that RAID 10 is the most expensive but all good things in this world are expensive., aren't they :) The most popular raid level, RAID 5 is mostly good for reads and for file servers which don't change much. RAID 5 performs 50% slower than RAID 10.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; References: &lt;/b&gt; As usual another cool Brad McGhee article from &lt;a href = "http://www.sql-server-performance.com/articles/audit/hardware_performance_optimization_p1.aspx"&gt;here&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-8092719690016415861?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/8092719690016415861/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=8092719690016415861&amp;isPopup=true' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/8092719690016415861'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/8092719690016415861'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/07/number-of-driveslocation-and-raid.html' title='Number of Drives,Location and Raid levels'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-1416785356635575326</id><published>2010-07-14T19:59:00.000-07:00</published><updated>2010-07-14T20:49:49.942-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='T SQL'/><title type='text'>Does IDENTITY_INSERT reset identity?</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;Most of us are aware that if we want to force a value of our choice on a identity column then we use the command &lt;b&gt; SET IDENTITY_INSERT table name ON &lt;/b&gt;. But does that reset the identity? What kind of impact does it have  on the current identity is what we will see on this post.&lt;br /&gt;&lt;br /&gt;Consider the following script.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;test_table&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;auto_id&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;IDENTITY&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;somedata&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br&gt;&lt;font color = "green"&gt;&lt;i&gt;/*&amp;nbsp;Creating&amp;nbsp;a&amp;nbsp;table&amp;nbsp;with&amp;nbsp;a&amp;nbsp;identity&amp;nbsp;value&amp;nbsp;*/&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;test_table&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'x'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO 10&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;Note that GO followed by number executes the same query number of times. The above statement on a query analyzer/ssms inserts 10 times producing 10 rows on the test_table.&lt;br /&gt;&lt;br /&gt;To confirm the current identity value let us use ident_current command&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Ident_current&lt;/b&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "red"&gt;'test_table'&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_TcYicum3HIs/TD56HJadpNI/AAAAAAAAAQo/DhyEHiC8ZqA/s1600/identity_1.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 341px; height: 221px;" src="http://2.bp.blogspot.com/_TcYicum3HIs/TD56HJadpNI/AAAAAAAAAQo/DhyEHiC8ZqA/s400/identity_1.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5493962858643170514" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;As expected the current identity value is 10.&lt;br /&gt;Let us push the value 50 into the table. It can be done by&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;identity_insert&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;test_table&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;test_table&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;auto_id&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;somedata&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "red"&gt;'x'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;identity_insert&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;test_table&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;OFF&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;Now let us check the identity value again.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Ident_current&lt;/b&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "red"&gt;'test_table'&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_TcYicum3HIs/TD6AArKEehI/AAAAAAAAAQw/Axbecw6jcQU/s1600/identity_2.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 329px; height: 221px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/TD6AArKEehI/AAAAAAAAAQw/Axbecw6jcQU/s400/identity_2.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5493969344511900178" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The identity value has been reset to 50. To confirm the same, let us do a insert of a row and verify the value.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;test_table&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'x'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;MAX&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;auto_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Ident_current&lt;/b&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "red"&gt;'test_table'&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;test_table&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_TcYicum3HIs/TD6CEvDjpsI/AAAAAAAAAQ4/OpUa5iheZ2o/s1600/identity_3.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 170px;" src="http://2.bp.blogspot.com/_TcYicum3HIs/TD6CEvDjpsI/AAAAAAAAAQ4/OpUa5iheZ2o/s400/identity_3.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5493971613301057218" /&gt;&lt;/a&gt;&lt;br /&gt;The new value inserted is 51 as shown in the picture.&lt;br /&gt;So this effectively proves that SET IDENTITY_INSERT resets the value of the identity column.But wait.There is some more. Let us try to force another value, but this time lower than the current identity value 51.So let us force 25.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;identity_insert&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;test_table&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;test_table&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;auto_id&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;somedata&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;25&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "red"&gt;'x'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;identity_insert&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;test_table&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;OFF&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;Let us check the current seed. &lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;MAX&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;auto_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Ident_current&lt;/b&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "red"&gt;'test_table'&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;test_table&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_TcYicum3HIs/TD6D-s_pphI/AAAAAAAAARA/q2cmY0geIRI/s1600/identity_4.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 143px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/TD6D-s_pphI/AAAAAAAAARA/q2cmY0geIRI/s400/identity_4.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5493973708691842578" /&gt;&lt;/a&gt;&lt;br /&gt;The seed still remains at 51 as it was earlier. So, IDENTITY_INSERT hasnt reset the identity this time.&lt;br /&gt;&lt;br /&gt;So, the conclusion is &lt;b&gt; IDENTITY_INSERT resets the value if the value forced is higher than the current identity value.If the value forced is  less than the current identity value then IDENTITY_INSERT doesnt reset the value.&lt;/b&gt; This behaviour helps when one needs to fill any gaps caused in the identity column because of delete statments.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; What are the other commands that can affect identity property? &lt;/b&gt;&lt;br /&gt;&lt;b&gt; 1) Truncate table &lt;/b&gt; resets the identity value seed set at the table creation. However, if the table contains any foriegn key contraints then truncate table doesnt work.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; 2) DBCC CHECKIDENT(table_name,reseed,new_seed_value): &lt;/b&gt;&lt;br /&gt;    Checkident can reseed the table's seed to new value whereas truncate always sets the identity value to the value set initially at the table creation.&lt;br /&gt;&lt;br /&gt;All the options specified above namely identity_insert,truncate_table,DBCC checkident require sysadmin, db_owner or DDLadmin rights on the sql server/database.&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-1416785356635575326?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/1416785356635575326/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=1416785356635575326&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/1416785356635575326'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/1416785356635575326'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/07/does-identityinsert-reset-identity.html' title='Does IDENTITY_INSERT reset identity?'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_TcYicum3HIs/TD56HJadpNI/AAAAAAAAAQo/DhyEHiC8ZqA/s72-c/identity_1.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-3167164322103699648</id><published>2010-07-08T02:59:00.000-07:00</published><updated>2010-07-08T03:06:38.713-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Scripts'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><title type='text'>Lock Monitoring - SQL Server 2005</title><content type='html'>&lt;p&gt;&lt;br /&gt;As mentioned in the previous &lt;a href = "http://strictlysql.blogspot.com/2010/07/lock-monitoring-sql-server-2000.html"&gt;post&lt;/a&gt;, the query provided below can be &lt;br /&gt;use to check locking on sql server 2005. Script is Simple , but powerful :)&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;TEXT&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[query]&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;request_mode&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;request_type&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;LOCK&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;request_status&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_tran_locks&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;resource_type&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;object_type&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Db_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;sysprocesses&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbid&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cpu&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;memusage&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;physical_io&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;status&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;query_status&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;loginame&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;hostname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;lastwaittype&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;last_batch&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cmd&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;program_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;open_tran&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Db_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_tran_locks&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;resource_database_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;locked_database_name&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_tran_locks&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;sysprocesses&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;OUTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;APPLY&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Fn_get_sql&lt;/b&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;sql_handle&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AND&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_tran_locks&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;request_session_id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ORDER&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;request_mode&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;DESC&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_TcYicum3HIs/TDWibMVm-EI/AAAAAAAAAQc/8AyAqrTHSPo/s1600/lock_monitoring_sql2k5.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 272px;" src="http://2.bp.blogspot.com/_TcYicum3HIs/TDWibMVm-EI/AAAAAAAAAQc/8AyAqrTHSPo/s400/lock_monitoring_sql2k5.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5491473908700215362" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-3167164322103699648?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/3167164322103699648/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=3167164322103699648&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3167164322103699648'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3167164322103699648'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/07/lock-monitoring-sql-server-2005.html' title='Lock Monitoring - SQL Server 2005'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_TcYicum3HIs/TDWibMVm-EI/AAAAAAAAAQc/8AyAqrTHSPo/s72-c/lock_monitoring_sql2k5.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-7130588055230651721</id><published>2010-07-07T08:07:00.000-07:00</published><updated>2010-07-07T08:19:59.010-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Scripts'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><title type='text'>Lock monitoring - SQL Server 2000</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;One of the important parts of performance monitoring is monitoring locks. Imagine a situation, where you have a query that just waits for long and&lt;br /&gt;you want to find which query and connection is locking it. One option is to use &lt;b&gt; sp_lock &lt;/b&gt; and check which connection holds exclusive lock and then check&lt;br /&gt;use &lt;b&gt; dbcc inputbuffer &lt;/b&gt; to find the actual query. Another option is to &lt;b&gt;Activity monitor&lt;/b&gt; on the Enterprise manager. Both of these dont directly specify the query responsible for locking. The following script will list the queries along with the locks held by them.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#sp_lock_details&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;dbid&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;objid&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;indid&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;TYPE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;100&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;resource&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;1000&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;mode&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;10&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;status&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#sp_lock_details&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Sp_lock&lt;/b&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@max&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@spid&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@exec_qry&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;100&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@monitored_dt&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;DATETIME&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@monitored_dt&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Getdate&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[#sysproc]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;id&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;IDENTITY&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[spid]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[SMALLINT]&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[dbname]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[NVARCHAR]&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;128&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;COLLATE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sql_latin1_general_cp1_cs_as&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[cpu]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[memusage]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[status]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[NCHAR]&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;30&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;COLLATE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sql_latin1_general_cp1_cs_as&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[loginame]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[NCHAR]&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;128&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;COLLATE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sql_latin1_general_cp1_cs_as&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[hostname]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[NCHAR]&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;128&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;COLLATE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sql_latin1_general_cp1_cs_as&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[lastwaittype]&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[NCHAR]&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;32&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;COLLATE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sql_latin1_general_cp1_cs_as&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[last_batch]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[DATETIME]&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[cmd]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[NCHAR]&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;16&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;COLLATE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sql_latin1_general_cp1_cs_as&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[program_name]&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[NCHAR]&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;128&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;COLLATE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sql_latin1_general_cp1_cs_as&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;QUERY&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;255&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;open_tran&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[PRIMARY]&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#dbccinfo&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;eventype&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;255&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;param&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;eventinfo&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;255&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#sysproc&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;dbname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cpu&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;memusage&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;status&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;loginame&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;hostname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;lastwaittype&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;last_batch&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cmd&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;program_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;open_tran&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Db_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "blue"&gt;MASTER&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;sysprocesses&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbid&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cpu&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;memusage&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;status&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;loginame&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;hostname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;lastwaittype&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;last_batch&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cmd&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;program_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;open_tran&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;MASTER&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;sysprocesses&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@max&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;@@IDENTITY&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHILE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@max&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@spid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;#sysproc&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@exec_qry&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'DBCC&amp;nbsp;INPUTBUFFER('&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;CONVERT&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;3&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@spid&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;')'&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#dbccinfo&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "#8000FF"&gt;@exec_qry&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;UPDATE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;QUERY&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;eventinfo&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;#sysproc&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;#dbccinfo&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;id&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;TRUNCATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#dbccinfo&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#sysproc&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;QUERY&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;#sp_lock_details&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;mode&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;LOCK&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;TYPE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;object_type&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;dbname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cpu&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;memusage&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;#sysproc&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;status&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;query_status&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;loginame&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;hostname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;lastwaittype&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;last_batch&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cmd&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;program_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;open_tran&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Db_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;#sp_lock_details&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbid&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;locked_object_dbname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;#sp_lock_details&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;indid&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;locked_obj_indid&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;#sysproc&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;#sp_lock_details&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;#sp_lock_details&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#sysproc&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;spid&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ORDER&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#sp_lock_details&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;mode&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;DESC&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DROP&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#dbccinfo&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DROP&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#sysproc&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DROP&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#sp_lock_details&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_TcYicum3HIs/TDSZsCbE0AI/AAAAAAAAAQU/e414lRJAflk/s1600/lock_script_2k.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 208px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/TDSZsCbE0AI/AAAAAAAAAQU/e414lRJAflk/s400/lock_script_2k.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5491182827515203586" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Fairly simple script. Logic is to get the sp_lock details along with sysprocesses details and fire a inputbuffer or each spid.The Script provides the query involved along with the details from sysprocesses and hence it covers all the info provided by Activity monitor and adds a valuable addition by giving the query as well.The script provides the results in descending order of lock mode, so &lt;br /&gt;that 'X' the exclusive lock, which most of us are interested in comes first.&lt;br /&gt;&lt;br /&gt;The Script for SQL Server 2005 is even more simpler, which I will post in the next post.&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-7130588055230651721?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/7130588055230651721/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=7130588055230651721&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7130588055230651721'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7130588055230651721'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/07/lock-monitoring-sql-server-2000.html' title='Lock monitoring - SQL Server 2000'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_TcYicum3HIs/TDSZsCbE0AI/AAAAAAAAAQU/e414lRJAflk/s72-c/lock_script_2k.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-3527710204429892141</id><published>2010-06-30T19:26:00.000-07:00</published><updated>2010-06-30T19:41:22.614-07:00</updated><title type='text'>SQL Pass blog directory</title><content type='html'>&lt;p&gt; My blog has been listed in sql pass blog directory. Check it over &lt;a href = http://www.sqlpass.org/Community/BlogDirectory.aspx&gt;here&lt;/a&gt;.  No matter what, its a special feeling to see your blog and name listed along with the best in the industry ( though you are no where close to them ). Thanks SQL PASS.&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-3527710204429892141?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/3527710204429892141/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=3527710204429892141&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3527710204429892141'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3527710204429892141'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/06/sql-pass-blog-directory.html' title='SQL Pass blog directory'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-2759510999948221830</id><published>2010-06-28T02:15:00.000-07:00</published><updated>2010-06-30T20:04:56.112-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Indexing'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><category scheme='http://www.blogger.com/atom/ns#' term='Storage Engine'/><title type='text'>Rebuild vs ReOrg</title><content type='html'>&lt;p&gt; Rebuild or Regorg, difference between Rebuild or Reorg is a fairly common topic on blogs and forums. Here is my take on it.As I am not good on writing tr and td html tags lemme list down the comparison as pointers instead of a tabular column.&lt;br /&gt;&lt;br /&gt;Before starting the comparsions, just a small intro on fragmentation. I have written in detail on it over &lt;a href = "http://strictlysql.blogspot.com/2009/08/fragmentation.html"&gt;here&lt;/a&gt;. But still, a quick intro.&lt;br /&gt;Database Fragmentation is of 2 types. Internal and external fragmentation. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;&gt;&lt;/b&gt;Internal Fragmentation refers to the fragmentation that happens with in a page.&lt;br /&gt;&lt;b&gt;&gt;&lt;/b&gt;External fragmentation is divided to 2 types. Logical and extent fragmentation. &lt;br /&gt;&lt;b&gt;&gt;&lt;/b&gt;Logical fragmentation refers to out of order pages within a extent.&lt;br /&gt;&lt;b&gt;&gt;&lt;/b&gt;Extent fragmentation refers to out of order extents.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; What Reorg does? : &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Takes the leaf pages of each index, fixes the internal fragmentation by ensuring that each leaf page is filled as per the fillfactor specified initially while creating the index. Reorg also swaps and rearranges the out of order pages to fix logical fragmentation.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;picture of an Sample Extent before Reorg: &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_TcYicum3HIs/TCho4H70QeI/AAAAAAAAAPg/8mbDRjKhlnU/s1600/reorg_before.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 303px; height: 400px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/TCho4H70QeI/AAAAAAAAAPg/8mbDRjKhlnU/s400/reorg_before.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5487751459362849250" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;picture of Sample Extent after Reorg: &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_TcYicum3HIs/TChpHenhL3I/AAAAAAAAAPo/MKz10elA3nE/s1600/reorg_after.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 254px; height: 400px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/TChpHenhL3I/AAAAAAAAAPo/MKz10elA3nE/s400/reorg_after.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5487751723149766514" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; What Rebuild does? : &lt;/b&gt; &lt;br /&gt;Rebuild index recreates the index from scratch which means both the leaf and non leaf pages's internal fragmentation is removed.As it rebuilds the index completely it fixes logical fragmentation. Rebuild fixes extent fragmentation most of the &lt;br /&gt;times, but there are a few exceptions.&lt;br /&gt;&lt;br /&gt;Let us see the important differences/similarities.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; New Page allocation difference - Reorg and Rebuild: &lt;/b&gt;&lt;br /&gt;Reorg while fixing internal and logical fragmentation, uses only the existing pages that are allocated to the table.It never allocates new pages and moves around the existing pages to get the internal and logical fragmentation correct.Perhaps thats the reason its called 'Reorg'. &lt;br /&gt;&lt;br /&gt;On the other hand Rebuild can allocate new pages, to fix logical and extent fragmentation. Allocating new pages means that Rebuild can sometimes increase the space usage in the data file.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Leaf level pages &lt;/b&gt;&lt;br /&gt;As mentioned earlier, Reorg defragments only leaf level pages of a index and Rebuild defrags both leaf and non leaf levels of an index.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Extent fragmentation &lt;/b&gt;&lt;br /&gt;Extents are a group of 8 pages, when not present physically in their logical order, is said to be extent fragmented.For ex: page 1-8 of table 1 are present in extent-1.if Pages-9 to 16 of the table, present in extent-2 is not physically stored after extent-1 then the table is said to extent fragmented.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Picture of Sample Extent Fragmented table: &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_TcYicum3HIs/TChxdDX1PnI/AAAAAAAAAPw/NhSQV2jp0IA/s1600/extent_fragmentation.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 148px; height: 400px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/TChxdDX1PnI/AAAAAAAAAPw/NhSQV2jp0IA/s400/extent_fragmentation.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5487760889886359154" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;b&gt; Picture of Extents storage order after Rebuild &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_TcYicum3HIs/TChyB9-8OqI/AAAAAAAAAP4/LOLEbbpfrAI/s1600/extent_fragmentation_after_rebuild.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 152px; height: 400px;" src="http://2.bp.blogspot.com/_TcYicum3HIs/TChyB9-8OqI/AAAAAAAAAP4/LOLEbbpfrAI/s400/extent_fragmentation_after_rebuild.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5487761524094941858" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Locking - online/offline differences &lt;/b&gt;&lt;br /&gt;Reorg is a online process. ie The table remains online during Reorg and updates and Inserts can be done to the table. Reorg takes &lt;b&gt; Indent Exclusive lock &lt;/b&gt; at the table level. At page level, it takes exclusive lock only at the page at which is currently working on (swapping/ fixing internal frag ) and leaves the rest of the pages of the table. Once it finishes work on a single page, then it releases the exclusive lock and pulls a exclusive lock the next page it has to work on. If the system is too busy, Reorg sometimes skips the pages.&lt;br /&gt;&lt;br /&gt;Rebuild is a &lt;b&gt; offline process and consumes a exclusive lock &lt;/b&gt; on the entire table and stops any access to it until the operation finishes.However, Rebuild can be done online using the option ONLINE = ON,if and only if you have a Enterprise edition.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Stats differences &lt;/b&gt;&lt;br /&gt;Rebuild of clustered index updates all the statistics assocaited with the table ( both auto and user created ) whereas Reorg doesn't do the same. The &lt;b&gt; STATS_DATE &lt;/b&gt; function can be used to check the date at which statistics were last updated. &lt;br /&gt;&lt;br /&gt;&lt;b&gt; Log file growths &lt;/b&gt;&lt;br /&gt;Both cause the log files to grow. Rebuild can be a minimally logged operation where as REOrg is a fully logged operation. &lt;br /&gt;&lt;br /&gt;&lt;b&gt; Stoping / Pausing REOrg &lt;/b&gt;&lt;br /&gt;REOrg can be stopped during execution and can continue its execution from it where left.But Rebuild acts like a single transaction and always starts from scratch.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Lob differences &lt;/b&gt;&lt;br /&gt;Rebuild of clustered index on a table with LOB column ( varchar(max),varbinary(max),image,xml,text ) fails when performed ONLINE.REOrg of Clustered index compacts LOB objects on the table. REOrg of a non clustered index compacts LOB columns that are part of the included non key columns of the NC Index.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Replacements &lt;/b&gt;&lt;br /&gt;Reorg replaces DBCC Indexfrag and Rebuild replaces DBCC DBReindex. The DBCC commands still work on SQL Server 2005, but its recommended to switch to REorg/Rebuild.&lt;br /&gt;&lt;br /&gt;Just a note about fragmentation. Fragmentation can hurt your servers performance only when your queries scan the tables instead of Seeking.If your indexes are well selected and accurate, then the queries should use seek operator to fetch the rows and as Seek directly jumps to the row it wants, it doesnt matter how much the table is fragemented or disordered.So, a occasional Reorg or rebuilt should be enough to keep your databases and tables healthy. In general, one perfoms Re0rg when fragmention is around 10 -30 % and rebuild when its greater than 30 %.&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-2759510999948221830?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/2759510999948221830/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=2759510999948221830&amp;isPopup=true' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/2759510999948221830'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/2759510999948221830'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/06/rebuild-vs-reorg.html' title='Rebuild vs ReOrg'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_TcYicum3HIs/TCho4H70QeI/AAAAAAAAAPg/8mbDRjKhlnU/s72-c/reorg_before.GIF' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-632218236776491376</id><published>2010-06-21T00:37:00.000-07:00</published><updated>2010-06-21T00:50:58.838-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><title type='text'>Perfmon  - useful links</title><content type='html'>&lt;p&gt;&lt;br /&gt;I have couple of useful links on Perfmon.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;1)  How to correlate Perfmon data with Profiler Data?  &lt;/b&gt; &lt;br /&gt;&lt;br /&gt;    Perfmon data gives us the high level picture of what is causing a performance slowdown.Profiler shows what queries exactly running. Correlating both, tells which processes/queries were exactly responsible for a performance slowdown. The article describes neat and clean way provided by Microsoft tools to correlate perfmon and profiler data. Refer &lt;a href = "http://www.mssqltips.com/tip.asp?tip=1212"&gt; here&lt;/a&gt;.&lt;br /&gt;for more details. Note that one needs SQL Server 2005 tools to use correlate functionality.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;2)  How to capture perfmon data directly into table? &lt;/b&gt; &lt;br /&gt;&lt;br /&gt;   Perfmon counter data can be automatically saved to SQL Servers table instead of binary file (blg) or csv file.Saving Perfmon counter into sql server table allows one to check perfmon counter values like CPU, memory, Disk usage % remotely by just using SSMS/ Query analyzer to connect to the server, without actually logging into the server. The above mentioned technique is one method which works in SQL 2000 to check the CPU of monitored instance remotely. Note that the method explained in &lt;a href ="http://strictlysql.blogspot.com/2010/06/finding-cpu-utilization-in-sql-server.html"&gt;last post&lt;/a&gt; doesn't work for SQL 2k. Refer &lt;a href = "http://www.netadmintools.com/art577.html"&gt;here&lt;/a&gt; for detailed explanation.&lt;br /&gt;&lt;br /&gt;   The Perfmon data gets captured into two tables namely CounterDetails,CounterData. The details can be viewed using the following query.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;counterdetails&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;objectname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;CASE&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;WHEN&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Isnull&lt;/b&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;instancename&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'!@#$'&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'!@#$'&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;THEN&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;''&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;ELSE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;instancename&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;instance_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;counterdetails&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;countername&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;counter_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;counterdata&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;counterdatetime&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;counterdata&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;countervalue&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;counterdata&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;counterdetails&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;counterdetails&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;counterid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;counterdata&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;counterid&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ORDER&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;counterdata&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;counterdatetime&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-632218236776491376?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/632218236776491376/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=632218236776491376&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/632218236776491376'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/632218236776491376'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/06/perfmon-useful-links.html' title='Perfmon  - useful links'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-2646796925370476189</id><published>2010-06-13T23:42:00.000-07:00</published><updated>2010-08-18T02:18:19.631-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Scripts'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><title type='text'>Finding CPU utilization in SQL Server 2005:</title><content type='html'>&lt;p&gt;&lt;br /&gt;Finding the CPU percentage used by SQL Server is perhaps the first step in performance monitoring. Often, finding the CPU % as shown by task manager, from SQL Server can be tricky as SQL Server's sysprocesses and exec_requests gives only CPU ticks and not the exact CPU % used. Logging into each and every server manually and checking the CPU can be a pain if you are administering a large number of servers. So here is a solution.&lt;br /&gt;&lt;br /&gt;  &lt;span style="font-family:Courier New;font-size:85%;"&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;DECLARE&lt;/span&gt; &lt;span style="color:#8000FF;"&gt;@ts_now&lt;/span&gt; &lt;span style="color:black;"&gt;&lt;i&gt;BIGINT&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#8000FF;"&gt;@ts_now&lt;/span&gt; &lt;span style="color:silver;"&gt;=&lt;/span&gt; &lt;span style="color:maroon;"&gt;cpu_ticks&lt;/span&gt; &lt;span style="color:silver;"&gt;/&lt;/span&gt; &lt;span style="color:fuchsia;"&gt;&lt;i&gt;CONVERT&lt;/i&gt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;&lt;i&gt;FLOAT&lt;/i&gt;&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt; &lt;span style="color:maroon;"&gt;cpu_ticks_in_ms&lt;/span&gt;&lt;span style="color:maroon;"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;FROM&lt;/span&gt;   &lt;span style="color:maroon;"&gt;sys&lt;/span&gt;&lt;span style="color:silver;"&gt;.&lt;/span&gt;&lt;span style="color:maroon;"&gt;dm_os_sys_info&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;SELECT&lt;/span&gt; &lt;span style="color:maroon;"&gt;record_id&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:fuchsia;"&gt;&lt;i&gt;Dateadd&lt;/i&gt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;(&lt;/span&gt;&lt;span style="color:maroon;"&gt;ms&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt; &lt;span style="color:silver;"&gt;-&lt;/span&gt;&lt;span style="color:black;"&gt;1&lt;/span&gt; &lt;span style="color:silver;"&gt;*&lt;/span&gt; &lt;span style="color:maroon;"&gt;(&lt;/span&gt; &lt;span style="color:#8000FF;"&gt;@ts_now&lt;/span&gt; &lt;span style="color:silver;"&gt;-&lt;/span&gt; &lt;span style="color:maroon;"&gt;[timestamp]&lt;/span&gt; &lt;span style="color:maroon;"&gt;)&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt; &lt;span style="color:fuchsia;"&gt;&lt;i&gt;Getdate&lt;/i&gt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;(&lt;/span&gt;&lt;span style="color:maroon;"&gt;)&lt;/span&gt;&lt;span style="color:maroon;"&gt;)&lt;/span&gt; &lt;span style="color:blue;"&gt;AS&lt;/span&gt; &lt;span style="color:maroon;"&gt;eventtime&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:maroon;"&gt;sqlprocessutilization&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:maroon;"&gt;systemidle&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:black;"&gt;100&lt;/span&gt; &lt;span style="color:silver;"&gt;-&lt;/span&gt; &lt;span style="color:maroon;"&gt;systemidle&lt;/span&gt; &lt;span style="color:silver;"&gt;-&lt;/span&gt; &lt;span style="color:maroon;"&gt;sqlprocessutilization&lt;/span&gt;               &lt;span style="color:blue;"&gt;AS&lt;/span&gt;&lt;br /&gt;      &lt;span style="color:maroon;"&gt;otherprocessutilization&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;FROM&lt;/span&gt;   &lt;span style="color:maroon;"&gt;(&lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT&lt;/span&gt; &lt;span style="color:maroon;"&gt;record&lt;/span&gt;&lt;span style="color:silver;"&gt;.&lt;/span&gt;&lt;span&gt;&lt;span class="Apple-style-span"  style="color:#FF0080;"&gt;&lt;b&gt;value&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;(&lt;/span&gt;&lt;span style="color:red;"&gt;'(./Record/@id)[1]'&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt; &lt;span style="color:red;"&gt;'int'&lt;/span&gt;&lt;span style="color:maroon;"&gt;)&lt;/span&gt;&lt;br /&gt;                     &lt;span style="color:blue;"&gt;AS&lt;/span&gt; &lt;span style="color:maroon;"&gt;record_id&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt;&lt;br /&gt;&lt;span style="color:maroon;"&gt;record&lt;/span&gt;&lt;span style="color:silver;"&gt;.&lt;/span&gt;&lt;span style="color:#FF0080;"&gt;&lt;b&gt;value&lt;/b&gt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;(&lt;/span&gt;&lt;span style="color:red;"&gt;'(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]'&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt;&lt;br /&gt;&lt;span style="color:red;"&gt;'int'&lt;/span&gt;&lt;span style="color:maroon;"&gt;)&lt;/span&gt;&lt;br /&gt;              &lt;span style="color:blue;"&gt;AS&lt;/span&gt; &lt;span style="color:maroon;"&gt;systemidle&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt;&lt;br /&gt;&lt;span style="color:maroon;"&gt;record&lt;/span&gt;&lt;span style="color:silver;"&gt;.&lt;/span&gt;&lt;span style="color:#FF0080;"&gt;&lt;b&gt;value&lt;/b&gt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;(&lt;/span&gt;&lt;span style="color:red;"&gt;'(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]'&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt; &lt;span style="color:red;"&gt;'int'&lt;/span&gt;&lt;span style="color:maroon;"&gt;)&lt;/span&gt; &lt;span style="color:blue;"&gt;AS&lt;/span&gt; &lt;span style="color:maroon;"&gt;sqlprocessutilization&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt;&lt;br /&gt;&lt;span style="color:maroon;"&gt;timestamp&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;FROM&lt;/span&gt;   &lt;span style="color:maroon;"&gt;(&lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT&lt;/span&gt; &lt;span style="color:maroon;"&gt;timestamp&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt;&lt;br /&gt;               &lt;span style="color:fuchsia;"&gt;&lt;i&gt;CONVERT&lt;/i&gt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;&lt;i&gt;XML&lt;/i&gt;&lt;/span&gt;&lt;span style="color:silver;"&gt;,&lt;/span&gt; &lt;span style="color:maroon;"&gt;record&lt;/span&gt;&lt;span style="color:maroon;"&gt;)&lt;/span&gt; &lt;span style="color:blue;"&gt;AS&lt;/span&gt; &lt;span style="color:maroon;"&gt;record&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:blue;"&gt;FROM&lt;/span&gt;   &lt;span style="color:maroon;"&gt;sys&lt;/span&gt;&lt;span style="color:silver;"&gt;.&lt;/span&gt;&lt;span style="color:maroon;"&gt;dm_os_ring_buffers&lt;/span&gt;&lt;br /&gt;        &lt;span style="color:blue;"&gt;WHERE&lt;/span&gt;  &lt;span style="color:maroon;"&gt;ring_buffer_type&lt;/span&gt; &lt;span style="color:silver;"&gt;=&lt;/span&gt; &lt;span style="color:red;"&gt;N'RING_BUFFER_SCHEDULER_MONITOR'&lt;/span&gt;&lt;br /&gt;               &lt;span style="color:blue;"&gt;AND&lt;/span&gt; &lt;span style="color:maroon;"&gt;record&lt;/span&gt; &lt;span style="color:blue;"&gt;LIKE&lt;/span&gt; &lt;span style="color:red;"&gt;'%&lt;systemhealth&gt;%'&lt;/systemhealth&gt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;)&lt;/span&gt; &lt;span style="color:blue;"&gt;AS&lt;/span&gt; &lt;span style="color:maroon;"&gt;x&lt;/span&gt;&lt;span style="color:maroon;"&gt;)&lt;/span&gt; &lt;span style="color:blue;"&gt;AS&lt;/span&gt; &lt;span style="color:maroon;"&gt;y&lt;/span&gt;&lt;br /&gt;&lt;span style="color:blue;"&gt;ORDER&lt;/span&gt;  &lt;span style="color:blue;"&gt;BY&lt;/span&gt; &lt;span style="color:maroon;"&gt;record_id&lt;/span&gt; &lt;span style="color:blue;"&gt;DESC&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The script provided above provides the approximate CPU usage % as shown in task manager. The result is shown below&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TcYicum3HIs/TBXQFEZ0gpI/AAAAAAAAAPY/65yJ4-ySau0/s1600/cpu_2k5.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 292px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/TBXQFEZ0gpI/AAAAAAAAAPY/65yJ4-ySau0/s400/cpu_2k5.GIF" border="0" alt="" id="BLOGGER_PHOTO_ID_5482516906893410962" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Column description is provided below:&lt;br /&gt;&lt;b&gt; SQLProcessutilization: &lt;/b&gt; Percentage used by SQL Server process ( sqlserver.exe)&lt;br /&gt;&lt;b&gt; OtherProcessutilization: &lt;/b&gt; Percentage used by other processes in the server&lt;br /&gt;&lt;b&gt; Eventtime: &lt;/b&gt; Time at which the CPU percentage was recorded.&lt;br /&gt;&lt;b&gt; systemidle: &lt;/b&gt; Perecentage of CPU left.&lt;br /&gt;&lt;b&gt; record_id: &lt;/b&gt; Just a incremental row id&lt;br /&gt;&lt;br /&gt;The CPU percentage is obtained from &lt;b&gt; sys.dm_os_ring_buffers &lt;/b&gt; DMV. sys.dm_os_ring_buffers records the CPU percentage&lt;br /&gt;for every one minute and it maintains CPU % for the last 256 minutes. Like all DMVs if the server is restarted then&lt;br /&gt;the DMVs lose the data.&lt;br /&gt;&lt;br /&gt;Now for the credits :)&lt;br /&gt;&lt;br /&gt;The Script provided above is obtained from the scripts deployed by Performance dashboard tool explained in the &lt;a href="http://strictlysql.blogspot.com/2010/06/performance-dashboard-sql-server-2005.html"&gt;last post&lt;/a&gt; Performance Dashboard uses the sp &lt;b&gt; [MS_PerfDashboard].[usp_Main_GetCPUHistory] &lt;/b&gt; for getting the reports on CPU. I just tweaked the sp to obtain the CPU % for 256 minutes instead of the last 15 minutes returned by Performance Dashboard. Thanks Performance Dashboard again :)&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-2646796925370476189?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/2646796925370476189/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=2646796925370476189&amp;isPopup=true' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/2646796925370476189'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/2646796925370476189'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/06/finding-cpu-utilization-in-sql-server.html' title='Finding CPU utilization in SQL Server 2005:'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_TcYicum3HIs/TBXQFEZ0gpI/AAAAAAAAAPY/65yJ4-ySau0/s72-c/cpu_2k5.GIF' height='72' width='72'/><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-3873572496641442275</id><published>2010-06-09T23:32:00.000-07:00</published><updated>2010-06-10T00:07:26.993-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><title type='text'>Performance Dashboard - SQL Server 2005 Performance monitoring tool</title><content type='html'>&lt;p&gt;&lt;br /&gt;Performance Dashboard is a simple performance monitoring tool for SQL Server 2005 released by Microsoft which is absolutely &lt;b&gt; FREE &lt;/b&gt; :) . Good and detailed explanation is given by Brad McGhee as usual over &lt;a href = "http://www.sql-server-performance.com/articles/per/bm_performance_dashboard_2005_p1.aspx"&gt;here&lt;/a&gt;. Let me briefly describe the salient features of it.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; How to Set up? &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Download Performance dashboard.msi from &lt;a href = "http://www.microsoft.com/downloads/details.aspx?familyid=1d3a4a0d-7e0c-4730-8204-e419218c1efc&amp;displaylang=en"&gt;here &lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Install on the machine from which you want to monitor your production servers. The installation completes in seconds in 3-4 clicks and doesn't demand a restart.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Proceed to &lt;I&gt; C:\Program files\Microsoft SQL Server\90\Tools\PerformanceDashboard &lt;/I&gt; folder ( or the folder where you have installed SQL Server. Performance Dashboard is installed on the folder where where you have already installed SQL Server)&lt;br /&gt;&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Copy the Setup.sql script and execute it in the production servers to be monitored.&lt;br /&gt;&lt;br /&gt;Thats it. You are ready to go.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; How to use it? &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Open SSMS on the machine you installed Performance Dashboard.&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Connect to the instance/SQL Server to be monitored on the SSMS&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Right Click on the connect server on the object explorer &lt;br /&gt;and Select &lt;b&gt; Reports &lt;/b&gt;.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TcYicum3HIs/TBCJTmPfPYI/AAAAAAAAAPI/21zSZUbaOZo/s1600/custom_reports.GIF"&gt;&lt;img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;width: 400px; height: 328px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/TBCJTmPfPYI/AAAAAAAAAPI/21zSZUbaOZo/s400/custom_reports.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5481031716285922690" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Pick &lt;b&gt; Custom Reports &lt;/b&gt;&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Select &lt;b&gt; performance_dashboard_main.rdl &lt;/b&gt; from C:\Program files\Microsoft SQL Server\90\Tools\PerformanceDashboard&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; And then experience the magic :)&lt;br /&gt;&lt;br /&gt;Opening Dashboard Screen shot is provided below.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TcYicum3HIs/TBCKG-ledaI/AAAAAAAAAPQ/7tk6pZjJ5Z8/s1600/home_page.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 390px; height: 281px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/TBCKG-ledaI/AAAAAAAAAPQ/7tk6pZjJ5Z8/s400/home_page.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5481032598993925538" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Why I like it? &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Can check CPU/Buffer cache hit ratio and overall health of Database Servers remotely from SSMS. &lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Quickly check the recently ran expensive queries ( as per by IO/CPU/ along with their Query Plan&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Check the Database Size and Log file size, growth, and other paramaters with in a few clicks.&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Provides Missing indexes and suggestions to improve peformance. Also provides number of seeks/scans on the objects.&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Provides DB level IO stats like Reads, Write and their wait times.&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; All these brilliant info at almost 0% cost on the monitored server.&lt;br /&gt;&lt;br /&gt;Now for few minor cons. &lt;br /&gt;&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; An alerting feature along with the tool would have been great. &lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Also, there are one or two minor bugs for which solutions are already available.&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; Ability to add perfmon counters as per our choice would have been even better.&lt;br /&gt;&lt;b&gt; * &lt;/b&gt; One cant do a direct copy or paste from the report, has to export to PDF /excel and then copy the required data.&lt;br /&gt;&lt;br /&gt;But, am I asking for bit too much on a free tool. Definitely yes :) Inspite of these little cons,Performance Dashboard is a must have for all DBAs, especially if you are monitoring 25+  servers. Just very handy for quickly checking the overall state and health of the server without actually logging into the server. Other than showing larger picture, it helps even to narrow down up to the query level. Simply awesome stuff. &lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-3873572496641442275?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/3873572496641442275/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=3873572496641442275&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3873572496641442275'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3873572496641442275'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/06/performance-dashboard-sql-server-2005.html' title='Performance Dashboard - SQL Server 2005 Performance monitoring tool'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_TcYicum3HIs/TBCJTmPfPYI/AAAAAAAAAPI/21zSZUbaOZo/s72-c/custom_reports.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-7403123449621024443</id><published>2010-06-06T19:31:00.000-07:00</published><updated>2010-06-06T20:17:13.522-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Indexing'/><category scheme='http://www.blogger.com/atom/ns#' term='Best Practices'/><category scheme='http://www.blogger.com/atom/ns#' term='T SQL'/><title type='text'>Scalar functions - on where clause</title><content type='html'>&lt;p&gt;&lt;br /&gt;Scalar functions as discussed in the &lt;a href= "http://strictlysql.blogspot.com/2010/05/performance-comparison-functions-versus.html"&gt;last post&lt;/a&gt; can be a performance bottleneck.Another place where Scalar functions ( both user defined and system ) can become a performance bottleneck is when used on a where clause of the query. Scalar functions when used on a where clause of a query can make the optimizer pick a table scan, even if the index is present, making the index useless.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "green"&gt;&lt;i&gt;/******&amp;nbsp;Object:&amp;nbsp;&amp;nbsp;Index&amp;nbsp;[ncix_Supplier_product_customer_trans_date]&amp;nbsp;&amp;nbsp;&amp;nbsp;******/&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NONCLUSTERED&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INDEX&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[ncix_Supplier_product_customer_trans_date]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[Supplier_Product_Customer]&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[trans_date]&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ASC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[PRIMARY]&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;A non clustered index on trans_date column of supplier_product_customer table has been created.When one needs to find the list of transactions for a particular day ( cutting the time part of the , the most common method of writing the query would be&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[Supplier_Product_customer]&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Datediff&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;dd&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[trans_date]&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'11/19/2009'&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;0&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;But,the problem with such a query is that it takes table scan instead of using the Non clustered index created on trans_date. The reason is that scalar function &lt;I&gt;datediff&lt;/I&gt; used on trans_date column stops the query analyzer from using the index.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_TcYicum3HIs/TAxdKQ7fAsI/AAAAAAAAAOQ/mWggnADncyA/s1600/qp_scalar_fun_on_where.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 193px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/TAxdKQ7fAsI/AAAAAAAAAOQ/mWggnADncyA/s400/qp_scalar_fun_on_where.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5479857277527589570" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;A better way of writing it would be&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_product_customer&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;trans_date&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'20091119'&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AND&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;trans_date&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'20091120'&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_TcYicum3HIs/TAxgyj0dfQI/AAAAAAAAAOY/Nb9vZAlmZhU/s1600/qp_scalar_fun_on_where_uses_index.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 179px;" src="http://2.bp.blogspot.com/_TcYicum3HIs/TAxgyj0dfQI/AAAAAAAAAOY/Nb9vZAlmZhU/s400/qp_scalar_fun_on_where_uses_index.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5479861268328054018" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The picture above shows a non clustered index seek instead of table scan. There are few other common scenarios where functions can be avoided. Let me briefly list them here&lt;br /&gt;&lt;br /&gt;          &lt;b&gt; * Left() string fuction &lt;/b&gt; can be effectively replaced by &lt;b&gt; like &lt;br /&gt;                'str%' &lt;/b&gt;&lt;br /&gt;          &lt;b&gt; * &lt;/b&gt; Usage of &lt;b&gt; upper/lower string functions &lt;/b&gt; can be avoided &lt;br /&gt;                      when the database has a &lt;b&gt; case insensitive collation. &lt;/b&gt;&lt;br /&gt;          &lt;b&gt; *  isnull(col1,'xyz') = 'xyz' &lt;/b&gt; can be replaced by &lt;b&gt; col1 = 'xyz' &lt;br /&gt;                 or col1 is null &lt;/b&gt;&lt;br /&gt;                 Note that OR conditions do use indexes and but at times they don't. &lt;br /&gt;                 Please check before use.&lt;br /&gt;          &lt;b&gt; * Getting data older than 60/n days query. &lt;/b&gt;&lt;br /&gt;                Standard way of doing it would be &lt;br /&gt;                    &lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Where&lt;/b&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;datediff&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;dd&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;trans_date&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;getdate&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;60&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt; &lt;br /&gt;&lt;b&gt; Replaced by &lt;/b&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Where&lt;/b&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;trans_date&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;CONVERT&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "blue"&gt;VARCHAR&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;getdate&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;-&lt;/font&gt;&lt;font color = "black"&gt;60&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;112&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;                  &lt;br /&gt;To Conclude, one should try the best to avoid having scalar functions in where clauses.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-7403123449621024443?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/7403123449621024443/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=7403123449621024443&amp;isPopup=true' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7403123449621024443'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7403123449621024443'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/06/scalar-functions-on-where-clause.html' title='Scalar functions - on where clause'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_TcYicum3HIs/TAxdKQ7fAsI/AAAAAAAAAOQ/mWggnADncyA/s72-c/qp_scalar_fun_on_where.GIF' height='72' width='72'/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-5397941010732835436</id><published>2010-05-25T02:36:00.000-07:00</published><updated>2010-05-25T06:45:10.248-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Best Practices'/><category scheme='http://www.blogger.com/atom/ns#' term='T SQL'/><title type='text'>Performance comparison - Functions versus Joins</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;User defined Scalar function is often chosen as a easier alternative for joining two or multiple tables while writing queries by developers. Other reason given by 'Few' developers is that its provides better readability on a long query.  Both are bad reasons for using functions as scalar functions when used inappropriately can potentially kill the performance. &lt;br /&gt;&lt;br /&gt;Scalar function when used on a query is executed for every row of the result set, just like a cursor would do. Row by Row processing is very very expensive when compared to SET based processing provided by joins. Scalar functions also makes indexes on the joined tables less effective or even useless.Above factors make joins, in general, a better option when they can replace a scalar function.&lt;br /&gt;&lt;br /&gt;We will see the same with an example. &lt;br /&gt;&lt;br /&gt;Consider the following tables &lt;b&gt; student &lt;/b&gt; , &lt;b&gt; student_marks &lt;/b&gt;. Student table contains student details and student_marks contain marks obtained by students on 5 subjects. Table definition and random data population scripts are provided below.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "green"&gt;&lt;i&gt;/***********************************************/&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "green"&gt;&lt;i&gt;/*************&amp;nbsp;Table&amp;nbsp;[dbo].[student_marks]******/&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[student_marks]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[sid]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[marks]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[subject_name]&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[VARCHAR]&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[subject_id]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;CONSTRAINT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[PK_student_marks]&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;PRIMARY&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;KEY&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;CLUSTERED&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[sid]&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ASC&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[subject_id]&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ASC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[PRIMARY]&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "green"&gt;&lt;i&gt;/******&amp;nbsp;Table&amp;nbsp;[dbo].[Student]&amp;nbsp;******/&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[Student]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[sid]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/i&gt;&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[student_name]&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[VARCHAR]&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;CONSTRAINT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[PK_Student]&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;PRIMARY&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;KEY&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;CLUSTERED&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[sid]&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ASC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ON&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[PRIMARY]&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;b&gt; Random data population scripts &lt;/b&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "green"&gt;&lt;i&gt;/*************Student&amp;nbsp;table&amp;nbsp;data&amp;nbsp;population*********/&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;0&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHILE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;100000&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;student&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;CHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;%&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;26&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;65&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'-'&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;CONVERT&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "green"&gt;&lt;i&gt;/*************Student_marks&amp;nbsp;table&amp;nbsp;population**********************/&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;student_marks&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Round&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "fuchsia"&gt;&lt;i&gt;Abs&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Rand&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;x&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;subid&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1000000000000&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;-&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Round&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Rand&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;x&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;subid&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;1000000000000&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;/&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;100&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;0&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;100&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;0&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;x&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;subject&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;x&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;subid&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;student&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'math'&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;subject&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;subid&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;UNION&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'physics'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;2&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;UNION&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'chemistry'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;3&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;UNION&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'computer_science'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;4&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;UNION&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'tamil'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;5&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;x&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;The Scripts provided would insert 100,001 rows in student table and 500,005 rows student marks table.Primary key clustered index has been created on sid column on student table and sid,subject_id columns on student_marks table.&lt;br /&gt;&lt;br /&gt;Assume that the requirement is to provide the student names and marks of all students in the school.Solution using scalar function would involve creating a function to get student name from student table and querying the student_marks table for obtaining rest of the details. Simple function definition would look like&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "green"&gt;&lt;i&gt;/******&amp;nbsp;Object:&amp;nbsp;&amp;nbsp;UserDefinedFunction&amp;nbsp;[dbo].[student_name]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;******/&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;ansi_nulls&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;quoted_identifier&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;FUNCTION&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;[Student_name]&lt;/b&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "#8000FF"&gt;@sid&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;RETURNS&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;AS&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@student_name&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@student_name&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;student_name&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;student&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@sid&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;RETURN&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@student_name&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;After creating the function, the following query using scalar function is executed. &lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;statistics&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;io&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dt&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;DATETIME&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dt&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Getdate&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Student_name&lt;/b&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;student_marks&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;20000&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Datediff&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;ms&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dt&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Getdate&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;b&gt; Perfomance Stats are provided below: &lt;/b&gt;&lt;br /&gt;   &lt;b&gt; Time taken :55640 ms ( 0:56 Seconds)  &lt;/b&gt;&lt;br /&gt;   &lt;b&gt; Rows Returned :100000 &lt;/b&gt;&lt;br /&gt;   &lt;b&gt; IO details &lt;/b&gt; : &lt;I&gt; Table Table 'student_marks'. Scan count 1, logical reads 415, physical reads 56, read-ahead reads 412, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. &lt;/I&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Query plan: &lt;/b&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_TcYicum3HIs/S_ueSy7RRqI/AAAAAAAAANw/LggSAg-13vQ/s1600/qp_functions.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 220px;" src="http://2.bp.blogspot.com/_TcYicum3HIs/S_ueSy7RRqI/AAAAAAAAANw/LggSAg-13vQ/s400/qp_functions.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5475143817744172706" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The query plan doesnt show whats exactly happening with in the function/ &lt;br /&gt;how many times was the function precisely called.So, to find the same, let me use the &lt;b&gt; DMV dm_exec_query_Stats &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Substring&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "blue"&gt;TEXT&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;statement_start_offset&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;/&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;2&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;CASE&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;statement_end_offset&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;WHEN&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;-&lt;/font&gt;&lt;font color = "black"&gt;1&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;THEN&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Datalength&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "blue"&gt;TEXT&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;ELSE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;statement_end_offset&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "silver"&gt;-&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;statement_start_offset&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;/&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;2&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;statement_text&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Isnull&lt;/b&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "fuchsia"&gt;&lt;i&gt;Object_name&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;objectid&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'-'&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;obj_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;execution_count&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;max_elapsed_time&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;max_logical_reads&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;max_logical_writes&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_exec_query_stats&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;OUTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;APPLY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Dm_exec_sql_text&lt;/b&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;plan_handle&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sqltext&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;TEXT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;LIKE&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'%dm_exec_query_Stats%'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ORDER&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;execution_count&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;DESC&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://3.bp.blogspot.com/_TcYicum3HIs/S_ue7DbZaeI/AAAAAAAAAN4/i5OhxxZQl6k/s1600/functions.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 800px; height: 250px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/S_ue7DbZaeI/AAAAAAAAAN4/i5OhxxZQl6k/s400/functions.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5475144509368658402" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Execution count column on dm_exec_query_stats DMV clearly shows that the query &lt;u&gt; Select dbo.student_name(sid),* From student_marks where sid &lt; 20000 &lt;/u&gt; in the function dbo.student_name has been executed &lt;b&gt; 10000 times &lt;/b&gt;, which is exactly the same number of rows returned. So its basically passing a number ( sid ) and making the  functions' code execute over and over which makes it painfully slow.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Let us Consider the join based solution: &lt;/b&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;statistics&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;io&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dt&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;DATETIME&lt;/i&gt;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dt&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Getdate&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;student_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;student_marks&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;student_marks&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;student&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;student_marks&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;student&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AND&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;student_marks&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;sid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;20000&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Datediff&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;ms&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dt&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Getdate&lt;/i&gt;&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Performance Stats : &lt;/b&gt;&lt;br /&gt;   &lt;b&gt; Time taken : 2876 ms ( 2.9 Seconds)  &lt;/b&gt;&lt;br /&gt;   &lt;b&gt; Rows Returned: 100,000 &lt;/b&gt;&lt;br /&gt;   &lt;b&gt; IO details : &lt;/b&gt; &lt;I&gt; Table 'student_marks'. Scan count 1, logical reads 415, physical reads 3, read-ahead reads 412, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;br /&gt;Table 'Student'. Scan count 1, logical reads 60, physical reads 1, read-ahead reads 58, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. &lt;/I&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Query plan: &lt;/b&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_TcYicum3HIs/S_umjU4OUFI/AAAAAAAAAOA/XpbY2ZMzv9I/s1600/qp_join.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 338px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/S_umjU4OUFI/AAAAAAAAAOA/XpbY2ZMzv9I/s400/qp_join.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5475152897829130322" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Join based solution completes the query in less than three seconds, which is 20 times faster than Scalar functions. IO also indicates lesser number of reads. The student table is also read only once against 100,000 times as in the case of scalar functions. Query Stats DMV confirm the same.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_TcYicum3HIs/S_umwS8OitI/AAAAAAAAAOI/jtPv88gvLdk/s1600/join_query_stats.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 800px; height: 250px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/S_umwS8OitI/AAAAAAAAAOI/jtPv88gvLdk/s400/join_query_stats.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5475153120647350994" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;So the conclusion is, one should always avoid using scalar functions as much as possible and should be used only when we don't have any other option to replace it.Complex string manipulation requirements or date calculation requirements are perhaps, plausible excuses for Scalar functions, but still they have to be used with caution as they have been found as performance bottlenecks more often than not.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-5397941010732835436?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/5397941010732835436/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=5397941010732835436&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/5397941010732835436'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/5397941010732835436'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/05/performance-comparison-functions-versus.html' title='Performance comparison - Functions versus Joins'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/_TcYicum3HIs/S_ueSy7RRqI/AAAAAAAAANw/LggSAg-13vQ/s72-c/qp_functions.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-4029072729142001931</id><published>2010-05-19T00:28:00.001-07:00</published><updated>2010-05-19T00:45:55.707-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><title type='text'>SQLDiag</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;When one needs to monitor a servers performance, one has to make use of the Performance monitor to capture the system parameters and obtain the bigger picture, identify the bottleneck and then use profiler utility to identify the queries causing the performance slowdown. &lt;br /&gt;&lt;br /&gt;Instead of using multiple tools to gather performance data, SQLDIAG is a single utility which is capable of capturing the following.&lt;br /&gt;&lt;br /&gt;             * Queries executed and corresponding events.&lt;br /&gt;             * Performance counters&lt;br /&gt;             * Error logs&lt;br /&gt;             * SQL Server blocks and resource usage information.&lt;br /&gt;             * SQL Server version and configuration information.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;SQLDiag is a command line utility where one can configure the things to be captured in a XML file.SQLDiag is found in the drive in which SQL Executables are installed,with the path '..\Microsoft SQL Server\90\Tools\Binn\' . SQLDiag takes a few input parameters like XML file to be taken as input, place to write the captured results etc. Once started provides the captured results into the specified folder.&lt;br /&gt;Details about useful output files are provided below.&lt;br /&gt;&lt;br /&gt;    &lt;b&gt; 1)  SQLDIAG.BLG&lt;/b&gt; - Stores the perfmon counters values asked for&lt;br /&gt;    &lt;b&gt; 2)  &lt;servername&gt;_sp_trace.trc&lt;/b&gt; - Provides the profiler details/query &lt;br /&gt;                details&lt;br /&gt;    &lt;b&gt; 3)  ***_sp_SQLDiag_shutdown.OUT&lt;/b&gt;- Provides the sqlerrorlog,current &lt;br /&gt;            running queries,open transaction,configuration info, blocking query &lt;br /&gt;            info taken at the time of stopping SQLDiag.&lt;br /&gt;&lt;br /&gt;Sample call is provided below:&lt;br /&gt;&lt;br /&gt;         &lt;b&gt; START SQLDIAG /I "%cd%\customized.XML" /O "%cd%\SQLDiagOutput" /P &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;"%cd%" Refers to the current directory. customized.XML is the XML I prepared for capturing data. SQLServer by default ships SQLDIAG.XML which can be used to capture data. SQLDIAG.XML captures a whole lot of data with too many counters and too  &lt;br /&gt;many profiler events. Instead, I have created a customized XML which grabs important 15 - 20 counters and queries that are executed against the database. Customized.xml can be downloaded from &lt;a href = "http://www.mediafire.com/?2jjvjmzjn4y"&gt;here&lt;/a&gt;. There are very few resources on SQLDiag the &lt;br /&gt;net.Refer &lt;a href = "http://www.mssqltips.com/tip.asp?tip=1863"&gt;here&lt;/a&gt; for more details.&lt;br /&gt;&lt;br /&gt;SQLDiag's advantages and drawbacks are provided below:&lt;br /&gt;  &lt;br /&gt;       &lt;b&gt; Advantages: &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;        * Single tool to capture all the data required.&lt;br /&gt;        * Can be easily reused across servers as its just a single XML file which &lt;br /&gt;          contains all the configuration and settings&lt;br /&gt;        * Can be run as a Service&lt;br /&gt;&lt;br /&gt;       &lt;b&gt; Disadvantages :&lt;/b&gt;&lt;br /&gt;        * Cant be configured from a remote machine. One needs to be logged in to the &lt;br /&gt;          actual server.&lt;br /&gt;          Stores the data only on the monitored instance.&lt;br /&gt;        * Changing XML can be bit tedious and difficult.&lt;br /&gt;        * No performance gain when compared to profiler/perfmon. Incurs the same &lt;br /&gt;          amount of resouces as other tools.&lt;br /&gt;        * Doesnt store data on the database which can make it easier for analysis. &lt;br /&gt;          Stores only on trc/text files.&lt;br /&gt;   &lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-4029072729142001931?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/4029072729142001931/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=4029072729142001931&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/4029072729142001931'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/4029072729142001931'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/05/sqldiag.html' title='SQLDiag'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-6394957777471897929</id><published>2010-05-11T04:07:00.000-07:00</published><updated>2010-05-11T04:20:16.942-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><title type='text'>Getting the IP Address and port no on SQL Server 2000,SQL Server 2005</title><content type='html'>&lt;p&gt;&lt;br /&gt;This is my 50th Post. Thanks a lot for all the readers for your support.&lt;br /&gt;&lt;br /&gt;Now for the actual topic.&lt;br /&gt;Finding the IP address and port number of a SQL Server from a remote client can be tricky especially if you are on SQL Server 2000.I am never a big fan of &lt;b&gt; xp_cmdshell &lt;/b&gt;. So I am not using xp_cmdshell 'ipconfig'. The alternative method that works for me is reading the SQL Error log thro &lt;b&gt; sp_readerrorlog &lt;/b&gt;. Run the following command&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Sp_readerrorlog&lt;/font&gt;&lt;/b&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;sp_readerrorlog provides an output as shown below.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_TcYicum3HIs/S-k6yVnOgWI/AAAAAAAAANg/_aShJW8sIrw/s1600/sp_readerrorlog.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 347px; height: 400px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/S-k6yVnOgWI/AAAAAAAAANg/_aShJW8sIrw/s400/sp_readerrorlog.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5469967858887721314" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;If you scroll 20 to 25 lines ( varies from server to server ) of output, you should find the text 'SQL Server is listening on IP Address : Port no'. Note that we can also find the port at which SQL Server is listening for connections. By default SQL Server listens for connections at 1433. &lt;br /&gt;&lt;br /&gt;If you are on SQL Server 2005, then you are life is made lot easier using the DMV &lt;b&gt; sys.dm_exec_connections&lt;/b&gt;.Execute the following query&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_exec_connections&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_TcYicum3HIs/S-k8LK8_UrI/AAAAAAAAANo/NwOIOeY7Ii0/s1600/dm_exec_connections.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 237px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/S-k8LK8_UrI/AAAAAAAAANo/NwOIOeY7Ii0/s400/dm_exec_connections.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5469969385034568370" /&gt;&lt;/a&gt;&lt;br /&gt;The local_net_address,local_tcp_port columns on the DMV dm_exec_connections provides the IP Address of the SQL Server and the port at which SQL Server listens for connections. Additionally columns Client_net_address,Client_tcp_port indicate the ip addresses that are currently connected to SQL Server and the the source ports from which they are connecting to.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-6394957777471897929?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/6394957777471897929/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=6394957777471897929&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/6394957777471897929'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/6394957777471897929'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/05/getting-ip-address-and-port-no-on-sql.html' title='Getting the IP Address and port no on SQL Server 2000,SQL Server 2005'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_TcYicum3HIs/S-k6yVnOgWI/AAAAAAAAANg/_aShJW8sIrw/s72-c/sp_readerrorlog.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-1814531584581620410</id><published>2010-05-03T20:16:00.000-07:00</published><updated>2010-05-03T20:29:19.807-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Links'/><title type='text'>Clustering - few useful  documents</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;This is One of best articles I have read on clustering.&lt;br /&gt;Very complete, and provides info on just about every task we happen to perform&lt;br /&gt;on clustered servers. Must have for a DBA, if any of your servers are clustered.&lt;br /&gt;Download from &lt;a href = "http://download.microsoft.com/download/3/7/f/37fb8b19-91b5-484e-b6ff-d6ab565c7783/SQLServer2005FOC.doc"&gt;here&lt;/a&gt;. Best part its a white paper provided by Microsoft itself :)&lt;br /&gt;&lt;br /&gt;In case, if you want to read on how to install clustered sql server with some simple &lt;br /&gt;screen-shots, you can also refer to embarrassingly simple article written by Brad Mcghee &lt;a href = "http://www.sql-server-performance.com/articles/clustering/cluster_sql_server_2005_p1.aspx"&gt;here&lt;/a&gt;. &lt;br /&gt;&lt;br /&gt;Enjoy.&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-1814531584581620410?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/1814531584581620410/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=1814531584581620410&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/1814531584581620410'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/1814531584581620410'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/05/clustering-few-useful-documents.html' title='Clustering - few useful  documents'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-7944560008289227</id><published>2010-04-30T21:56:00.000-07:00</published><updated>2010-04-30T22:19:31.857-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Storage Engine'/><title type='text'>Plan Cache - Part 3 - What gets recompiled</title><content type='html'>&lt;p&gt;&lt;br /&gt;As a continuation of series of posts on Plan caching, let us get started with Part -3 &lt;br /&gt;Earlier posts are given below:&lt;br /&gt;&lt;br /&gt;        &lt;a href = "http://strictlysql.blogspot.com/2010/04/plan-cache-introduction.html" &gt;Plan caching - part-1 - Introduction&lt;/a&gt;&lt;br /&gt;        &lt;a href = "http://strictlysql.blogspot.com/2010/04/plan-cache-part-2-what-gets-cached.html" &gt;Plan caching - part-2 - What gets cached&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Last post explained how stored procedures,prepared statements are cached. But there are a few scenarios where caching doesn't work, and object/query plan in the cache gets recompiled. Recompilation is the process where SQL Server finds that the plan in the cache is no good/invalid and creates a new plan for the query submitted. So lets get started. The following scenarios cause recompilation.&lt;br /&gt;&lt;br /&gt;* &lt;b&gt; A change in the structure/definition of the object &lt;/b&gt;&lt;br /&gt;        If the structure of the stored proc/table(Add/delete column, constraint,  &lt;br /&gt;        index) has changed since last execution then the plan is obviously         &lt;br /&gt;        invalid and hence it has to be recompiled. &lt;br /&gt;&lt;br /&gt;       We are using the same stored procedure test_plan_caching defined in the last post. Consider the following script.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;freeproccache&lt;/font&gt;&amp;nbsp;&lt;font color = "green"&gt;&lt;i&gt;&amp;#45;&amp;#45;Clear&amp;nbsp;the&amp;nbsp;cache&amp;nbsp;as&amp;nbsp;usual&lt;/font&gt;&lt;/i&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Test_plan_caching&lt;/font&gt;&lt;/b&gt;&amp;nbsp;&lt;font color = "red"&gt;'moscow'&lt;/font&gt;&amp;nbsp;&lt;font color = "green"&gt;&lt;i&gt;&amp;#45;&amp;#45;Call&amp;nbsp;the&amp;nbsp;SP&lt;/font&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ALTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;PROCEDURE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;[Test_plan_caching]&lt;/font&gt;&lt;/b&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@city&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;AS&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_xid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;product_xid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;trans_city&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;qty&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;comments&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;balance_left&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_product_customer&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;trans_city&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@city&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;ORDER&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_xid&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;DESC&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;font color = "green"&gt;&lt;br /&gt;&lt;br /&gt;&lt;i&gt;&amp;#45;&amp;#45;Forcing&amp;nbsp;an&amp;nbsp;alter&amp;nbsp;to&amp;nbsp;the&amp;nbsp;sp,&amp;nbsp;though&amp;nbsp;no&amp;nbsp;logical&amp;nbsp;change&amp;nbsp;has&amp;nbsp;been&amp;nbsp;done&amp;nbsp;&lt;/font&gt;&lt;/i&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Test_plan_caching&lt;/font&gt;&lt;/b&gt;&amp;nbsp;&lt;font color = "red"&gt;'moscow'&lt;/font&gt;&lt;font color = "green"&gt;&lt;i&gt;&amp;#45;&amp;#45;Call&amp;nbsp;the&amp;nbsp;SP&lt;/font&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TEXT&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;usecounts&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;size_in_bytes&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cacheobjtype&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;objtype&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_exec_cached_plans&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;OUTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;APPLY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Dm_exec_sql_text&lt;/font&gt;&lt;/b&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;plan_handle&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sqltext&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;TEXT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;LIKE&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'%dm_exec_cached_plans%'&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TcYicum3HIs/S9u2ulZo3TI/AAAAAAAAANQ/o-cfLckyIzc/s1600/alter_proc.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 259px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/S9u2ulZo3TI/AAAAAAAAANQ/o-cfLckyIzc/s400/alter_proc.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5466163484174114098" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Alter made on the sp forced the optimizer to recreate the plan.&lt;br /&gt;Similar changes that can cause such recompilations are adding/dropping an index,changing a trigger/proc/view definition etc.&lt;br /&gt;&lt;br /&gt; * &lt;b&gt; A change in the connection parameters using SET command &lt;/b&gt;.&lt;br /&gt;&lt;br /&gt;    Changing the following connection parameters using SET keyword can cause recompilation.&lt;br /&gt;    &lt;br /&gt;     ANSI_NULL_DFLT_OFF/ANSI_NULL_DFLT_ON/ANSI_NULLS/ANSI_PADDING/ANSI_WARNINGS&lt;br /&gt;     /ARITHABORT/CONCAT_NULL_YIELDS_NULL/DATEFIRST/DATEFORMAT/FORCEPLAN/LANGUAGE&lt;br /&gt;     /NO_BROWSETABLE/NUMERIC_ROUNDABORT/QUOTED_IDENTIFIER&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;freeproccache&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Test_plan_caching&lt;/font&gt;&lt;/b&gt;&amp;nbsp;&lt;font color = "red"&gt;'San&amp;nbsp;Franciso'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;ansi_null_dflt_off&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ON&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Test_plan_caching&lt;/font&gt;&lt;/b&gt;&amp;nbsp;&lt;font color = "red"&gt;'San&amp;nbsp;Franciso'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_TcYicum3HIs/S9u3QGb_a5I/AAAAAAAAANY/ElmLHoF3WjA/s1600/SET_chg.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 302px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/S9u3QGb_a5I/AAAAAAAAANY/ElmLHoF3WjA/s400/SET_chg.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5466164059978034066" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Query stats DMV clearly shows two plans. One for the plan generated before setting ANSI_NULL_DFLT_OFF and other for the plan with different value on ANSI_NULL_DFLT_OFF.&lt;br /&gt;&lt;br /&gt;Note that a change of option 'ANSI_NULL/QUOTED_IDENTIFIER' doesn't affect a Stored proc or Table valued function. But it does affect Adhoc queries or prepared statements.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; * A Forced recompilation &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;   When a Object is marked for recompilation using sp_recompile stored procedure or when with recompile hint is used then query is recompiled.&lt;br /&gt;Example:&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Sp_recompile&lt;/font&gt;&lt;/b&gt;&amp;nbsp;&lt;font color = "red"&gt;'test_plan_caching'&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;The statement above ensures that next call made to test_plan_caching results in a recomplie.&lt;br /&gt;&lt;br /&gt;  When a procedure is created with an option &lt;b&gt; WITH RECOMPILE&lt;/b&gt;, any call made to the stored proc &lt;br /&gt;  is always recompiled. &lt;br /&gt; &lt;br /&gt;  usage of hint &lt;b&gt; OPTION RECOMPILE  &lt;/b&gt; causes a statement level recompilation.&lt;br /&gt;  The scenarios where these options are useful are potential posts on their own. Will cover them soon.&lt;br /&gt;&lt;br /&gt; &lt;b&gt; * A change in statistics, caused by changes done to most of the rows in the table.&lt;/b&gt;&lt;br /&gt;    A rough algorithm of how SQL Server decides whether the table needs an updated statistics is given below. The algorithm changes is based on RT factor ( Recompilation threshold ) which depends on the number of changes made to the table. &lt;br /&gt;            If a table contains less than 500 rows then atleast 500 changes are required to cause a recompilation.If a table has more than 500 rows, then number of changes required are  500 + 20 % of rowcount.&lt;br /&gt;&lt;br /&gt;   As the algorithm changes with version and service pack releases, its approximate.  &lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-7944560008289227?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/7944560008289227/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=7944560008289227&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7944560008289227'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7944560008289227'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/04/plan-cache-part-3-what-gets-recompiled.html' title='Plan Cache - Part 3 - What gets recompiled'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_TcYicum3HIs/S9u2ulZo3TI/AAAAAAAAANQ/o-cfLckyIzc/s72-c/alter_proc.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-5746411983720798422</id><published>2010-04-24T22:07:00.001-07:00</published><updated>2010-04-24T22:30:09.195-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Storage Engine'/><title type='text'>Plan cache - part 2 - What gets cached</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;br /&gt;In general, we can classify queries submitted into three catogories.&lt;br /&gt;&lt;br /&gt;They are &lt;br /&gt;1) &lt;b&gt; Adhoc queries &lt;/b&gt; -&gt; The ones submitted thro query analyzer or the queries from applications which are not prepared statments ( executed as just statement without setting any parameter ).&lt;br /&gt;2) &lt;b&gt; SQL objects &lt;/b&gt; -&gt; calls made to Stored procs, Triggers, UDFs, system and extended procs &lt;br /&gt;3) &lt;b&gt; Prepared Statements &lt;/b&gt; -&gt; Sp_executesql with parameters,Prepared statements from applications.&lt;br /&gt; &lt;br /&gt;Calls to sql objects and prepared statements are more likely to be cached. Let us see  it in detail.Consider the following adhoc query:&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;freeproccache&lt;/font&gt;&amp;nbsp;&lt;font color = "green"&gt;&lt;i&gt;&amp;#45;&amp;#45;&amp;nbsp;Just&amp;nbsp;to&amp;nbsp;free&amp;nbsp;the&amp;nbsp;cache&amp;nbsp;before&amp;nbsp;we&amp;nbsp;start&amp;nbsp;the&amp;nbsp;experiments.&lt;/font&gt;&lt;/i&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_xid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;product_xid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;trans_city&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;qty&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;comments&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;balance_left&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_product_customer&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;trans_city&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'San&amp;nbsp;Franciso'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ORDER&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_xid&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;DESC&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;Let us change the parameter and execute again&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_xid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;product_xid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;trans_city&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;qty&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;comments&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;balance_left&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_product_customer&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;trans_city&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'Perth'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ORDER&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_xid&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;DESC&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;To check whether the query was cached or not let me fire the query on DMV cached plans.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TEXT&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;usecounts&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;size_in_bytes&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cacheobjtype&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;objtype&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_exec_cached_plans&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;OUTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;APPLY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Dm_exec_sql_text&lt;/font&gt;&lt;/b&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;plan_handle&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sqltext&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;TEXT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;LIKE&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'%dm_exec_cached_plans%'&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://4.bp.blogspot.com/_TcYicum3HIs/S9PQAyL7z6I/AAAAAAAAAM4/r9rFp4muSos/s1600/adhoc_query.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 195px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/S9PQAyL7z6I/AAAAAAAAAM4/r9rFp4muSos/s400/adhoc_query.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5463939484820950946" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The usecounts column has a value 1 which indicates clearly that the query was not cached. By default, adhoc queries are cached only when there is a exact match. A hange in parameter, even a change in case  or additional space in the query results in sql server not reusing the query plan prepared eariler.&lt;br /&gt;&lt;br /&gt;Let us make it a Stored proc and try the query again.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;PROCEDURE&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Test_plan_caching&lt;/font&gt;&lt;/b&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@city&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;50&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;AS&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_xid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;product_xid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;trans_city&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;qty&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;comments&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;balance_left&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_product_customer&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;trans_city&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@city&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;ORDER&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_xid&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;DESC&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Test_plan_caching&lt;/font&gt;&lt;/b&gt;&amp;nbsp;&lt;font color = "red"&gt;'San&amp;nbsp;Franciso'&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Test_plan_caching&lt;/font&gt;&lt;/b&gt;&amp;nbsp;&lt;font color = "red"&gt;'Perth'&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;A query to DMV cached plan shows that the query plan has been reused.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://2.bp.blogspot.com/_TcYicum3HIs/S9PQpmhKjCI/AAAAAAAAANA/TEQUygLoytA/s1600/test_plan_cache_proc.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 149px;" src="http://2.bp.blogspot.com/_TcYicum3HIs/S9PQpmhKjCI/AAAAAAAAANA/TEQUygLoytA/s400/test_plan_cache_proc.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5463940186063408162" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Now let us try sp_executesql method. Consider the following script&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@SQLString&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;NVARCHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;1000&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@paramstring&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;NVARCHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;100&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@city&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;100&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@SQLString&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'Select&amp;nbsp;supplier_xid,product_xid,trans_city,qty,comments,balance_left&amp;nbsp;from&amp;nbsp;'&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'Supplier_Product_Customer&amp;nbsp;where&amp;nbsp;trans_city&amp;nbsp;=&amp;nbsp;@variable&amp;nbsp;order&amp;nbsp;by&amp;nbsp;supplier_xid&amp;nbsp;desc&amp;nbsp;'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@paramstring&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'@variable&amp;nbsp;varchar(100)'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@city&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'San&amp;nbsp;Franciso'&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Sp_executesql&lt;/font&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@SQLString&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@paramstring&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@variable&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@City&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@city&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'Perth'&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Sp_executesql&lt;/font&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@SQLString&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@paramstring&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@variable&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@City&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;Note that we have changed the parameters by setting a new value to input variable and executed the same query. &lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_TcYicum3HIs/S9PRYzDuwOI/AAAAAAAAANI/wdqIBJXzNmI/s1600/prepared_Stmt_.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 131px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/S9PRYzDuwOI/AAAAAAAAANI/wdqIBJXzNmI/s400/prepared_Stmt_.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5463940996883464418" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;DMV cached plan shows that query  is getting reused. Bear in mind that mere usage of sp_executesql without using input parameter doesnt reuse the plan.&lt;br /&gt;&lt;br /&gt;Note that the object type column on DMV cached plan indicates the kind of object that has been cached. Proc refers to procedure, Prepared refers to prepared plans created by sp_executesql and by application queries and Adhoc refers to adhoc queries.&lt;br /&gt;&lt;br /&gt;There are still a few exception scenarios where a stored procedure plan doesnt get effectively reused.We will explore them more in the next post.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-5746411983720798422?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/5746411983720798422/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=5746411983720798422&amp;isPopup=true' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/5746411983720798422'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/5746411983720798422'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/04/plan-cache-part-2-what-gets-cached.html' title='Plan cache - part 2 - What gets cached'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_TcYicum3HIs/S9PQAyL7z6I/AAAAAAAAAM4/r9rFp4muSos/s72-c/adhoc_query.GIF' height='72' width='72'/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-6333923006214457458</id><published>2010-04-16T22:30:00.000-07:00</published><updated>2010-04-16T22:46:38.099-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Storage Engine'/><title type='text'>Plan cache - Introduction</title><content type='html'>&lt;p&gt;&lt;br /&gt;When a query is submitted to SQL server, the query optimiser checks the syntax, verifies the objects  and prepares a query plan. A query plan prepared is stored in a area of memory called &lt;b&gt; plan cache &lt;/b&gt;. A plan placed in plan cache will be reused when a same query or 'similar query' is submitted to the query analyzer. Preparing a plan everytime can be expensive and time consuming task &lt;br /&gt;and hence the query optimiser attempts to resuse a plan when it is supposed to.&lt;br /&gt;&lt;br /&gt;A quick example.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dt&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;DATETIME&lt;/font&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dt&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Getdate&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_xid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;product_xid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;trans_city&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;qty&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;comments&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;balance_left&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_product_customer&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;trans_city&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;IN&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'San&amp;nbsp;Franciso'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'Perth'&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ORDER&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;supplier_xid&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;DESC&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Datediff&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;ms&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dt&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Getdate&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;First, the above query is executed. The table contains about 110,000 rows and the query returns about 45000 rows.The query returned the results first time in &lt;b&gt; 1217 ms.&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Let us execute the same script again. The script completes in &lt;b&gt; 626 ms &lt;/b&gt; which is about half of first execution's time. When executing for the second time,The table's query plan is already in the cache and hence it is reused. We can check whether our query is cached or not using the dmv &lt;b&gt; sys.dm_exec_cached_plans&lt;/b&gt;. The following query helps us find the same.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TEXT&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;usecounts&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;size_in_bytes&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;cacheobjtype&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;objtype&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_exec_cached_plans&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;OUTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;APPLY&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Dm_exec_sql_text&lt;/font&gt;&lt;/b&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;plan_handle&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sqltext&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;TEXT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;LIKE&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'%dm_exec_cached_plans%'&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_TcYicum3HIs/S8lJVCIVf2I/AAAAAAAAAMs/ngnqD64Xy54/s1600/dm_exec_plan_cache.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 240px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/S8lJVCIVf2I/AAAAAAAAAMs/ngnqD64Xy54/s400/dm_exec_plan_cache.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5460976648861351778" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Observe the result provided above. The text column provides the query and usercounts indicates how many times the plan has been used. For the above script, usecounts value is 2 and hence it indicates that the query has been reused.&lt;br /&gt;&lt;br /&gt;In general, the queries that are 'likely' to be cached and used are as follows.&lt;br /&gt;&lt;br /&gt;              * Stored procedures, Triggers, Table valued functions.&lt;br /&gt;              * Prepared Statements used from applications ( from java/.net/c# etc )&lt;br /&gt;              * Queries executed using sp_executesql with input paramters.&lt;br /&gt;&lt;br /&gt;As always there are a few exceptions and thats the reason the word 'likely' is in place.&lt;br /&gt;&lt;br /&gt;What is written here is just the tip of the iceberg and we will explore more and more on the caching, pros and cons of caching,and the way plan cache works in the next few posts.&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-6333923006214457458?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/6333923006214457458/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=6333923006214457458&amp;isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/6333923006214457458'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/6333923006214457458'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/04/plan-cache-introduction.html' title='Plan cache - Introduction'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_TcYicum3HIs/S8lJVCIVf2I/AAAAAAAAAMs/ngnqD64Xy54/s72-c/dm_exec_plan_cache.GIF' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-8842738194403562210</id><published>2010-04-12T01:27:00.000-07:00</published><updated>2010-04-12T02:20:58.258-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><title type='text'>Job still runs inspite of disabling</title><content type='html'>&lt;p&gt;&lt;br /&gt;When one has to disable a job/group of jobs, common way is to do it from&lt;br /&gt;Enterprise manager or using the Graphical user interface. At times, when one is &lt;br /&gt;trying to update for a group of jobs then one way people attempt to do it&lt;br /&gt;is by updating sysjobs system table directly. ie.,&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;UPDATE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sysjobs&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;enabled&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;0&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;name&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;IN&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'job&amp;nbsp;a'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'job&amp;nbsp;b'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'job&amp;nbsp;c'&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;This is a very dangerous way of doing it, as it doesn't disable completely.&lt;br /&gt;Worse is that when you update sysjobs directly,the job appears to be disabled&lt;br /&gt;to on the GUI, but still the job continues to run as scheduled.&lt;br /&gt;&lt;br /&gt;For example consider the following job Test.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TcYicum3HIs/S8LjcvENQAI/AAAAAAAAAMc/vdTdf9LENoY/s1600/test.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 279px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/S8LjcvENQAI/AAAAAAAAAMc/vdTdf9LENoY/s400/test.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5459175781136416770" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The job is currently enabled. &lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;UPDATE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sysjobs&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;enabled&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;0&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;name&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'test'&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;After executing the query provided above, the job appears to be disabled as shown below.&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_TcYicum3HIs/S8LkhBvkJtI/AAAAAAAAAMk/n4Lp5ZzFOlc/s1600/disbaled.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 247px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/S8LkhBvkJtI/AAAAAAAAAMk/n4Lp5ZzFOlc/s400/disbaled.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5459176954381215442" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;However, the job continues to run as scheduled. The reason is that the properties&lt;br /&gt;and other attributes of the job are kept in memory and a update to sysjobs&lt;br /&gt;doesnt update the SQL Agent. The correct way of enabling/disabling a job&lt;br /&gt;or updating any property of a job will be to use system stored procedure &lt;b&gt; sp_update_job &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Sample call for disabling a job is shown below.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;msdb&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Sp_update_job&lt;/font&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@job_name&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'test'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@enabled&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;0&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;For updating a job schedule use &lt;b&gt; sp_update_schedule/sp_update_jobschedule &lt;/b&gt;&lt;br /&gt;For more details refer books online &lt;a href = "http://msdn.microsoft.com/en-us/library/ms188745.aspx"&gt; here &lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Also, just a short script which provides a script to disable all &lt;br /&gt;jobs in your server.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'EXEC&amp;nbsp;sp_update_job&amp;nbsp;@job_name&amp;nbsp;=&amp;nbsp;'''&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;name&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;''',@enabled&amp;nbsp;=&amp;nbsp;0&amp;nbsp;'&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;CHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;10&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'&amp;nbsp;GO&amp;nbsp;'&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;CHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;10&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sysjobs&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;View the results of the script in text mode ( instead of grid)&lt;br /&gt;to get the script correctly.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-8842738194403562210?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/8842738194403562210/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=8842738194403562210&amp;isPopup=true' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/8842738194403562210'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/8842738194403562210'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/04/job-still-runs-inspite-of-disabling.html' title='Job still runs inspite of disabling'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_TcYicum3HIs/S8LjcvENQAI/AAAAAAAAAMc/vdTdf9LENoY/s72-c/test.GIF' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-4929864033735062225</id><published>2010-04-07T23:53:00.000-07:00</published><updated>2010-04-08T00:32:21.936-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL 2k8'/><category scheme='http://www.blogger.com/atom/ns#' term='T SQL'/><title type='text'>Sparse column - Maximum size of a row</title><content type='html'>&lt;p&gt;&lt;br /&gt;Just to clarify, by including a sparse column, the maximum size of the row &lt;b&gt; DOES NOT &lt;/b&gt; reduce to 8018 bytes.Quite a few sites have mentioned that sparse column reduces the max row size to 8018 , but its not true.What is true is that the total size occupied by sparse columns alone shouldn't exceed 8019 bytes. The row size ( sparse columns size + normal columns size ) can be greater than 8018 bytes and has the normal row limitation of 8060 bytes. The script provided below illustrates the same.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DROP&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[sparse_col]&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[sparse_col]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[dt]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[DATETIME]&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[value]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;data&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;CHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;500&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sparse_data&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;CHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;7500&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;SPARSE NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[sparse_col]&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Getdate&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;0&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "red"&gt;'sparse_data'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "red"&gt;'sparse&amp;nbsp;data'&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;The insert is successful without any errors.&lt;br /&gt;&lt;br /&gt;Note that the sparse column 'sparse_data' has a size of 7500 bytes.&lt;br /&gt;Total size of the row when all columns have non null value will exceed 8018 bytes.&lt;br /&gt;Actual size of the row can be checked using DMV dm_db_index_physical_stats as shown below.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dbid&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/font&gt;&lt;/i&gt;&lt;font color = "silver"&gt;;&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dbid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Db_id&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;;&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Object_name&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;object_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[table_name]&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;record_count&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;min_record_size_in_bytes&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;max_record_size_in_bytes&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;avg_record_size_in_bytes&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "#FF0080"&gt;&lt;b&gt;Dm_db_index_physical_stats&lt;/font&gt;&lt;/b&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "#8000FF"&gt;@dbid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'Detailed'&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Object_name&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;object_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'sparse_col'&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_TcYicum3HIs/S72CFEclQrI/AAAAAAAAAMU/K6bOi-E45xo/s1600/row_size_with_sparse.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 152px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/S72CFEclQrI/AAAAAAAAAMU/K6bOi-E45xo/s400/row_size_with_sparse.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5457661347047490226" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The size of the row is 8045 bytes, which is above 8018 bytes.Thus its clear that adding a column as sparse column doesnt reduce the size of the row to be 8018 bytes.&lt;br /&gt;&lt;br /&gt;Now let us see an example which generates the actual error.&lt;br /&gt;Run the following script.&lt;br /&gt;The script used earlier is slightly modified by changing the 'data' column to sparse. Rest of the structure remains the same with no changes done to the length of the columns.So, the sparse columns on the table are data,sparse whose combined size are 8000 + few bytes used for internal use for storing sparse data.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DROP&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[sparse_col]&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[sparse_col]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[dt]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[DATETIME]&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[value]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;data&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;CHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;500&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;SPARSE NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sparse_data&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;CHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;7500&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;SPARSE NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[sparse_col]&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Getdate&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;0&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "red"&gt;'some  data'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "red"&gt;'sparse&amp;nbsp;data'&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;The insert fails with the error&lt;br /&gt;&lt;font color = "red"&gt;Msg 576, Level 16, State 5, Line 1&lt;br /&gt;Cannot create a row that has sparse data of size 8031 which is greater than the allowable maximum sparse data size of 8019.&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;The reason is that the total size consumed by sparse_data,data column is 8000 bytes + 31 bytes used for internal use which exceeds the total size allowed (8019 bytes) for sparse columns.&lt;br /&gt;&lt;br /&gt;Reference: SQL Server 2008 Internals by Kalen Delaney :)&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-4929864033735062225?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/4929864033735062225/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=4929864033735062225&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/4929864033735062225'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/4929864033735062225'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/04/sparse-column-maximum-size-of-row.html' title='Sparse column - Maximum size of a row'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_TcYicum3HIs/S72CFEclQrI/AAAAAAAAAMU/K6bOi-E45xo/s72-c/row_size_with_sparse.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-7406894260413670463</id><published>2010-04-02T06:48:00.000-07:00</published><updated>2010-04-05T19:54:51.395-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL 2k8'/><title type='text'>Sparse Columns</title><content type='html'>&lt;p&gt;&lt;br /&gt;Sparse columns are a new column property introduced in SQL Server 2008, to improve the storage of NULL values on columns.When a column is defined as SPARSE, and if a null is inserted on the column then the column doesnt occupy any space at all.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Some facts: &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;*&lt;/b&gt; A Non varying column with a NULL value occupies the entire length of the column.( Datetime column with null occupies 8 bytes)&lt;br /&gt;  &lt;b&gt;*&lt;/b&gt; A varying column(varchar) with a NULL value occupies a minimum of two bytes.&lt;br /&gt;&lt;br /&gt;So, by defining a column sparse one can save the space that is wasted when NULL values are inserted.But Sparse columns come at a cost. When a sparse column &lt;br /&gt;contains NON NULL value / Valid value, it consumes extra 4 bytes of space. So,&lt;br /&gt;one is expected to use sparse columns only when 90% of the value on the sparse column is expected to be null.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Example: &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Consider the following table.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[sparse_col]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[dt]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[DATETIME]&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[value]&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;data&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;CHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;500&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;Data column doesnt have a sparse property defined on it. Let me insert 10,000 rows using the following script&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/font&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHILE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;10000&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[sparse_col]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Getdate&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;The spaceused occupied by the table is 6280 kb as shown below.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_TcYicum3HIs/S7YJw3oYJBI/AAAAAAAAAME/BU2pZMXf7-Q/s1600/normal_column_space.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 295px;" src="http://4.bp.blogspot.com/_TcYicum3HIs/S7YJw3oYJBI/AAAAAAAAAME/BU2pZMXf7-Q/s400/normal_column_space.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5455558733777609746" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Let us recreate the table with 'data' column defined as a sparse column.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[sparse_col]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[dt]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[DATETIME]&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[value]&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;data&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;CHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;500&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt; Sparse NULL&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;The same script provided above is used to insert the rows. The space occupied is just 264KB after switching to sparse column.&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_TcYicum3HIs/S7YQjkNvbLI/AAAAAAAAAMM/L_5Kaz6BbHo/s1600/sparse_column_space.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 309px;" src="http://2.bp.blogspot.com/_TcYicum3HIs/S7YQjkNvbLI/AAAAAAAAAMM/L_5Kaz6BbHo/s400/sparse_column_space.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5455566201808710834" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Size of a row &lt;/b&gt;:&lt;br /&gt;&lt;br /&gt;Size of each row in SQL Server 2008 is limited to 8060 bytes. Sparse columns doesnt allow one to exceed this limitation.The size of all Non null sparse columns of a row is limited to 8019 byes.&lt;br /&gt;&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;Let us change the table definition , by adding addtional column&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;ALTER&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sparse_col&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;ADD&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;data2&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;CHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;8000&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;Sparse NULL&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sparse_col&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Getdate&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;5&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;The above insert works. But the below doesnt.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sparse_col&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Getdate&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;5&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "red"&gt;'x'&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "red"&gt;'x'&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;The insert fails with a error indicating that the size of the row exceeded 8060 byte limitation.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Adding more than 1024 columns: &lt;/b&gt;&lt;br /&gt;By default SQL Server 2008 allows 1024 columns per table. But, by using sparse columns one can increase the number of columns to 30,000. However, there can be a maximum of 1024 non sparse columns on the table and the rest have to be sparse columns. The sparse columns defined will have to be grouped using the Column set feature introduced in SQL Server 2008. Column set is a untyped XML column which will group all the sparse columns on the table. The Column set column is a virtual column which doesnt get stored in the table. For more details refer &lt;a href = "http://msdn.microsoft.com/en-us/library/cc280521.aspx"&gt;here&lt;/a&gt;. &lt;br /&gt;&lt;br /&gt;A table with more than 1024 columns is called a wide table and it can be defined with the following script.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DROP&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[sparse_col]&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;GO&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;[dbo]&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;[sparse_col]&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[dt]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[DATETIME]&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[value]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;[INT]&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;data&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;CHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;500&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;Sparse NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;specialpurposecolumns&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;XML COLUMN_SET FOR ALL_SPARSE_COLUMNS&lt;/font&gt;&lt;/i&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;A table with the column set feature is created using the above script. &lt;I&gt;SpecialPurposeColumns&lt;/I&gt; is a column set which will be grouping all the sparse columns in the table.The script provided below adds more sparse columns to the table.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/font&gt;&lt;/i&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@sql&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;NVARCHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;100&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHILE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;25000&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@sql&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'ALTER&amp;nbsp;TABLE&amp;nbsp;[sparse_col]&amp;nbsp;ADD&amp;nbsp;Col'&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;CONVERT&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'&amp;nbsp;&amp;nbsp;int&amp;nbsp;sparse&amp;nbsp;null&amp;nbsp;'&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt;Sp_executesql&lt;/font&gt;&lt;/b&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@sql&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;The above script adds 25000 columns to a table. One doubts whether its neccassary.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Sparse columns do come with many restrictions like sparse columns cant participate in primary keys,sparse columns cant have default values etc. For complete set of restrictions refer &lt;a href ="http://msdn.microsoft.com/en-us/library/cc280604.aspx"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-7406894260413670463?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/7406894260413670463/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=7406894260413670463&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7406894260413670463'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/7406894260413670463'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/04/sparse-columns.html' title='Sparse Columns'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_TcYicum3HIs/S7YJw3oYJBI/AAAAAAAAAME/BU2pZMXf7-Q/s72-c/normal_column_space.GIF' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-654656155037568270</id><published>2010-03-26T22:40:00.000-07:00</published><updated>2010-03-26T23:03:47.979-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><title type='text'>Changing Server name - sp_dropserver,sp_addserver</title><content type='html'>&lt;p&gt;&lt;br /&gt;When one changes the name of the computer running the SQL Server, one needs to &lt;br /&gt;perform the following steps to ensure that SQL Server also uses the new updated&lt;br /&gt;name. &lt;br /&gt;First to check whether the SQL Server has the correct name, &lt;br /&gt;execute the following query&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;@@SERVERNAME&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;oldname&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Serverproperty&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "red"&gt;'Servername'&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;actual_name&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; @@Servername &lt;/b&gt; gives the computer name that has been set/registered with SQL Server&lt;br /&gt;&lt;b&gt;Serverproperty('Servername')&lt;/b&gt; provides the newly updated name from windows.&lt;br /&gt;&lt;br /&gt;If @@servername,Serverproperty('Servername') results are different then &lt;br /&gt;one should update the new name on the SQL Server. To update, follow the steps&lt;br /&gt;provided below.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; step 1 :- sp_dropserver/sp_addserver&lt;/b&gt;&lt;br /&gt;  &lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt; Sp_dropserver&lt;/font&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "red"&gt;'oldservername'&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt; &gt; &lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&amp;nbsp;&lt;font color = "#FF0080"&gt;&lt;b&gt; Sp_addserver&lt;/font&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "red"&gt;'newservername'&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "red"&gt;'local'&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; step 2 :- update sysjobs&lt;/b&gt;&lt;br /&gt;Applies only for SQL 2k.&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;USE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;msdb&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;UPDATE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;sysjobs&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;originating_server&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Serverproperty&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "red"&gt;'Servername'&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;b&gt; step 3 :- Restart SQL Service &lt;/b&gt;&lt;br /&gt;Restart SQL Services for the changes to take effect.&lt;br /&gt;&lt;br /&gt;Even if one doesnt update the Servername on SQL Server, SQL Server will still function,but one can face a few issues like the error &lt;br /&gt;&lt;br /&gt;&lt;I&gt;'Error 14274: Cannot delete a job that originated from an MSX server'&lt;/I&gt;&lt;br /&gt;The above error appears, when one attempts to change the SQL jobs properties like enabled/disabled and if the Servername is not correctly updated.&lt;br /&gt;Linked server configurations may not work correctly. So in general, if the servername is changed its better to update it on SQL Server using the above mentioned steps.&lt;br /&gt;For more details refer &lt;a href ="http://msdn.microsoft.com/en-us/library/ms143799.aspx"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-654656155037568270?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/654656155037568270/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=654656155037568270&amp;isPopup=true' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/654656155037568270'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/654656155037568270'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/03/changing-server-name.html' title='Changing Server name - sp_dropserver,sp_addserver'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-3099609665960870175</id><published>2010-03-22T08:10:00.000-07:00</published><updated>2010-03-22T08:58:51.722-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Scripts'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><title type='text'>Finding Space Used,Space left on Data and Log files</title><content type='html'>&lt;p&gt;&lt;br /&gt;The data files and log files in SQL Server grow as specified by the growth property of the files.The growth property is specified either in % of existing size or in terms of MB. After a file grows, the newly allocated space is used for the transactions that happen in the database. One of the responsiblities of the DBA can be to keep track of how much of space is used and how much is free&lt;br /&gt;with in the data and the log files. Let us run thro the options we have in SQL Server 2000 and SQL Server 2005.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Option 1 : Perfmon counter &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;A popular option can be to use perfmon counters, but the drawback is perfmon counters provide only the data file size, and not Data file used size, Data file space left. However, they do provide the space left and space used details for log files.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "maroon"&gt;SELECT &lt;/font&gt;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&lt;font color = "maroon"&gt;FROM   sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_os_performance_counters&lt;/font&gt;&lt;br /&gt;&lt;font color = "maroon"&gt;WHERE  object_name  LIKE &lt;/font&gt;&lt;font color = "red"&gt;'%:Databases%'&lt;/font&gt;&lt;br /&gt;&lt;font color = "maroon"&gt;AND    counter_name LIKE &lt;/font&gt;&lt;font color = "red"&gt;'%File%'&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;For SQL 2000 use sysperfinfo&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Option 2 : DBCC Showfilestats() &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;DBCC Showfilestats takes the file id as parameter and provides used space and free space in terms of extents.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DBCC&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;showfilestats&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://3.bp.blogspot.com/_TcYicum3HIs/S6ePPTL0wLI/AAAAAAAAALs/-51Kj-NhMAA/s1600-h/dbcc_showfile_stats.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 400px; height: 187px;" src="http://3.bp.blogspot.com/_TcYicum3HIs/S6ePPTL0wLI/AAAAAAAAALs/-51Kj-NhMAA/s400/dbcc_showfile_stats.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5451483366965952690" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;But, DBCC Showfilestats doesnt provide the info for log files and does it only for data files. So, to help our cause it should be used in combination with perfmon counters.Another word of caution about DBCC Showfilestats(). It is 'undocumneted'. &lt;br /&gt;So, I still prefer a alternative way for checking these details.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Option 3: Fileproperty/sysfiles &lt;/b&gt;&lt;br /&gt;The third option which perhaps meets all our requirements is the one using  sysfiles system table and fileproperty function. Sysfiles provides the size of data and log files in the database. Fileproperty takes filename( from sysfiles table) and the string 'spaceused' as parameter and provides the actually used by the file.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Example: &lt;/b&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;filename&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;size&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Fileproperty&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;font color = "red"&gt;'SpaceUsed'&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;spaceused&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sysfiles&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;The script provided below can be used against any database SQL Server 2000/2005/2008 and extracts the space used and spaceleft information for all the databases in the server.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@rowcnt&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/font&gt;&lt;/i&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@iterator&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/font&gt;&lt;/i&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dbname&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;200&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DECLARE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "#8000FF"&gt;@exec_sql&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;500&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@rowcnt&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;0&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@iterator&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#db_file_info&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[Database_Name]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;SYSNAME&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;  &lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[File_ID]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;SMALLINT&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;  &lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[File_Type]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;10&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;  &lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[File_Name]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;SYSNAME&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;  &lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[File_Path]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;500&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;  &lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[File_Size_in_MB]&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;  &lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[Space_Used_in_MB]&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;  &lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;[Space_Left_in_MB]&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/font&gt;&lt;/i&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;  &lt;font color = "blue"&gt;NOT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;NULL&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;CREATE&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#db&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;dbid&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;INT&lt;/font&gt;&lt;/i&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;name&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;&lt;i&gt;VARCHAR&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;200&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;INSERT&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;INTO&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#db&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;dbid&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;name&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;MASTER&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dbo&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;sysdatabases&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@rowcnt&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;@@ROWCOUNT&lt;/font&gt;&lt;/i&gt;&lt;br /&gt;&lt;font color = "blue"&gt;WHILE&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@iterator&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;&amp;lt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@rowcnt&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;BEGIN&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dbname&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;name&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;#db&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;dbid&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@iterator&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@exec_sql&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;'&amp;nbsp;USE&amp;nbsp;'&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@dbname&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "red"&gt;';&amp;nbsp;Insert&amp;nbsp;into&amp;nbsp;#DB_FILE_INFO&lt;br /&gt;Select&amp;nbsp;db_name(),fileid,case&amp;nbsp;when&amp;nbsp;groupid&amp;nbsp;=&amp;nbsp;0&amp;nbsp;then&amp;nbsp;''log&amp;nbsp;file''&amp;nbsp;else&amp;nbsp;''data&amp;nbsp;file''&amp;nbsp;end,&lt;br /&gt;name,filename, &lt;br /&gt;  &lt;br /&gt;  [file_size] =&amp;nbsp;&lt;br /&gt;   convert(int,round((sysfiles.size*1.000)/128.000,0)),&lt;br /&gt;  [space_used] =&lt;br /&gt;   convert(int,round(fileproperty(sysfiles.name,''SpaceUsed'')/128.000,0)),&lt;br /&gt;  [space_left] =&lt;br /&gt;   convert(int,round((sysfiles.size-fileproperty(sysfiles.name,''SpaceUsed''))/128.000,0))&lt;br /&gt; from&lt;br /&gt;  dbo.sysfiles;&lt;br /&gt;'&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;EXEC&lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@exec_sql&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;SET&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@iterator&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "#8000FF"&gt;@iterator&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;END&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;#db_file_info&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DROP&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#db&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;DROP&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;TABLE&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;#db_file_info&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Resultset is provided below: &lt;/b&gt;&lt;br /&gt;&lt;a href="http://1.bp.blogspot.com/_TcYicum3HIs/S6eTDFp_d0I/AAAAAAAAAL8/-b4n92FBO98/s1600-h/resultset.GIF"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 500px; height: 320px;" src="http://1.bp.blogspot.com/_TcYicum3HIs/S6eTDFp_d0I/AAAAAAAAAL8/-b4n92FBO98/s400/resultset.GIF" border="0" alt=""id="BLOGGER_PHOTO_ID_5451487555222468418" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1619266374223964354-3099609665960870175?l=strictlysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://strictlysql.blogspot.com/feeds/3099609665960870175/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=1619266374223964354&amp;postID=3099609665960870175&amp;isPopup=true' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3099609665960870175'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1619266374223964354/posts/default/3099609665960870175'/><link rel='alternate' type='text/html' href='http://strictlysql.blogspot.com/2010/03/finding-space-usedspace-left-on-data.html' title='Finding Space Used,Space left on Data and Log files'/><author><name>Nagaraj Venkatesan</name><uri>http://www.blogger.com/profile/17134038987463298279</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://3.bp.blogspot.com/_TcYicum3HIs/S6ePPTL0wLI/AAAAAAAAALs/-51Kj-NhMAA/s72-c/dbcc_showfile_stats.GIF' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1619266374223964354.post-1854733866499525538</id><published>2010-03-17T09:13:00.000-07:00</published><updated>2010-03-17T09:45:54.656-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Administration'/><category scheme='http://www.blogger.com/atom/ns#' term='Performance Monitoring'/><title type='text'>Whats causing my tempdb to grow - SQL Server 2005</title><content type='html'>&lt;p&gt;&lt;br /&gt;In SQL Server 2005, with the introduction of Snapshot Isolation levels, tempdb is even more heavily used.In addition to the points listed &lt;a href = "http://strictlysql.blogspot.com/2010/03/whats-causing-my-tempdb-to-grow-sql.html"&gt;here&lt;/a&gt; , Online index rebuild, Snapshot isolation, Read comitted snapshot isolation ( RCSI ),MARS, XML operations,cause heavy tempdb usage. The tempdb usage , in SQL 2005 can be divided into 3 cateogories.&lt;br /&gt;&lt;br /&gt; &lt;b&gt; User objects &lt;/b&gt; : Mainly meant for storing local and global temp tables and  &lt;br /&gt;    table variables.&lt;br /&gt; &lt;b&gt; Internal objects &lt;/b&gt; : Worktables created for order by /sort /group by  &lt;br /&gt;     operators/ hash algortihms etc.&lt;br /&gt; &lt;b&gt; Version store &lt;/b&gt; : Version store is mainly used at two scenarios. They are&lt;br /&gt;                          &lt;b&gt;*&lt;/b&gt; When indexes are built online, version store when &lt;br /&gt;                                   keeps multiple versions of the same row.&lt;br /&gt;                          &lt;b&gt;*&lt;/b&gt; When the database has snapshot isolation enabled &lt;br /&gt;                                   and if any transaction runs at snapshot isolation &lt;br /&gt;                                   or RCSI.&lt;br /&gt;&lt;br /&gt;So, to find whats causing the tempdb growth, one needs to find which category of the three contributes to the growth, the most.&lt;br /&gt;&lt;br /&gt;&lt;b&gt; Step 1 : categorizing the tempdb growth using dm_db_file_space_usage &lt;/b&gt;&lt;br /&gt;&lt;br /&gt; The DMV sys.dm_db_file_space_usage can help us in this cause.&lt;br /&gt; Use the following query to check on which areas tempdb is growing&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "maroon"&gt;SELECT SUM &lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;user_object_reserved_page_count&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "maroon"&gt;    &lt;/font&gt;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;font color = "black"&gt;8&lt;/font&gt;&lt;font color = "maroon"&gt;  AS usr_obj_kb      &lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&lt;font color = "maroon"&gt;       SUM &lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;internal_object_reserved_page_count&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;font color = "black"&gt;8&lt;/font&gt;&lt;font color = "maroon"&gt;  AS internal_obj_kb &lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&lt;font color = "maroon"&gt;       SUM &lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;version_store_reserved_page_count&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "maroon"&gt;  &lt;/font&gt;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;font color = "black"&gt;8&lt;/font&gt;&lt;font color = "maroon"&gt;  AS version_store_kb&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&lt;font color = "maroon"&gt;       SUM &lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;unallocated_extent_page_count&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "maroon"&gt;      &lt;/font&gt;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;font color = "black"&gt;8&lt;/font&gt;&lt;font color = "maroon"&gt;  AS freespace_kb    &lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&lt;font color = "maroon"&gt;       SUM &lt;/font&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;mixed_extent_page_count&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;font color = "maroon"&gt;            &lt;/font&gt;&lt;font color = "silver"&gt;*&lt;/font&gt;&lt;font color = "maroon"&gt; 8 AS mixedextent_kb&lt;/font&gt;&lt;br /&gt;&lt;font color = "maroon"&gt;FROM   sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_db_file_space_usage&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;If usr_obj_kb is taking the major percentage, then it implies that temp tables /table variables&lt;br /&gt;are used heavily. This is not considered a problem as long as you have enough space on the disk.&lt;br /&gt;&lt;br /&gt;If internal_obj_kb is taking the major percentage, then it implies that large amount work tables,worker files are created because of ineffective query plans.&lt;br /&gt;&lt;br /&gt;If version_strore_reserved_page_count is high, then its likely due to long running transactions when there is atleast one active transaction under snapshot isolation level.&lt;br /&gt;&lt;br /&gt;One can poll the content of the above query on dm_db_file_space_usage to a table&lt;br /&gt;every 2 minutes and use it for monitoring. &lt;br /&gt;&lt;br /&gt;&lt;b&gt; Step 2:Identifying queries causing the growth using dm_db_session_space_usage, dm_db_task_space_usage &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;   After identifying which category of tempdb is growing, one needs to pin point which query is causing the growth. Couple of DMVs help in that cause.&lt;br /&gt;    &lt;br /&gt;   &lt;b&gt; sys.dm_db_session_space_usage : &lt;/b&gt;  Provides the number of user pages and internal pages allocated and deallocated for a particular session/connection. Doesnt &lt;br /&gt;provide the values for currently running query.Provides only for completed queries/requests in a batch.&lt;br /&gt;   &lt;b&gt; sys.dm_db_task_space_usage : &lt;/b&gt; Provides the same values as dm_db_session_space_usage but it does only for the currently active queries. &lt;br /&gt;&lt;br /&gt;The following query can help us zoom in on the queries that use user pages and internal pages heavily.&lt;br /&gt;&lt;br /&gt;&lt;font face="Courier New" size="2"&gt;&lt;br /&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r1&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;session_id&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r5&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "blue"&gt;TEXT&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r1&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;internal_objects_alloc_page_count&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;r2&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;task_internal_objects_alloc_page_count&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;internal_objects_alloc_page_count&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r1&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;internal_objects_dealloc_page_count&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;r2&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;task_internal_objects_dealloc_page_count&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;internal_objects_dealloc_page_count&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r1&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;user_objects_alloc_page_count&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;r2&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;task_user_objects_alloc_page_count&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;user_objects_alloc_page_count&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r1&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;user_objects_dealloc_page_count&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;+&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;r2&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;task_user_objects_dealloc_page_count&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;user_objects_dealloc_page_count&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r3&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;client_net_address&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r4&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;host_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r4&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;program_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r4&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;last_request_start_time&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r4&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;last_request_end_time&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r4&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;login_time&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r4&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;cpu_time&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r4&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;memory_usage&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r4&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;reads&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r4&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;writes&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r4&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;logical_reads&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r4&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;status&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r4&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;login_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r4&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;nt_domain&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;r4&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;nt_user_name&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Count&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "black"&gt;1&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_tran_session_transactions&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;t1&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;WHERE&lt;/font&gt;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;t1&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;session_id&lt;/font&gt;&amp;nbsp;&lt;font color = "silver"&gt;=&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;r1&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;session_id&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;open_transactions&lt;/font&gt;&lt;br /&gt;&lt;font color = "blue"&gt;FROM&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;sys&lt;/font&gt;&lt;font color = "silver"&gt;.&lt;/font&gt;&lt;font color = "maroon"&gt;dm_db_session_space_usage&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;r1&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "maroon"&gt;session_id&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Sum&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;internal_objects_alloc_page_count&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;task_internal_objects_alloc_page_count&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Sum&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;internal_objects_dealloc_page_count&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;task_internal_objects_dealloc_page_count&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Sum&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;user_objects_alloc_page_count&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;task_user_objects_alloc_page_count&lt;/font&gt;&lt;font color = "silver"&gt;,&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "fuchsia"&gt;&lt;i&gt;Sum&lt;/font&gt;&lt;/i&gt;&lt;font color = "maroon"&gt;(&lt;/font&gt;&lt;font color = "maroon"&gt;user_objects_dealloc_page_count&lt;/font&gt;&lt;font color = "maroon"&gt;)&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;font color = "blue"&gt;AS&lt;/font&gt;&amp;nbsp;&lt;font color = "maroon"&gt;task_user_objects_dealloc_page_count&lt;/font&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;
